23 Commits

Author SHA1 Message Date
v1nh1shungry
326d749a36
[clang-tidy] Fix cppcoreguidelines-prefer-member-initializer false positive for inherited members (#153941)
```cpp
struct Base {
  int m;
};

template <class T>
struct Derived : Base {
  Derived() { m = 0; }
};
```

would previously generate the following output:

```
<source>:7:15: warning: 'm' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    7 |   Derived() { m = 0; }
      |               ^~~~~~
      |             : m(0)
```

This patch fixes this false positive.

Note that before this patch the checker won't give false positive for

```cpp
struct Derived : Base {
  Derived() { m = 0; }
};
```

and the constructor's AST is

```
`-CXXConstructorDecl 0x557df03d1fb0 <line:7:3, col:22> col:3 Derived 'void ()' implicit-inline
    |-CXXCtorInitializer 'Base'
    | `-CXXConstructExpr 0x557df03d2748 <col:3> 'Base' 'void () noexcept'
    `-CompoundStmt 0x557df03d2898 <col:13, col:22>
      `-BinaryOperator 0x557df03d2878 <col:15, col:19> 'int' lvalue '='
        |-MemberExpr 0x557df03d2828 <col:15> 'int' lvalue ->m 0x557df03d1c40
        | `-ImplicitCastExpr 0x557df03d2808 <col:15> 'Base *' <UncheckedDerivedToBase (Base)>
        |   `-CXXThisExpr 0x557df03d27f8 <col:15> 'Derived *' implicit this
        `-IntegerLiteral 0x557df03d2858 <col:19> 'int' 0
```

so `isAssignmentToMemberOf` would return empty due to


f0967fca04/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp (L118-L119)

Fixes #104400
2025-08-17 11:42:38 +02:00
Congcong Cai
7deca859e5
[clang-tidy] fix false positive when member initialization depends on structured binging variable in cppcoreguidelines-prefer-member-initializer (#108743)
Fixes: #82970

Detecting dependiences with `varDecl` is too strict. It will ignore the
`bingingDecl`.
This patch wants to use `valueDecl` to match more cases including
`bingingDecl`.
2024-09-16 09:04:32 +08:00
Kazu Hirata
33ceb2dd75
[clang-tidy] Avoid repeated hash lookups (NFC) (#107490) 2024-09-05 19:04:30 -07:00
Congcong Cai
e9cec39239
[clang-tidy] fix incorrect hint for InitListExpr in prefer-member-initializer (#81560) 2024-02-16 09:18:21 +08:00
Carlos Galvez
6f32d6a4f3
[clang-tidy] Remove enforcement of rule C.48 from cppcoreguidelines-prefer-member-init (#80330)
This functionality already exists in
cppcoreguidelines-use-default-member-init. It was deprecated from this
check in clang-tidy 17.

This allows us to fully decouple this check from the corresponding
modernize check, which has an unhealthy dependency.

Fixes https://github.com/llvm/llvm-project/issues/62169

---------

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2024-02-01 21:16:24 +01:00
Piotr Zegar
6a80e56ad0
[clang-tidy] Fix macros handling in cppcoreguidelines-prefer-member-initializer (#72037)
Produces now valid fixes for a member variables initialized with macros.
Correctly uses expansion location instead of location inside macro to
get init code.

Close #70189
2024-01-22 16:17:33 +01:00
Congcong Cai
43e13fdc9e
[NFC][clang-tidy]refactor isAssignmentToMemberOf in PreferMemberInitializerCheck (#71006) 2023-11-02 19:30:06 +08:00
Congcong Cai
263746754a
[clang-tidy]Fix PreferMemberInitializer false positive for reassignment (#70316)
- assignment twice cannot be simplified to once when assigning to
reference type
- safe assignment cannot be advanced before unsafe assignment
2023-10-27 23:56:25 +08:00
Congcong Cai
f5063bf7ed [clang-tidy][NFC]refactor PreferMemberInitializerCheck for readability 2023-10-26 13:37:50 +08:00
Piotr Zegar
cbdc3e1bf9 [clang-tidy][NFC] Fix cppcoreguidelines-init-variables findings
Fix issues found by clang-tidy in clang-tidy source directory.
2023-08-27 11:59:03 +00:00
Piotr Zegar
7a4b12e39b
[clang-tidy] Exclude delegate constructors in cppcoreguidelines-prefer-member-initializer
As proposed by check fix would result in compilation error,
lets exclude delegate constructors from being checked.

Fixes: #52818

Reviewed By: ccotter

Differential Revision: https://reviews.llvm.org/D157242
2023-08-07 06:08:39 +00:00
Piotr Zegar
b34ca0851a [clang-tidy] Fix handling of UseAssignment option in cppcoreguidelines-prefer-member-initializer
From now on check will use value from cppcoreguidelines-prefer-member-initializer
and fallback to modernize-use-default-member-init.UseAssignment if not specified.

Fixes: #55616.

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D147929
2023-04-16 10:34:48 +00:00
Carlos Galvez
7d2ea6c422 [clang-tidy][NFC] Use C++17 nested namespaces in the clang-tidy folder
Fix applied by running:

run-clang-tidy.py -checks=-*,modernize-concat-nested-namespaces

Differential Revision: https://reviews.llvm.org/D141770
2023-01-14 18:51:39 +00:00
Nathan James
b859c39c40
[clang-tidy] Add a Standalone diagnostics mode to clang-tidy
Adds a flag to `ClangTidyContext` that is used to indicate to checks that fixes will only be applied one at a time.
This is to indicate to checks that each fix emitted should not depend on any other fixes emitted across the translation unit.
I've currently implemented the `IncludeInserter`, `LoopConvertCheck` and `PreferMemberInitializerCheck` to use these support these modes.

Reasoning behind this is in use cases like `clangd` it's only possible to apply one fix at a time.
For include inserter checks, the include is only added once for the first diagnostic that requires it, this will result in subsequent fixes not having the included needed.

A similar issue is seen in the `PreferMemberInitializerCheck` where the `:` will only be added for the first member that needs fixing.

Fixes emitted in `StandaloneDiagsMode` will likely result in malformed code if they are applied all together, conversely fixes currently emitted may result in malformed code if they are applied one at a time.
For this reason invoking `clang-tidy` from the binary will always with `StandaloneDiagsMode` disabled, However using it as a library its possible to select the mode you wish to use, `clangd` always selects `StandaloneDiagsMode`.

This is an example of the current behaviour failing
```lang=c++
struct Foo {
  int A, B;
  Foo(int D, int E) {
    A = D;
    B = E; // Fix Here
  }
};
```
Incorrectly transformed to:
```lang=c++
struct Foo {
  int A, B;
  Foo(int D, int E), B(E) {
    A = D;
     // Fix Here
  }
};
```
In `StandaloneDiagsMode`, it gets transformed to:
```lang=c++
struct Foo {
  int A, B;
  Foo(int D, int E) : B(E) {
    A = D;
     // Fix Here
  }
};
```

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D97121
2022-04-16 09:53:35 +01:00
Nathan James
d0fcbb3783
[clang-tidy] Fix invalid fix-it for cppcoreguidelines-prefer-member-initializer
Fixes https://github.com/llvm/llvm-project/issues/53515.

Reviewed By: LegalizeAdulthood

Differential Revision: https://reviews.llvm.org/D118927
2022-04-07 19:13:50 +01:00
Kazu Hirata
dfc46f0268 [clang-tidy] Drop unnecessary const from return types (NFC)
Identified with readability-const-return-type.
2021-09-05 08:37:27 -07:00
Martin Storsjö
86029e4c22 [clang-tools-extra] Rename StringRef _lower() method calls to _insensitive() 2021-06-25 00:22:01 +03:00
Nathan James
a2e15fa532
[clang-tidy] Harden PreferMemberInitializerCheck
Prevent warning when the values are initialized using fields that will be initialized later or VarDecls defined in the constructors body.
Both of these cases can't be safely fixed.
Also improve logic of finding where to insert member initializers, previously it could be confused by in class member initializers.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D97132
2021-02-22 19:41:11 +00:00
Nathan James
557d2ade01
[NFC] Refactor PreferMemberInitializerCheck 2021-02-20 23:35:58 +00:00
Nathan James
27553933a8 [clang-tidy] Add support for diagnostics with no location
Add methods for emitting diagnostics with no location as well as a special diagnostic for configuration errors.
These show up in the errors as [clang-tidy-config].
The reason to use a custom name rather than the check name is to distinguish the error isn't the same category as the check that reported it.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D91885
2020-12-08 20:29:31 +00:00
Adam Balogh
4fc0214a10 [clang-tidy] New check cppcoreguidelines-prefer-member-initializer
Finds member initializations in the constructor body which can be placed
into the initialization list instead. This does not only improves the
readability of the code but also affects positively its performance.
Class-member assignments inside a control statement or following the
first control statement are ignored.

Differential Revision: https://reviews.llvm.org/D71199
2020-09-21 14:42:58 +02:00
Roman Lebedev
ebf496d805
Revert "[clang-tidy] New check readability-prefer-member-initializer"
Either contains unbounded loops, or has *very* high runtime,
100+x of all the current clang-tidy checks.

This reverts commit f5fd7486d6c0debb465de3e927fcc31884874280.
2020-09-10 16:32:18 +03:00
Adam Balogh
f5fd7486d6 [clang-tidy] New check readability-prefer-member-initializer
Finds member initializations in the constructor body which can
be placed to the member initializers of the constructor instead.
This does not only improves the readability of the code but also
affects positively its performance. Class-member assignments
inside a control statement or following the first control
statement are ignored.

Differential Revision: https://reviews.llvm.org/D71199
2020-08-31 15:59:29 +02:00