llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: flovent

<details>
<summary>Changes</summary>

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 &lt;col:3&gt; 'GH62206::Base' lvalue 
&lt;UncheckedDerivedToBase (Base)&gt;
        | `-DeclRefExpr &lt;col:3&gt; 'Derived' lvalue Var 0x1d11a400 'd' 
'Derived'
```

This patch consider possible `implicitCastExpr` in reinit matcher.

Closes #<!-- -->62206.

---
Full diff: https://github.com/llvm/llvm-project/pull/197438.diff


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp (+2-1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp (+17) 


``````````diff
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
+  }
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/197438
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to