c++ - 为什么 GCC 警告我这一行是 "misleadingly indented as if it were guarded by"if?

标签 c++ gcc-warning

警告是:

/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;
  ^~~~~~~~~~~~~~~~~

代码是这样的:

if (toHexSize < 1)
    toHexSize = 1;

MapTileSizeAtZoom = toHexSize;

如果 MapTileSizeAtZoom ... 行缩进更多,我可以看出这是一种误导,但它与“if”的缩进级别相同,所以这对我来说似乎是正确的。

我以为可能有额外的空格和/或制表符 float ,但我在文本后删除了任何不必要的空白字符,这没有任何区别。

我想可能是被空行搞糊涂了,但是去掉并没有停止警告。

此外,在同一个 .cpp 文件中的这段代码之前,它没有警告:

if (toHexSize < 1)
    toHexSize = 1;

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

为什么它会(完全)警告一个,为什么不警告另一个,无论这是否是 GCC 错误,我该怎么做才能避免它?

代码

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

最佳答案

条件第 49 行缩进使用了空格,但第 51 行缩进使用了制表符

制表符被 GCC 视为 8 个空格。编译器错误地认为它与 if 语句对齐,即使在编辑器中看起来并非如此。这是与空格缩进保持一致的另一个原因(例如,避免在源代码中使用任何制表符)。

关于c++ - 为什么 GCC 警告我这一行是 "misleadingly indented as if it were guarded by"if?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50318900/

相关文章:

gcc - 如何从库头文件中抑制 GCC 警告?

C++ -Weffc++ 警告与指针

gcc - 使用 -isystem 包含 CMake 项目依赖项包含目录

c++ - 持有派生类引用的基类的 std::unique_ptr 在 gcc 编译器中不显示警告,而裸指针显示它。为什么?

c - 检测到堆栈溢出时强制 gcc 编译

c++ - 了解 C++ 中运算符的范围

c++ - 为什么初始化列表初始化需要堆分配数组的大小?

c++ - const 引用的地址可以与引用对象的地址不同吗?

c++ - 在 std::future::unwrap() 和 std::future::get() 中竞赛

C++ 处理函数重载的更好方法