clang-tidy-21 FIX-ITs include <algorithm> header twice (readability-use-std-min-max and modernize-use-ranges)
Nobody has claimed this yet.
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
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
#include "test.h"
int main() {
std::vector<int> nums {1, 3, 2, 4};
std::sort(nums.begin(), nums.end());
int largest = nums.front();
for (auto n : nums) {
if (n > largest) {
largest = n;
}
}
}
test.h
#include <vector>
#include <algorithm>
compile_commands.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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the test.cpp, test.h, and compile_commands.json reproducer and run the stated clang-tidy command with readability-use-std-min-max, modernize-use-ranges, and readability-duplicate-include. Read the fix-it behavior of the two checks and their interaction with include insertion. Done means one fix pass does not introduce a duplicate include or cause the subsequent duplicate-include check to fail.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100