[clang-tools-extra] [clang-tidy] Speed up/rewrite `bugprone-stringview-nullptr` (PR #192889)

Victor Chernyakin via cfe-commits cfe-commits at lists.llvm.org
Tue May 5 10:02:34 PDT 2026


================
@@ -1625,14 +1624,29 @@ void constructor_invocation() /* r */ {
   struct AcceptsSV {
     explicit AcceptsSV(std::string_view) {}
   } r1(nullptr);
-  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: passing{{.*}}empty string
+  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: constructing
   // CHECK-FIXES: } r1("");
 
   (void)(AcceptsSV{nullptr}) /* r2 */;
-  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: passing{{.*}}empty string
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: constructing
   // CHECK-FIXES: (void)(AcceptsSV{""}) /* r2 */;
 
   AcceptsSV r3{nullptr};
-  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: passing{{.*}}empty string
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: constructing
   // CHECK-FIXES: AcceptsSV r3{""};
 }
+
+void different_ways_of_spelling_nullptr() {
+  std::string_view sv1 {0};
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: constructing
+  // CHECK-FIXES: std::string_view sv1 {};
+
+  std::string_view sv2 {NULL};
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: constructing
+  // CHECK-FIXES: std::string_view sv2 {};
+
+  const char * const null_char_ptr = nullptr;
+  std::string_view sv3 {null_char_ptr};
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: constructing
+  // CHECK-FIXES: std::string_view sv3 {};
----------------
localspook wrote:

Yep, it does fancy constant folding tricks: https://github.com/llvm/llvm-project/blob/a6f40eed9b4cb1976d5c92be2700bf0931673057/clang/include/clang/AST/Expr.h#L657-L664

https://github.com/llvm/llvm-project/pull/192889


More information about the cfe-commits mailing list