Skip to content

Commit

Permalink
Auto merge of #3929 - KarboniteKream:single-char-pattern, r=oli-obk
Browse files Browse the repository at this point in the history
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 `'\''`.
  • Loading branch information
bors committed Apr 8, 2019
2 parents 4fdd113 + 560fd16 commit 949f584
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2143,7 +2143,8 @@ fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx h
then {
let mut applicability = Applicability::MachineApplicable;
let snip = snippet_with_applicability(cx, arg.span, "..", &mut applicability);
let hint = format!("'{}'", &snip[1..snip.len() - 1]);
let c = &snip[1..snip.len() - 1];
let hint = format!("'{}'", if c == "'" { "\\'" } else { c });
span_lint_and_sugg(
cx,
SINGLE_CHAR_PATTERN,
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/single_char_pattern.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ fn main() {
x.trim_end_matches('x');
// Make sure we escape characters correctly.
x.split('\n');
x.split('\'');
x.split('\'');

let h = HashSet::<String>::new();
h.contains("X"); // should not warn
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/single_char_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ fn main() {
x.trim_end_matches("x");
// Make sure we escape characters correctly.
x.split("\n");
x.split("'");
x.split("\'");

let h = HashSet::<String>::new();
h.contains("X"); // should not warn
Expand Down
18 changes: 15 additions & 3 deletions tests/ui/single_char_pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,28 @@ LL | x.split("/n");
| ^^^^ help: try using a char instead: `'/n'`

error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:48:31
--> $DIR/single_char_pattern.rs:44:13
|
LL | x.split("'");
| ^^^ help: try using a char instead: `'/''`

error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:45:13
|
LL | x.split("/'");
| ^^^^ help: try using a char instead: `'/''`

error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:50:31
|
LL | x.replace(";", ",").split(","); // issue #2978
| ^^^ help: try using a char instead: `','`

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

error: aborting due to 20 previous errors
error: aborting due to 22 previous errors

0 comments on commit 949f584

Please sign in to comment.