[clang-tools-extra] [clang-tidy] Enhance modernize-use-starts-ends-with to handle substr patterns (PR #116033)

Helmut Januschka via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 18 02:55:57 PST 2024


hjanuschka wrote:

@HerrCai0907 about the fixit.

its strange, using the llvm-lit test case it emits a warning, and even shows where the substr() comes from:

```
       738: /home/chrome/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/checkers/modernize/Output/use-starts-ends-with.cpp.tmp.cpp:315:17: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with]
not:imp1                                                                                                                                                !~~~~~~~                                                                          error: no match expected
        739:  315 | "prefix" == SUBSTR(STR(), 0, 6);
        740:  | ^
        741: /home/chrome/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/checkers/modernize/Output/use-starts-ends-with.cpp.tmp.cpp:309:33: note: expanded from macro 'SUBSTR'
        742:  309 | #define SUBSTR(X, A, B) (X).substr((A), (B))
        743:  | ^
        
```

to reduce the complexity and test it tried to:
```c++
// RUN: %check_clang_tidy -std=c++20 %s modernize-use-starts-ends-with %t -- -debug

#include <string>

void test_macro_case() {
    std::string str("hello world");
    
    #define SUBSTR(X, A, B) (X).substr((A), (B))
    #define STR() str
    
    "prefix" == SUBSTR(STR(), 0, 6);
    
}
```        

```sh
./bin/clang-tidy test.cpp -checks="-*, modernize-use-starts-ends-with" --extra-arg=-std=c++20   -
```

doesnt even come up with a warning. i dont know whats "right" now, warning and showing root cause, seems to be fairly good, fixing a macro might be even a bad idea?

https://github.com/llvm/llvm-project/pull/116033


More information about the cfe-commits mailing list