Skip to content

Commit 8f9f7d0

Browse files
committed
[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
1 parent 18adbb8 commit 8f9f7d0

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,16 @@ void StaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
107107
FixItHints.push_back(
108108
FixItHint::CreateReplacement(SourceRange(AssertLoc), "static_assert"));
109109

110-
std::string StaticAssertMSG = ", \"\"";
111110
if (AssertExprRoot) {
112111
FixItHints.push_back(FixItHint::CreateRemoval(
113112
SourceRange(AssertExprRoot->getOperatorLoc())));
114113
FixItHints.push_back(FixItHint::CreateRemoval(
115114
SourceRange(AssertMSG->getBeginLoc(), AssertMSG->getEndLoc())));
116-
StaticAssertMSG = (Twine(", \"") + AssertMSG->getString() + "\"").str();
115+
FixItHints.push_back(FixItHint::CreateInsertion(
116+
LastParenLoc, (Twine(", \"") + AssertMSG->getString() + "\"").str()));
117+
} else if (!Opts.CPlusPlus17) {
118+
FixItHints.push_back(FixItHint::CreateInsertion(LastParenLoc, ", \"\""));
117119
}
118-
119-
FixItHints.push_back(
120-
FixItHint::CreateInsertion(LastParenLoc, StaticAssertMSG));
121120
}
122121

123122
diag(AssertLoc, "found assert() that could be replaced by static_assert()")

clang-tools-extra/test/clang-tidy/checkers/misc-static-assert.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %check_clang_tidy %s misc-static-assert %t
1+
// RUN: %check_clang_tidy -std=c++11 -check-suffixes=,CXX11 %s misc-static-assert %t
2+
// RUN: %check_clang_tidy -std=c++17 -check-suffixes=,CXX17 %s misc-static-assert %t
23

34
void abort() {}
45
#ifdef NDEBUG
@@ -37,7 +38,8 @@ class B {
3738
template <class T> void doSomething(T t) {
3839
assert(myfunc(1, 2));
3940
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
40-
// CHECK-FIXES: {{^ }}static_assert(myfunc(1, 2), "");
41+
// CHECK-FIXES-CXX11: {{^ }}static_assert(myfunc(1, 2), "");
42+
// CHECK-FIXES-CXX17: {{^ }}static_assert(myfunc(1, 2));
4143

4244
assert(t.method());
4345
// CHECK-FIXES: {{^ }}assert(t.method());
@@ -52,7 +54,8 @@ int main() {
5254

5355
assert(myfunc(1, 2) && (3 == 4));
5456
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
55-
// CHECK-FIXES: {{^ }}static_assert(myfunc(1, 2) && (3 == 4), "");
57+
// CHECK-FIXES-CXX11: {{^ }}static_assert(myfunc(1, 2) && (3 == 4), "");
58+
// CHECK-FIXES-CXX17: {{^ }}static_assert(myfunc(1, 2) && (3 == 4));
5659

5760
int x = 1;
5861
assert(x == 0);
@@ -74,7 +77,8 @@ int main() {
7477

7578
assert(ZERO_MACRO);
7679
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
77-
// CHECK-FIXES: {{^ }}static_assert(ZERO_MACRO, "");
80+
// CHECK-FIXES-CXX11: {{^ }}static_assert(ZERO_MACRO, "");
81+
// CHECK-FIXES-CXX17: {{^ }}static_assert(ZERO_MACRO);
7882

7983
assert(!"Don't report me!");
8084
// CHECK-FIXES: {{^ }}assert(!"Don't report me!");
@@ -136,7 +140,8 @@ int main() {
136140

137141
assert(10 == 5 + 5);
138142
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
139-
// CHECK-FIXES: {{^ }}static_assert(10 == 5 + 5, "");
143+
// CHECK-FIXES-CXX11: {{^ }}static_assert(10 == 5 + 5, "");
144+
// CHECK-FIXES-CXX17: {{^ }}static_assert(10 == 5 + 5);
140145
#undef assert
141146

142147
return 0;

0 commit comments

Comments
 (0)