为什么 GCC 警告我这条线“被误导性地缩进,就好像它被 if 保护一样”?

2024-01-10

警告是:

/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”的缩进水平相同,所以这对我来说似乎是正确的。

我想也许有多余的空格和/或制表符浮动,但我修剪掉了文本后任何不必要的空白字符,这没有什么区别。

我想也许它被空行弄糊涂了,但把它拿出来并没有停止警告。

此外,在同一个 .cpp 文件中,它之前是以下代码,它不会发出警告:

if (toHexSize < 1)
    toHexSize = 1;

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

为什么它(根本)警告一个,为什么它不警告另一个,这是否是一个 GCC bug,以及我能做些什么来避免它?

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

条件行 49 中使用了空格来缩进,但是a tab用于缩进第 51 行。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 GCC 警告我这条线“被误导性地缩进,就好像它被 if 保护一样”? 的相关文章

随机推荐