Commit 6540094
committed
[clang-tidy][modernize-use-starts-ends-with] Fix operator rewriting false negative
In C++20, `operator!=` can be rewritten by negating `operator==`. This is the case for `std::string`, where `operator!=` is not provided hence relying on this rewriting.
Cover this case by matching `binaryOperation` and adding one case to `isNegativeComparison`.
This is only relevant in the newly added `substr` case, previous cases used a simple BinaryOperator. Release notes already mention adding this new case so I don't think further comments there are necessary.
Testing on non-mock:
```
> cat tmp.cpp
#include <string>
void f(std::string u, std::string v) { u.substr(0, v.size()) != v; }
# Before change, no warning.
> ./build/bin/clang-tidy -checks="-*,modernize-use-starts-ends-with" tmp.cpp -- -std=c++20
# After change.
> ./build/bin/clang-tidy -checks="-*,modernize-use-starts-ends-with" tmp.cpp -- -std=c++20
tmp.cpp:2:42: warning: use starts_with instead of substr [modernize-use-starts-ends-with]
2 | void f(std::string u, std::string v) { u.substr(0, v.size()) != v; }
| ^~~~~~ ~~~~~~~~~~~~~~~~~
| ! starts_with v)
```1 parent 5084482 commit 6540094
File tree
3 files changed
+23
-7
lines changed- clang-tools-extra
- clang-tidy/modernize
- test/clang-tidy/checkers
- Inputs/Headers
- modernize
3 files changed
+23
-7
lines changedLines changed: 7 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | | - | |
| 23 | + | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
29 | 33 | | |
30 | 34 | | |
31 | 35 | | |
| |||
185 | 189 | | |
186 | 190 | | |
187 | 191 | | |
188 | | - | |
| 192 | + | |
189 | 193 | | |
190 | 194 | | |
191 | 195 | | |
| |||
Lines changed: 6 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | 139 | | |
144 | 140 | | |
145 | 141 | | |
| |||
148 | 144 | | |
149 | 145 | | |
150 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
151 | 152 | | |
152 | 153 | | |
153 | 154 | | |
| 155 | + | |
154 | 156 | | |
155 | 157 | | |
156 | 158 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
0 commit comments