[clang-tools-extra] [clang-tidy] Add modernize-substr-to-starts-with check (PR #116033)

Nicolas van Kempen via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 14 08:10:03 PST 2024


https://github.com/nicovank commented:

This feels very intrusive, a lot should be able to be re-used from things already used in the matcher. I expect something like this would be sufficient:

```cpp
Finder->addMatcher(
    cxxOperatorCallExpr(
        hasAnyOperatorName("==", "!="),
        hasOperands(
            expr().bind("needle"),
            cxxMemberCallExpr(
                argumentCountIs(2), hasArgument(0, ZeroLiteral),
                hasArgument(1, lengthExprForStringNode("needle")),
                callee(cxxMethodDecl(hasName("substr"),
                                      ofClass(OnClassWithStartsWithFunction))
                            .bind("find_fun")))
                .bind("find_expr")))
        .bind("expr"),
    this);
```

I expect that the only change in `check` would be related to the negation since this will cause some segmentation fault from always getting it as a `BinaryOperator`.

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


More information about the cfe-commits mailing list