https://github.com/flovent created
https://github.com/llvm/llvm-project/pull/197438
When calling base class's `operator=` through derived object, a implicit cast
with `UncheckedDerivedToBase` will be generated:
```
void foo() {
Base b;
Derived d;
std::move(d);
d = b;
}
```
AST for `d = b`'s `d`:
```
|-ImplicitCastExpr <col:3> 'GH62206::Base' lvalue
<UncheckedDerivedToBase (Base)>
| `-DeclRefExpr <col:3> 'Derived' lvalue Var 0x1d11a400 'd' 'Derived'
```
This patch consider possible `implicitCastExpr` in reinit matcher.
Closes #62206.
>From bf4b01f211deea5246348eceb0a28cc27825c31f Mon Sep 17 00:00:00 2001
From: flovent <[email protected]>
Date: Wed, 13 May 2026 21:10:39 +0800
Subject: [PATCH] [clang-tidy] Fix false positives about reinitialization
detection in `bugprone-use-after-move`
---
.../clang-tidy/bugprone/UseAfterMoveCheck.cpp | 3 ++-
clang-tools-extra/docs/ReleaseNotes.rst | 3 +++
.../checkers/bugprone/use-after-move.cpp | 17 +++++++++++++++++
3 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
index 31c70b3643be6..7fd810554804f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -132,7 +132,8 @@ makeReinitMatcher(const ValueDecl *MovedVariable,
// operator, test for built-in assignment as well, since
// template functions may be instantiated to use std::move()
on
// built-in types.
- binaryOperation(hasOperatorName("="), hasLHS(DeclRefMatcher)),
+ binaryOperation(hasOperatorName("="),
+ hasLHS(ignoringImpCasts(DeclRefMatcher))),
// Declaration. We treat this as a type of reinitialization
// too, so we don't need to treat it separately.
declStmt(hasDescendant(equalsNode(MovedVariable))),
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst
b/clang-tools-extra/docs/ReleaseNotes.rst
index edcf490433f7f..2024cec8f026d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -430,6 +430,9 @@ Changes in existing checks
- Avoid false positives when moving object to a base type then accessing
non-base members.
+ - Avoid false positives when moving object is reinitialized via the base
+ class's ``operator=``.
+
- Improved :doc:`cppcoreguidelines-avoid-capturing-lambda-coroutines
<clang-tidy/checks/cppcoreguidelines/avoid-capturing-lambda-coroutines>`
check by adding the `AllowExplicitObjectParameters` option. When enabled,
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
index 80df2b99eb874..65b7f8f1d619c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
@@ -1790,3 +1790,20 @@ void Run() {
db6.Query();
}
} // namespace custom_reinitialization
+
+namespace GH62206 {
+ struct Base {
+
+ };
+
+ struct Derived: public Base {
+ using Base::operator=;
+ };
+
+ void foo() {
+ Base b;
+ Derived d;
+ std::move(d);
+ d = b; // Should not warn
+ }
+}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits