5 Commits

Author SHA1 Message Date
Clement Courbet
415a82c64a
[clang-tidy] doesNotMutateObject: Handle calls to member functions … (#94362)
…and operators that have non-const overloads.

This allows  `unnecessary-copy-initialization` to warn on more cases.

The common case is a class with a a set of const/non-sconst overloads
(e.g. std::vector::operator[]).

```
void F() {
  std::vector<Expensive> v;
  // ...

  const Expensive e = v[i];
}
```
2024-06-10 11:15:11 +02:00
Clement Courbet
308a236072
[clang-tidy] isOnlyUsedAsConst: Handle static method calls. (#84005)
... using method syntax:

```
struct S {
  static void f()
};

void DoIt(S& s) {
  s.f();  // Does not mutate `s` through the `this` parameter.
}
```
2024-03-07 21:01:46 +01:00
Clement Courbet
94ca854d3c
[clang-tidy] Add support for determining constness of more expressions. (#82617)
This uses a more systematic approach for determining whcich
`DeclRefExpr`s mutate the underlying object: Instead of using a few
matchers, we walk up the AST until we find a parent that we can prove
cannot change the underlying object.

This allows us to handle most address taking and dereference, bindings
to value and const& variables, and track constness of pointee (see
changes in DeclRefExprUtilsTest.cpp).

This allows supporting more patterns in
`performance-unnecessary-copy-initialization`.

Those two patterns are relatively common:

```
const auto e = (*vector_ptr)[i]
```

and

```
const auto e = vector_ptr->at(i);
```

In our codebase, we have around 25% additional findings from
`performance-unnecessary-copy-initialization` with this change. I did
not see any additional false positives.
2024-02-26 09:55:07 +01:00
Shivam Gupta
1e51268837 [clang-tidy] performance-* checks: Also allow allow member expressions to be used in a const manner.
Until now when determining all the const uses of a VarDecl we only considered
how the variable itself was used. This change extends checking for const usages
of the type's members as well.

This increases the number of true positives for various performance checks that
share the same const usage analysis.

Path by Felix Berger

Reviewed By: njames93, PiotrZSL

Differential Revision: https://reviews.llvm.org/D97567
2023-07-24 00:08:29 +05:30
Clement Courbet
3b72448084 [clang-tidy] Add unit tests for DeclRefExprUtils.
In preparation for D114539.
2021-11-24 16:47:55 +01:00