Issue 176225
Summary clang-tidy-21 FIX-ITs include <algorithm> header twice (readability-use-std-min-max and modernize-use-ranges)
Labels
Assignees
Reporter e8y
    # Reproduce Steps

## Setup

Create a directory with three files (test.cpp, test.h, and compile_commands.json), with the following content. (You may need to change the directory in compile_commands.json to use an absolute path.)

**test.cpp**

```cpp

```

**test.h**

```cpp

```

**compile_commands.json**

```json
[
 {
    "directory": ".",
    "command": "clang++-21 -std=c++20 -Wall -Wextra -c test.cpp",
    "file": "test.cpp"
  }
]
```

## Invoke clang-tidy

```
$ clang-tidy-21 -p . --fix --warnings-as-errors='*' --checks='readability-use-std-min-max,modernize-use-ranges,readability-duplicate-include' test.cpp
```

Command output:

```
3 warnings generated.
test.cpp:5:5: error: use a ranges version of this algorithm [modernize-use-ranges,-warnings-as-errors]
    2 |
    3 | int main() {
 4 |     std::vector<int> nums {1, 3, 2, 4};
    5 | std::sort(nums.begin(), nums.end());
      |     ^~~~~~~~~ ~~~~~~~~~~~~ ~~~~~~~~~~
      |     std::ranges::sort nums
test.cpp:2:1: note: FIX-IT applied suggested code changes
    2 |
      | ^
test.cpp:5:5: note: FIX-IT applied suggested code changes
    5 |     std::sort(nums.begin(), nums.end());
      |     ^
test.cpp:5:15: note: FIX-IT applied suggested code changes
    5 |     std::sort(nums.begin(), nums.end());
      | ^
test.cpp:5:28: note: FIX-IT applied suggested code changes
    5 |     std::sort(nums.begin(), nums.end());
      | ^
test.cpp:9:9: error: use `std::max` instead of `>` [readability-use-std-min-max,-warnings-as-errors]
    2 |
    3 | int main() {
    4 |     std::vector<int> nums {1, 3, 2, 4};
    5 | std::sort(nums.begin(), nums.end());
    6 |
    7 |     int largest = nums.front();
    8 |     for (auto n : nums) {
    9 |         if (n > largest) {
      |         ^~~~~~~~~~~~~~~~~~
      |         largest = std::max(n, largest);
   10 |             largest = n;
      | ~~~~~~~~~~~~
   11 |         }
      |         ~
test.cpp:2:1: note: FIX-IT applied suggested code changes
    2 |
      | ^
test.cpp:9:9: note: FIX-IT applied suggested code changes
    9 |         if (n > largest) {
      |         ^
clang-tidy applied 6 of 6 suggested fixes.
Suppressed 1 warnings (1 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
2 warnings treated as errors
```

**test.cpp** after fix-its:

```
#include "test.h"

#include <algorithm>

#include <algorithm>

int main() {
    std::vector<int> nums {1, 3, 2, 4};
    std::ranges::sort(nums);

    int largest = nums.front();
    for (auto n : nums) {
        largest = std::max(n, largest);
    }
}
```

The algorithm header was included twice by the different checks. If you run clang-tidy again, this fails due to the `readability-duplicate-include` rule. It can be fixed again, but it requires 2 passes.

In my case, my build script does a first pass with `-fix` and a second pass without it, and this triggers an error which was not introduced by the user.

# Environment

```
$ clang-tidy-21 --version
Ubuntu LLVM version 21.1.8
  Optimized build.

$ uname -a
Linux alternating-bike 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.3 LTS
Release:        24.04
Codename:       noble
```

I installed LLVM via `./llvm.sh 21 all` downloaded from `https://apt.llvm.org/llvm.sh`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to