Search code examples
c++gcc-warning

Why is GCC warning me this line is "misleadingly indented as if it were guarded by" an if?


The warning is:

/home/dronz/OF/apps/myApps/HexMap/src/HexMap.cpp:48:5: warning:
this ‘if’ clause does not guard... [-Wmisleading-indentation]

     if (toHexSize < 1)
     ^~

/home/dronz/OF/apps/myApps/HexMap/src/HexMap.cpp:51:2: note: ...
this statement, but the latter is misleadingly indented as
if it were guarded by the ‘if’

  MapTileSizeAtZoom = toHexSize;
  ^~~~~~~~~~~~~~~~~

And the code is this:

if (toHexSize < 1)
    toHexSize = 1;

MapTileSizeAtZoom = toHexSize;

I could see it being misleading if the MapTileSizeAtZoom ... line were indented more, but it's at the same level of indentation as the 'if', so that seems correct to me.

I thought maybe there were extra spaces and/or tabs floating around, but I trimmed out any needless whitespace characters after text, and that made no difference.

I thought maybe it was confused by the blank line, but taking it out did not stop the warning.

Furthermore, before it in the same .cpp file is this code, which it does not warn about:

if (toHexSize < 1)
    toHexSize = 1;

HexInfo centerOnHex;
if (SelectedHex.type != -1)

Why does it warn about one (at all), and why doesn't it warn about the other, whether or not this is a GCC bug, and what can I do to avoid it?

Code

#include "HexMap.h"
#include <algorithm>
#include <cmath>

//--------------------------------------------------------------
HexMap::HexMap()
{}

//--------------------------------------------------------------
int HexMap::SetZoom(int toHexSize)
{
    if (toHexSize < 1)
        toHexSize = 1;

    HexInfo centerOnHex;
    if (SelectedHex.type != -1)
    {
        // Center map on the selected hex.
        centerOnHex = SelectedHex;
    }
    else
    {
        // Center map on current center of viewpoint.
        centerOnHex = GetHex(
            MapFrame.x + MapFrame.getWidth() / 2,
            MapFrame.y + MapFrame.getHeight() / 2 );
        if ((centerOnHex.x > WORLDMAPWIDTH) || (centerOnHex.x < 0))
            centerOnHex.x = WORLDMAPWIDTH / 2;
        if ((centerOnHex.y > WORLDMAPHEIGHT) || (centerOnHex.y < 0))
            centerOnHex.y = WORLDMAPHEIGHT / 2;
    }

    setHexDisplaySize(toHexSize);

    // Center map:
    HexOriginX = MapFrame.x + MapTileWidth  * 0.25f;
    HexOriginY = MapFrame.y + MapTileHeight * 0.5f;
    ViewPosOnWorld.set(
        centerOnHex.x - (MapFrame.getWidth() / 2) / MapTileWidth,
        centerOnHex.y - (MapFrame.getHeight() / 2) / MapTileHeight);

    return 0;
}

//--------------------------------------------------------------
void HexMap::setHexDisplaySize(int toHexSize)
{
    if (toHexSize < 1)
        toHexSize = 1;

    MapTileSizeAtZoom = toHexSize;
    MapTileWidth = MapTileSizeAtZoom * 1.5f; // hex x-spacing is 1.5 * r
    MapTileHeight = MapTileSizeAtZoom * 1.73205f; // hex height = sqrt(3*r)

    // Size images & hexmask:
    MaskWidth = MapTileHeight * 1.154700538;  // 1/(sqrt(3)/2)
}

Solution

  • There were spaces used to indent the conditional line 49, but a tab was used to indent line 51.

    Tabs are viewed as 8 spaces by GCC. The compiler mistakenly thought that it was aligned with the if statement, even though it didn't seem like that in the editor. It is another reason to be consistent with white space indentation (e.g., by avoiding using any tabs in source code).