[clang-tools-extra] [clang-tidy] Add readability-string-view-substr check (PR #120055)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 13 14:58:20 PST 2025


https://github.com/5chmidti commented:

You can move even more into the matchers. Here is a mock-up of what I mean (untested):

```
m cxxMemberCallExpr(
  anyOf(
    cxxMemberCallExpr(
        argumentCountIs(1)
       # is substr, and bind arg 1
    ),
    cxxMemberCallExpr(
        argumentCountIs(2),
        hasArgument(0, 
            expr(anyOf(integerLiteral(equals(0)), 
                     # evaluatesTo(0) # custom matcher
        )).bind("zero")),
        hasArgument(1, 
            expr(anyOf(
                lengthMatcher, 
                binaryOperator(hasOperatorName("-"),
                    hasLHS(lengthMatcher), hasRHS(expr().bind("n")))
            ))
        )
    )
    )
)
```

Then you can discern which case you have by checking what is bound.

```c++
// gettiung bound nodes
if (!Zero) {
  if (N)
    /*case 1*/;
  return;
}
if (N) {
  /*case 2*/
  return;
}
if (/*LHS and RHS of assign are equal*/) {// could technically be in the matcher, but this looks a bit better actually, IMO
  /*case 3*/;
  return;
}

/*case 4*/
```

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


More information about the cfe-commits mailing list