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

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 19 16:00:25 PST 2024


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

The detected issues together with the fixes can currently produce code that can't compile. For example:

```diff
void f(std::string_view sv);
void g(std::string_view sv) {
-  const auto copy = sv = sv.substr(0, sv.length() - 3);                                                                                                                                                                                   
+  const auto copy = sv.remove_suffix(3);                                                                                                                                                                                        
-  f(sv = sv.substr(0, sv.length() - 3));                                                                                                                                                                                                               
+  f(sv.remove_suffix(3));
}
```

I think the solution is to detect if the parent of the `CXXoperatorCallExpr` is an `ExprWithCleanup`: https://godbolt.org/z/d79MhEEea
Displaying a warning for these cases could still be done, just without the fix.

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


More information about the cfe-commits mailing list