Author: Nathan James Date: 2021-03-01T18:51:50Z New Revision: 8f9f7d02aaac98f8539158e05975e7acbbb58fcc
URL: https://github.com/llvm/llvm-project/commit/8f9f7d02aaac98f8539158e05975e7acbbb58fcc DIFF: https://github.com/llvm/llvm-project/commit/8f9f7d02aaac98f8539158e05975e7acbbb58fcc.diff LOG: [clang-tidy] Tweak misc-static-assert fix in c++17 If C++17 mode is enabled and the assert doesn't have a string literal, we can emit a static assert with no message in favour of one with an empty message. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D97313 Added: Modified: clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp clang-tools-extra/test/clang-tidy/checkers/misc-static-assert.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp index 2d6504cae689..e9ea69aaeb32 100644 --- a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp @@ -107,17 +107,16 @@ void StaticAssertCheck::check(const MatchFinder::MatchResult &Result) { FixItHints.push_back( FixItHint::CreateReplacement(SourceRange(AssertLoc), "static_assert")); - std::string StaticAssertMSG = ", \"\""; if (AssertExprRoot) { FixItHints.push_back(FixItHint::CreateRemoval( SourceRange(AssertExprRoot->getOperatorLoc()))); FixItHints.push_back(FixItHint::CreateRemoval( SourceRange(AssertMSG->getBeginLoc(), AssertMSG->getEndLoc()))); - StaticAssertMSG = (Twine(", \"") + AssertMSG->getString() + "\"").str(); + FixItHints.push_back(FixItHint::CreateInsertion( + LastParenLoc, (Twine(", \"") + AssertMSG->getString() + "\"").str())); + } else if (!Opts.CPlusPlus17) { + FixItHints.push_back(FixItHint::CreateInsertion(LastParenLoc, ", \"\"")); } - - FixItHints.push_back( - FixItHint::CreateInsertion(LastParenLoc, StaticAssertMSG)); } diag(AssertLoc, "found assert() that could be replaced by static_assert()") diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc-static-assert.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc-static-assert.cpp index 85ae053cffef..d4d083640d16 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/misc-static-assert.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc-static-assert.cpp @@ -1,4 +1,5 @@ -// RUN: %check_clang_tidy %s misc-static-assert %t +// RUN: %check_clang_tidy -std=c++11 -check-suffixes=,CXX11 %s misc-static-assert %t +// RUN: %check_clang_tidy -std=c++17 -check-suffixes=,CXX17 %s misc-static-assert %t void abort() {} #ifdef NDEBUG @@ -37,7 +38,8 @@ class B { template <class T> void doSomething(T t) { assert(myfunc(1, 2)); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert] - // CHECK-FIXES: {{^ }}static_assert(myfunc(1, 2), ""); + // CHECK-FIXES-CXX11: {{^ }}static_assert(myfunc(1, 2), ""); + // CHECK-FIXES-CXX17: {{^ }}static_assert(myfunc(1, 2)); assert(t.method()); // CHECK-FIXES: {{^ }}assert(t.method()); @@ -52,7 +54,8 @@ int main() { assert(myfunc(1, 2) && (3 == 4)); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be - // CHECK-FIXES: {{^ }}static_assert(myfunc(1, 2) && (3 == 4), ""); + // CHECK-FIXES-CXX11: {{^ }}static_assert(myfunc(1, 2) && (3 == 4), ""); + // CHECK-FIXES-CXX17: {{^ }}static_assert(myfunc(1, 2) && (3 == 4)); int x = 1; assert(x == 0); @@ -74,7 +77,8 @@ int main() { assert(ZERO_MACRO); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be - // CHECK-FIXES: {{^ }}static_assert(ZERO_MACRO, ""); + // CHECK-FIXES-CXX11: {{^ }}static_assert(ZERO_MACRO, ""); + // CHECK-FIXES-CXX17: {{^ }}static_assert(ZERO_MACRO); assert(!"Don't report me!"); // CHECK-FIXES: {{^ }}assert(!"Don't report me!"); @@ -136,7 +140,8 @@ int main() { assert(10 == 5 + 5); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be - // CHECK-FIXES: {{^ }}static_assert(10 == 5 + 5, ""); + // CHECK-FIXES-CXX11: {{^ }}static_assert(10 == 5 + 5, ""); + // CHECK-FIXES-CXX17: {{^ }}static_assert(10 == 5 + 5); #undef assert return 0; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits