17

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)
}
0

2 Answers 2

49

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).

8
  • 4
    Using a formatter such as clang-format is a good way to avoid problems like this in the future and enforce code style consistency in the repository.
    – eozd
    Commented May 13, 2018 at 18:17
  • 3
    or avoid the problem completely by always using braces with if statements unless the whole if statement is on a single line. Getting into this habit will save you pain in the future when you add a second statement to the if block and forget to add the braces. Commented May 13, 2018 at 18:37
  • 2
    You can also, on most IDEs set the tab to translate to spaces. One of my earliest scripts on the Mainframe was rogstop which compared the indentation of COBOL with the location of full stops (no braces, they're too complicated, so a block was just lines of code without a full stop at the end. Yeah - that one found dozens of bugs with either missing full stops at the last line or an extra one part way through.
    – Mike
    Commented May 12, 2021 at 8:35
  • 2
    "Tabs are viewed as 8 spaces by GCC": Some evidence: "By default, -Wmisleading-indentation assumes tabs to be 8 spaces wide." Commented May 13, 2022 at 16:23
  • 3
    Option -Wmisleading-indentation Commented May 13, 2022 at 16:26
2

Use braces {}:

if(x == y) {
    x = x + 1;
}

This should fix the problem regardless of any indentation issues in the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.