[clang-tools-extra] [clang-tidy] Fix false positives about reinitialization detection in `bugprone-use-after-move` (PR #197438)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 15 19:38:36 PDT 2026
https://github.com/flovent updated https://github.com/llvm/llvm-project/pull/197438
>From bf4b01f211deea5246348eceb0a28cc27825c31f Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Wed, 13 May 2026 21:10:39 +0800
Subject: [PATCH 1/3] [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
+ }
+}
>From 6b4d1ce38ee04dbd9d1207a49abdf26a6e07fd74 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Wed, 13 May 2026 22:20:35 +0800
Subject: [PATCH 2/3] Update
clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
Co-authored-by: Zeyi Xu <zeyi2 at nekoarch.cc>
---
.../test/clang-tidy/checkers/bugprone/use-after-move.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 65b7f8f1d619c..bc38d2b3a484c 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
@@ -1806,4 +1806,4 @@ namespace GH62206 {
std::move(d);
d = b; // Should not warn
}
-}
+} // namespace GH62206
>From 36704bdb29a983b1125b10d6c6c62a35c797e977 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Fri, 15 May 2026 22:07:19 +0800
Subject: [PATCH 3/3] consider outside paren
---
.../clang-tidy/bugprone/UseAfterMoveCheck.cpp | 2 +-
.../test/clang-tidy/checkers/bugprone/use-after-move.cpp | 7 +++++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
index 7fd810554804f..399442f52bd33 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -133,7 +133,7 @@ makeReinitMatcher(const ValueDecl *MovedVariable,
// template functions may be instantiated to use std::move() on
// built-in types.
binaryOperation(hasOperatorName("="),
- hasLHS(ignoringImpCasts(DeclRefMatcher))),
+ hasLHS(ignoringParenImpCasts(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/test/clang-tidy/checkers/bugprone/use-after-move.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
index bc38d2b3a484c..d4e78d359b654 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
@@ -1806,4 +1806,11 @@ namespace GH62206 {
std::move(d);
d = b; // Should not warn
}
+
+ void paren_version() {
+ Base b;
+ Derived d;
+ std::move(d);
+ (d) = b; // Should not warn
+ }
} // namespace GH62206
More information about the cfe-commits
mailing list