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

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 8 14:16:34 PDT 2024


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

Ignore assignments in RequiresExpr, to avoid false positives.

Fixes #97972

>From 90e7fb770a09ec2327d255596b79168f055d6e14 Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Mon, 8 Jul 2024 21:14:25 +0000
Subject: [PATCH] [clang-tidy] Ignore requires expr in
 bugprone-assignment-in-if-condition

Ignore assignments in RequiresExpr, to avoid false positives.

Fixes #97972
---
 .../clang-tidy/bugprone/AssignmentInIfConditionCheck.cpp    | 6 ++++++
 clang-tools-extra/docs/ReleaseNotes.rst                     | 4 ++++
 .../checkers/bugprone/assignment-in-if-condition-cxx20.cpp  | 6 ++++++
 3 files changed, 16 insertions(+)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/assignment-in-if-condition-cxx20.cpp

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; }) {
+  }
+}



More information about the cfe-commits mailing list