Commit 23b4bcd
committed
[clang-tidy] Enhance modernize-use-starts-ends-with with substr detection
Enhances the modernize-use-starts-ends-with check to detect substr-based
patterns that can be replaced with starts_with() (C++20). This improves code
readability and efficiency by avoiding temporary string creation.
New patterns detected:
str.substr(0, n) == "foo" -> str.starts_with("foo")
"foo" == str.substr(0, n) -> str.starts_with("foo")
str.substr(0, n) != "foo" -> !str.starts_with("foo")
str.substr(0, strlen("foo")) == "foo" -> str.starts_with("foo")
str.substr(0, prefix.size()) == "foo" -> str.starts_with("foo")
The enhancement:
- Integrates with existing starts_with patterns
- Handles substr with zero first argument
- Supports length via literals, strlen(), and size()/length()
- Validates string literal length matches
- Handles both == and != operators
Part of modernize-use-starts-ends-with check.1 parent 5845688 commit 23b4bcd
File tree
6 files changed
+216
-99
lines changed- clang-tools-extra
- clang-tidy/modernize
- docs
- clang-tidy/checks/modernize
- test/clang-tidy/checkers
- Inputs/Headers
- modernize
6 files changed
+216
-99
lines changedLines changed: 91 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
33 | 44 | | |
34 | 45 | | |
35 | 46 | | |
| |||
171 | 182 | | |
172 | 183 | | |
173 | 184 | | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
174 | 239 | | |
175 | 240 | | |
176 | 241 | | |
177 | | - | |
| 242 | + | |
178 | 243 | | |
179 | 244 | | |
180 | 245 | | |
| |||
183 | 248 | | |
184 | 249 | | |
185 | 250 | | |
| 251 | + | |
186 | 252 | | |
187 | 253 | | |
188 | 254 | | |
189 | 255 | | |
190 | 256 | | |
191 | 257 | | |
192 | | - | |
| 258 | + | |
193 | 259 | | |
194 | | - | |
195 | | - | |
196 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
197 | 264 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
205 | 269 | | |
206 | | - | |
207 | | - | |
| 270 | + | |
| 271 | + | |
208 | 272 | | |
209 | 273 | | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
216 | 277 | | |
217 | | - | |
218 | | - | |
219 | | - | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
220 | 288 | | |
221 | 289 | | |
222 | 290 | | |
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
247 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
248 | 251 | | |
249 | 252 | | |
250 | 253 | | |
| |||
Lines changed: 12 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
| 64 | + | |
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
| |||
0 commit comments