llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.24k stars 11.66k forks source link

`readability-misleading-indentation` false positive on several stacked loops without indentation #89595

Open HolyBlackCat opened 5 months ago

HolyBlackCat commented 5 months ago
int main()
{
    for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    {}
    (void)42;
}

Analyzing with clang-tidy 1.cpp -checks=readability-misleading-indentation -- results in a false positive:

1 warning generated.
/path/to/1.cpp:6:5: warning: misleading indentation: statement is indented too deeply [readability-misleading-indentation]
    6 |     (void)42;
      |     ^
/path/to/1.cpp:3:5: note: did you mean this line to be inside this 'for'
    3 |     for (int i = 0; i < 3; i++)
      |     ^

Using this indentation style for 2D loops is not unheard of, and it's a shame that clang-tidy warns about those. (The warning is otherwise useful, so I don't want to disable it everywhere.)

I suggest that this warning should be skipped for control statements with non-indented bodies.

llvmbot commented 5 months ago

@llvm/issue-subscribers-clang-tidy

Author: Egor (HolyBlackCat)

```cpp int main() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) {} (void)42; } ``` Analyzing with `clang-tidy 1.cpp -checks=readability-misleading-indentation --` results in a false positive: ``` 1 warning generated. /path/to/1.cpp:6:5: warning: misleading indentation: statement is indented too deeply [readability-misleading-indentation] 6 | (void)42; | ^ /path/to/1.cpp:3:5: note: did you mean this line to be inside this 'for' 3 | for (int i = 0; i < 3; i++) | ^ ``` Using this indentation style for 2D loops is not unheard of, and it's a shame that clang-tidy warns about those. (The warning is otherwise useful, so I don't want to disable it everywhere.) I suggest that this warning should be skipped for control statements with non-indented bodies.