File tree 3 files changed +25
-7
lines changed
3 files changed +25
-7
lines changed Original file line number Diff line number Diff line change @@ -26,14 +26,20 @@ std::optional<std::string> makeCharacterLiteral(const StringLiteral *Literal) {
26
26
Literal->outputString (OS);
27
27
}
28
28
// Now replace the " with '.
29
- auto Pos = Result.find_first_of (' "' );
30
- if (Pos == Result.npos )
29
+ auto OpenPos = Result.find_first_of (' "' );
30
+ if (OpenPos == Result.npos )
31
31
return std::nullopt;
32
- Result[Pos] = ' \' ' ;
33
- Pos = Result.find_last_of (' "' );
34
- if (Pos == Result.npos )
32
+ Result[OpenPos] = ' \' ' ;
33
+
34
+ auto ClosePos = Result.find_last_of (' "' );
35
+ if (ClosePos == Result.npos )
35
36
return std::nullopt;
36
- Result[Pos] = ' \' ' ;
37
+ Result[ClosePos] = ' \' ' ;
38
+
39
+ // "'" is OK, but ''' is not, so add a backslash
40
+ if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1 ] == ' \' ' )
41
+ Result.replace (OpenPos + 1 , 1 , " \\ '" );
42
+
37
43
return Result;
38
44
}
39
45
Original file line number Diff line number Diff line change @@ -188,6 +188,10 @@ Changes in existing checks
188
188
<clang-tidy/checks/modernize/loop-convert>` to support for-loops with
189
189
iterators initialized by free functions like ``begin ``, ``end ``, or ``size ``.
190
190
191
+ - Improved :doc: `performance-faster-string-find
192
+ <clang-tidy/checks/performance/faster-string-find>` check to properly escape
193
+ single quotes.
194
+
191
195
- Improved :doc: `performanc-noexcept-swap
192
196
<clang-tidy/checks/performance/noexcept-swap>` check to enforce a stricter
193
197
match with the swap function signature, eliminating false-positives.
Original file line number Diff line number Diff line change @@ -56,13 +56,21 @@ void StringFind() {
56
56
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
57
57
// CHECK-FIXES: Str.find('a', 1);
58
58
59
- // Doens 't work with strings smaller or larger than 1 char.
59
+ // Doesn 't work with strings smaller or larger than 1 char.
60
60
Str.find (" " );
61
61
Str.find (" ab" );
62
62
63
63
// Doesn't do anything with the 3 argument overload.
64
64
Str.find (" a" , 1 , 1 );
65
65
66
+ // Single quotes are escaped properly
67
+ Str.find (" '" );
68
+ // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
69
+ // CHECK-FIXES: Str.find('\'');
70
+ Str.find (" \' " );
71
+ // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
72
+ // CHECK-FIXES: Str.find('\'');
73
+
66
74
// Other methods that can also be replaced
67
75
Str.rfind (" a" );
68
76
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: 'rfind' called with a string literal
You can’t perform that action at this time.
0 commit comments