[PATCH] [clang-tidy] Fix for Bug 23161
Szabolcs Sipos
szabolcs.sipos at ericsson.com
Thu Apr 9 12:29:14 PDT 2015
Using built-in method to decide whether the result type is a pointer type.
http://reviews.llvm.org/D8920
Files:
clang-tidy/misc/StaticAssertCheck.cpp
test/clang-tidy/misc-static-assert.cpp
Index: clang-tidy/misc/StaticAssertCheck.cpp
===================================================================
--- clang-tidy/misc/StaticAssertCheck.cpp
+++ clang-tidy/misc/StaticAssertCheck.cpp
@@ -27,15 +27,17 @@
: ClangTidyCheck(Name, Context) {}
void StaticAssertCheck::registerMatchers(MatchFinder *Finder) {
- auto IsAlwaysFalse = ignoringParenImpCasts(
- anyOf(boolLiteral(equals(false)).bind("isAlwaysFalse"),
- integerLiteral(equals(0)).bind("isAlwaysFalse")));
+ auto IsAlwaysFalse = expr(ignoringParenImpCasts(
+ expr(anyOf(boolLiteral(equals(false)), integerLiteral(equals(0)),
+ nullPtrLiteralExpr())).bind("isAlwaysFalse")));
+ auto IsAlwaysFalseWithCast = ignoringParenImpCasts(anyOf(IsAlwaysFalse,
+ cStyleCastExpr(has(IsAlwaysFalse)).bind("castExpr")));
auto AssertExprRoot = anyOf(
binaryOperator(
- hasOperatorName("&&"),
+ anyOf(hasOperatorName("&&"), hasOperatorName("==")),
hasEitherOperand(ignoringImpCasts(stringLiteral().bind("assertMSG"))),
- anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalse)), anything()))
- .bind("assertExprRoot"),
+ anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalseWithCast)),
+ anything())).bind("assertExprRoot"),
IsAlwaysFalse);
auto Condition = expr(anyOf(
expr(ignoringParenCasts(anyOf(
@@ -60,6 +62,7 @@
const auto *AssertMSG = Result.Nodes.getNodeAs<StringLiteral>("assertMSG");
const auto *AssertExprRoot =
Result.Nodes.getNodeAs<BinaryOperator>("assertExprRoot");
+ const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("castExpr");
SourceLocation AssertExpansionLoc = CondStmt->getLocStart();
if (!Opts.CPlusPlus11 || !AssertExpansionLoc.isValid() ||
@@ -75,7 +78,7 @@
return;
// False literal is not the result of macro expansion.
- if (IsAlwaysFalse) {
+ if (IsAlwaysFalse && (!CastExpr || CastExpr->getType()->isPointerType())) {
SourceLocation FalseLiteralLoc =
SM.getImmediateSpellingLoc(IsAlwaysFalse->getExprLoc());
if (!FalseLiteralLoc.isMacroID())
@@ -83,7 +86,8 @@
StringRef FalseMacroName =
Lexer::getImmediateMacroName(FalseLiteralLoc, SM, Opts);
- if (FalseMacroName.compare_lower("false") == 0)
+ if (FalseMacroName.compare_lower("false") == 0 ||
+ FalseMacroName.compare_lower("null") == 0)
return;
}
Index: test/clang-tidy/misc-static-assert.cpp
===================================================================
--- test/clang-tidy/misc-static-assert.cpp
+++ test/clang-tidy/misc-static-assert.cpp
@@ -78,6 +78,25 @@
assert(false && "Don't report me!");
// CHECK-FIXES: {{^ }}assert(false && "Don't report me!");
+#define NULL ((void*)0)
+ assert(NULL && "Don't report me!");
+ // CHECK-FIXES: {{^ }}assert(NULL && "Don't report me!");
+
+ assert(NULL == "Don't report me!");
+ // CHECK-FIXES: {{^ }}assert(NULL == "Don't report me!");
+
+ assert("Don't report me!" == NULL);
+ // CHECK-FIXES: {{^ }}assert("Don't report me!" == NULL);
+
+ assert(0 == "Don't report me!");
+ // CHECK-FIXES: {{^ }}assert(0 == "Don't report me!");
+
+#define NULL ((unsigned int)0)
+ assert(NULL && "Report me!");
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
+ // CHECK-FIXES: {{^ }}static_assert(NULL , "Report me!");
+#undef NULL
+
assert(ZERO_MACRO && "Report me!");
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
// CHECK-FIXES: {{^ }}static_assert(ZERO_MACRO , "Report me!");
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8920.23521.patch
Type: text/x-patch
Size: 3576 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150409/458c1483/attachment.bin>
More information about the cfe-commits
mailing list