From b9852ff5fc4986c6cf8c4ecd1eb5726d55a08ea3 Mon Sep 17 00:00:00 2001 From: Piotr Zegar Date: Fri, 12 Jul 2024 19:58:45 +0200 Subject: [PATCH] [clang-tidy] Ignore requires expr in bugprone-assignment-in-if-condition (#98079) 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 9e56ee66a064..e03cac6c5fd8 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 bde096b9eebd..c1fa502534ea 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -232,6 +232,10 @@ Changes in existing checks ` check by detecting side effect from calling a method with non-const reference parameters. +- Improved :doc:`bugprone-assignment-in-if-condition + ` check by ignoring + assignments in the C++20 ``requires`` clause. + - Improved :doc:`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 000000000000..b332b2e49ec7 --- /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; }) { + } +}