[clang-tools-extra] [clang-tidy] Fix false negative for binary logical expressions for tw… (PR #230846)
Abhay Singh via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 13 01:50:04 PDT 2026
https://github.com/Abhay13332 updated https://github.com/llvm/llvm-project/pull/230846
>From 67518ef04dd71b7633f59aa4c63adb22d1a1043c Mon Sep 17 00:00:00 2001
From: abhay13332 <abhaysingh13072009 at gmail.com>
Date: Tue, 13 Oct 2026 14:02:59 +0530
Subject: [PATCH] [clang-tidy] Fix false negatives in
bugprone-redundant-branch-condition on cases like if (x && y) { if (y) ... }
---
.../bugprone/RedundantBranchConditionCheck.cpp | 7 +++----
clang-tools-extra/docs/ReleaseNotes.md | 12 +++++++++---
.../bugprone/redundant-branch-condition.cpp | 15 ++++++++++++++-
3 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
index 8d608dd4b0e66..7934da1f2fbdd 100644
--- a/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp
@@ -76,10 +76,9 @@ void RedundantBranchConditionCheck::registerMatchers(MatchFinder *Finder) {
ifStmt(
hasCondition(anyOf(
declRefExpr(hasDeclaration(ImmutableVar)).bind(OuterIfVar1Str),
- binaryOperator(
- hasOperatorName("&&"),
- hasEitherOperand(declRefExpr(hasDeclaration(ImmutableVar))
- .bind(OuterIfVar2Str))))),
+ binaryOperator(hasOperatorName("&&"),
+ forEach(declRefExpr(hasDeclaration(ImmutableVar))
+ .bind(OuterIfVar2Str))))),
hasThen(hasDescendant(
ifStmt(hasCondition(anyOf(
declRefExpr(hasDeclaration(
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 1448005253556..c151210f68a4a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -203,9 +203,15 @@ infrastructure are described first, followed by tool-specific sections.
the pointer points to an incomplete (forward-declared) type.
- Improved {doc}`bugprone-redundant-branch-condition
- <clang-tidy/checks/bugprone/redundant-branch-condition>` check by fixing
- false positives when the condition variable is changed later in a loop that
- encloses the inner `if`.
+ <clang-tidy/checks/bugprone/redundant-branch-condition>` check:
+
+ - Fixed false positives when the condition variable is changed later in a loop that
+ encloses the inner `if`.
+
+ - Fixed false negatives when there are two varibles statement in both side
+ of `&&` in `if` like `a && b` .it showing correct warning for first variable
+ and failed for second variable in if condition.eg. `(x && y) { if (y) ... }`
+ in this example warning is not shown properly.
- Fixed a crash in {doc}`bugprone-std-namespace-modification
<clang-tidy/checks/bugprone/std-namespace-modification>` when checking
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/redundant-branch-condition.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/redundant-branch-condition.cpp
index ad2238ff9984f..57ab6456a9987 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/redundant-branch-condition.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/redundant-branch-condition.cpp
@@ -12,7 +12,7 @@ bool tryToExtinguishByVal(bool);
void tryPutFireOut();
bool callTheFD();
void scream();
-
+bool isFireFighterComing();
bool someOtherCondition();
//===--- Basic Positives --------------------------------------------------===//
@@ -278,6 +278,19 @@ void positive_direct_outer_and_rhs() {
// CHECK-FIXES: {{^\ *$}}
}
}
+void positive_direct_outer_and_rhs_bool() {
+ bool onFire = isBurning();
+ bool isComing = isFireFighterComing();
+ if (isComing && onFire) {
+ if (onFire) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition]
+ // CHECK-FIXES: {{^\ *$}}
+ scream();
+ }
+ // CHECK-FIXES: {{^\ *$}}
+ }
+}
+
void positive_indirect_outer_and_rhs() {
bool onFire = isBurning();
More information about the cfe-commits
mailing list