[PATCH] D97313: [clang-tidy] Tweak misc-static-assert fix in c++17

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 1 10:52:04 PST 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rG8f9f7d02aaac: [clang-tidy] Tweak misc-static-assert fix in c++17 (authored by njames93).

Changed prior to commit:
  https://reviews.llvm.org/D97313?vs=325855&id=327186#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D97313/new/

https://reviews.llvm.org/D97313

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


Index: clang-tools-extra/test/clang-tidy/checkers/misc-static-assert.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/misc-static-assert.cpp
+++ 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 @@
 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 @@
 
   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 @@
 
   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 @@
 
   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;
Index: clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp
@@ -107,17 +107,16 @@
     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()")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97313.327186.patch
Type: text/x-patch
Size: 3359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210301/a1fd6a3f/attachment-0001.bin>


More information about the cfe-commits mailing list