[clang-tools-extra] [clang-tidy] Ignore requires expr in bugprone-assignment-in-if-condition (PR #98079)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 8 14:17:04 PDT 2024


llvmbot wrote:


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

@llvm/pr-subscribers-clang-tidy

Author: Piotr Zegar (PiotrZSL)

<details>
<summary>Changes</summary>

Ignore assignments in RequiresExpr, to avoid false positives.

Fixes #<!-- -->97972

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


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp (+6) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4) 
- (added) clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition-cxx20.cpp (+6) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp
index 9e56ee66a064fb..e03cac6c5fd836 100644
--- a/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp
@@ -39,6 +39,12 @@ void AssignmentInIfConditionCheck::check(
           return true;
         }
 
+        // Dont traverse into any requires expressions.
+        bool TraverseRequiresExpr(RequiresExpr *,
+                                  DataRecursionQueue * = nullptr) {
+          return true;
+        }
+
         bool VisitBinaryOperator(BinaryOperator *BO) {
           if (BO->isAssignmentOp())
             Check.report(BO);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index bde096b9eebd9f..c1fa502534ea52 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -232,6 +232,10 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/assert-side-effect>` check by detecting side
   effect from calling a method with non-const reference parameters.
 
+- Improved :doc:`bugprone-assignment-in-if-condition
+  <clang-tidy/checks/bugprone/assignment-in-if-condition>` check by ignoring
+  assignments in the C++20 ``requires`` clause.
+
 - Improved :doc:`bugprone-casting-through-void
   <clang-tidy/checks/bugprone/casting-through-void>` check by ignoring casts
   where source is already a ``void``` pointer, making middle ``void`` pointer
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition-cxx20.cpp
new file mode 100644
index 00000000000000..b332b2e49ec7f1
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition-cxx20.cpp
@@ -0,0 +1,6 @@
+// RUN: %check_clang_tidy -std=c++20 %s bugprone-assignment-in-if-condition %t
+
+void testRequires() {
+  if constexpr (requires(int &a) { a = 0; }) {
+  }
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/98079


More information about the cfe-commits mailing list