[PATCH] D157436: [clang-tidy] `performance-faster-string-find` generates incorrect fixes for single quote character literals

Fabian Wolff via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 8 16:00:18 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG5b95d17da1f0: [clang-tidy] `performance-faster-string-find` generates incorrect fixes for… (authored by fwolff).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D157436

Files:
  clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp
@@ -56,13 +56,21 @@
   // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
   // CHECK-FIXES: Str.find('a', 1);
 
-  // Doens't work with strings smaller or larger than 1 char.
+  // Doesn't work with strings smaller or larger than 1 char.
   Str.find("");
   Str.find("ab");
 
   // Doesn't do anything with the 3 argument overload.
   Str.find("a", 1, 1);
 
+  // Single quotes are escaped properly
+  Str.find("'");
+  // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
+  // CHECK-FIXES: Str.find('\'');
+  Str.find("\'");
+  // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
+  // CHECK-FIXES: Str.find('\'');
+
   // Other methods that can also be replaced
   Str.rfind("a");
   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: 'rfind' called with a string literal
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -188,6 +188,10 @@
   <clang-tidy/checks/modernize/loop-convert>` to support for-loops with
   iterators initialized by free functions like ``begin``, ``end``, or ``size``.
 
+- Improved :doc:`performance-faster-string-find
+  <clang-tidy/checks/performance/faster-string-find>` check to properly escape
+  single quotes.
+
 - Improved :doc:`performanc-noexcept-swap
   <clang-tidy/checks/performance/noexcept-swap>` check to enforce a stricter
   match with the swap function signature, eliminating false-positives.
Index: clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
+++ clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
@@ -26,14 +26,20 @@
     Literal->outputString(OS);
   }
   // Now replace the " with '.
-  auto Pos = Result.find_first_of('"');
-  if (Pos == Result.npos)
+  auto OpenPos = Result.find_first_of('"');
+  if (OpenPos == Result.npos)
     return std::nullopt;
-  Result[Pos] = '\'';
-  Pos = Result.find_last_of('"');
-  if (Pos == Result.npos)
+  Result[OpenPos] = '\'';
+
+  auto ClosePos = Result.find_last_of('"');
+  if (ClosePos == Result.npos)
     return std::nullopt;
-  Result[Pos] = '\'';
+  Result[ClosePos] = '\'';
+
+  // "'" is OK, but ''' is not, so add a backslash
+  if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] == '\'')
+    Result.replace(OpenPos + 1, 1, "\\'");
+
   return Result;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157436.548387.patch
Type: text/x-patch
Size: 2891 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230808/92ed0d0b/attachment.bin>


More information about the cfe-commits mailing list