Skip to content

Commit 949f584

Browse files
committed
Auto merge of #3929 - KarboniteKream:single-char-pattern, r=oli-obk
Escape a single quote in single_char_pattern hint This PR correctly escapes single quotes in the hint for `single_char_pattern`. For instance, the hint for `x.split("'")` was `'''`, while it should be `'\''`.
2 parents 4fdd113 + 560fd16 commit 949f584

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

clippy_lints/src/methods/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2143,7 +2143,8 @@ fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx h
21432143
then {
21442144
let mut applicability = Applicability::MachineApplicable;
21452145
let snip = snippet_with_applicability(cx, arg.span, "..", &mut applicability);
2146-
let hint = format!("'{}'", &snip[1..snip.len() - 1]);
2146+
let c = &snip[1..snip.len() - 1];
2147+
let hint = format!("'{}'", if c == "'" { "\\'" } else { c });
21472148
span_lint_and_sugg(
21482149
cx,
21492150
SINGLE_CHAR_PATTERN,

tests/ui/single_char_pattern.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ fn main() {
4141
x.trim_end_matches('x');
4242
// Make sure we escape characters correctly.
4343
x.split('\n');
44+
x.split('\'');
45+
x.split('\'');
4446

4547
let h = HashSet::<String>::new();
4648
h.contains("X"); // should not warn

tests/ui/single_char_pattern.rs

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ fn main() {
4141
x.trim_end_matches("x");
4242
// Make sure we escape characters correctly.
4343
x.split("\n");
44+
x.split("'");
45+
x.split("\'");
4446

4547
let h = HashSet::<String>::new();
4648
h.contains("X"); // should not warn

tests/ui/single_char_pattern.stderr

+15-3
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,28 @@ LL | x.split("/n");
109109
| ^^^^ help: try using a char instead: `'/n'`
110110

111111
error: single-character string constant used as pattern
112-
--> $DIR/single_char_pattern.rs:48:31
112+
--> $DIR/single_char_pattern.rs:44:13
113+
|
114+
LL | x.split("'");
115+
| ^^^ help: try using a char instead: `'/''`
116+
117+
error: single-character string constant used as pattern
118+
--> $DIR/single_char_pattern.rs:45:13
119+
|
120+
LL | x.split("/'");
121+
| ^^^^ help: try using a char instead: `'/''`
122+
123+
error: single-character string constant used as pattern
124+
--> $DIR/single_char_pattern.rs:50:31
113125
|
114126
LL | x.replace(";", ",").split(","); // issue #2978
115127
| ^^^ help: try using a char instead: `','`
116128

117129
error: single-character string constant used as pattern
118-
--> $DIR/single_char_pattern.rs:49:19
130+
--> $DIR/single_char_pattern.rs:51:19
119131
|
120132
LL | x.starts_with("/x03"); // issue #2996
121133
| ^^^^^^ help: try using a char instead: `'/x03'`
122134

123-
error: aborting due to 20 previous errors
135+
error: aborting due to 22 previous errors
124136

0 commit comments

Comments
 (0)