[clang-tools-extra] r301743 - [clang-tidy] Expand AllowConditional*Casts to binary logical operators
Alexander Kornienko via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 29 05:06:47 PDT 2017
Author: alexfh
Date: Sat Apr 29 07:06:45 2017
New Revision: 301743
URL: http://llvm.org/viewvc/llvm-project?rev=301743&view=rev
Log:
[clang-tidy] Expand AllowConditional*Casts to binary logical operators
Modified:
clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast-allow-conditional-casts.cpp
Modified: clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp?rev=301743&r1=301742&r2=301743&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp Sat Apr 29 07:06:45 2017
@@ -11,6 +11,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
+#include <queue>
using namespace clang::ast_matchers;
@@ -281,20 +282,29 @@ void addFixItHintsForLiteralCastFromBool
Context)));
}
-StatementMatcher createConditionalExpressionMatcher() {
- return stmt(anyOf(ifStmt(), conditionalOperator(),
- parenExpr(hasParent(conditionalOperator()))));
-}
-
bool isAllowedConditionalCast(const ImplicitCastExpr *CastExpression,
ASTContext &Context) {
- auto AllowedConditionalMatcher = stmt(hasParent(stmt(
- anyOf(createConditionalExpressionMatcher(),
- unaryOperator(hasOperatorName("!"),
- hasParent(createConditionalExpressionMatcher()))))));
-
- auto MatchResult = match(AllowedConditionalMatcher, *CastExpression, Context);
- return !MatchResult.empty();
+ std::queue<const Stmt *> Q;
+ Q.push(CastExpression);
+ while (!Q.empty()) {
+ for (const auto &N : Context.getParents(*Q.front())) {
+ const Stmt *S = N.get<Stmt>();
+ if (!S)
+ return false;
+ if (isa<IfStmt>(S) || isa<ConditionalOperator>(S))
+ return true;
+ if (isa<ParenExpr>(S) || isa<ImplicitCastExpr>(S) ||
+ (isa<UnaryOperator>(S) &&
+ cast<UnaryOperator>(S)->getOpcode() == UO_LNot) ||
+ (isa<BinaryOperator>(S) && cast<BinaryOperator>(S)->isLogicalOp())) {
+ Q.push(S);
+ } else {
+ return false;
+ }
+ }
+ Q.pop();
+ }
+ return false;
}
} // anonymous namespace
Modified: clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast-allow-conditional-casts.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast-allow-conditional-casts.cpp?rev=301743&r1=301742&r2=301743&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast-allow-conditional-casts.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-cast-allow-conditional-casts.cpp Sat Apr 29 07:06:45 2017
@@ -25,8 +25,11 @@ void regularImplicitCastIntegerToBoolIsN
void implicitCastIntegerToBoolInConditionalsIsAllowed() {
if (functionReturningInt()) {}
if (!functionReturningInt()) {}
+ if (functionReturningInt() && functionReturningPointer()) {}
+ if (!functionReturningInt() && !functionReturningPointer()) {}
int value1 = functionReturningInt() ? 1 : 2;
- int value2 = ! functionReturningInt() ? 1 : 2;
+ int value2 = !functionReturningInt() ? 1 : 2;
+ int value3 = (functionReturningInt() && functionReturningPointer() || !functionReturningInt()) ? 1 : 2;
}
void regularImplicitCastPointerToBoolIsNotIgnored() {
More information about the cfe-commits
mailing list