Skip to content

Commit a573e50

Browse files
committed
fix the problem with hasAncestor
1 parent b529e9e commit a573e50

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

clang-tools-extra/clang-tidy/performance/BoolBitwiseOperationCheck.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,12 @@ void BoolBitwiseOperationCheck::check(const MatchFinder::MatchResult &Result) {
167167
ReplaceOperator = FixItHint::CreateReplacement(TokenRange, FixSpelling);
168168

169169
std::optional<BinaryOperatorKind> ParentOpcode;
170-
if (const auto *Parent = Result.Nodes.getNodeAs<BinaryOperator>("p"); Parent)
170+
if (const auto *Parent = Result.Nodes.getNodeAs<BinaryOperator>("p");
171+
Parent && llvm::any_of(std::array{Parent->getLHS(), Parent->getRHS()},
172+
[&](const Expr *E) {
173+
return dyn_cast<BinaryOperator>(
174+
E->IgnoreImpCasts()) == MatchedExpr;
175+
}))
171176
ParentOpcode = Parent->getOpcode();
172177

173178
const auto *RHS =

clang-tools-extra/test/clang-tidy/checkers/performance/bool-bitwise-operation-nontraditional.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,27 @@ void bad_with_priors2() {
186186
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [performance-bool-bitwise-operation]
187187
// CHECK-MESSAGES: :[[@LINE-2]]:16: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [performance-bool-bitwise-operation]
188188
// CHECK-FIXES: (b and c) or a;
189+
}
190+
191+
template<typename T>
192+
T ident(T val) { return val; }
189193

190-
// case to check `hasAncestor` works as we expected:
194+
// cases to check `hasAncestor` works as we expected:
195+
void bad_has_ancestor() {
196+
bool a = false, b = true, c = true;
191197
bool d = false;
192198
d xor (a and b bitand c);
193199
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [performance-bool-bitwise-operation]
194200
// CHECK-FIXES: d xor (a and b and c);
201+
202+
a xor ident(b bitand c or a);
203+
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [performance-bool-bitwise-operation]
204+
// CHECK-FIXES: a xor ident(b and c or a);
205+
206+
a bitor ident(a ? b bitand c : c);
207+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [performance-bool-bitwise-operation]
208+
// CHECK-MESSAGES: :[[@LINE-2]]:25: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [performance-bool-bitwise-operation]
209+
// CHECK-FIXES: a bitor ident(a ? b and c : c);
195210
}
196211

197212
void bad_with_priors_already_braced() {

clang-tools-extra/test/clang-tidy/checkers/performance/bool-bitwise-operation.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,27 @@ void bad_with_priors2() {
186186
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [performance-bool-bitwise-operation]
187187
// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [performance-bool-bitwise-operation]
188188
// CHECK-FIXES: (b && c) || a;
189+
}
190+
191+
template<typename T>
192+
T ident(T val) { return val; }
189193

190-
// case to check `hasAncestor` works as we expected:
194+
// cases to check `hasAncestor` works as we expected:
195+
void bad_has_ancestor() {
196+
bool a = false, b = true, c = true;
191197
bool d = false;
192198
d ^ (a && b & c);
193199
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [performance-bool-bitwise-operation]
194200
// CHECK-FIXES: d ^ (a && b && c);
201+
202+
a ^ ident(b & c || a);
203+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [performance-bool-bitwise-operation]
204+
// CHECK-FIXES: a ^ ident(b && c || a);
205+
206+
a | ident(a ? b & c : c);
207+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [performance-bool-bitwise-operation]
208+
// CHECK-MESSAGES: :[[@LINE-2]]:21: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [performance-bool-bitwise-operation]
209+
// CHECK-FIXES: a | ident(a ? b && c : c);
195210
}
196211

197212
void bad_with_priors_already_braced() {

0 commit comments

Comments
 (0)