Skip to content

Commit f6c4e30

Browse files
authored
Merge pull request #2999 from flip1995/single_char_pattern
Fix single_char_pattern lint for escaped chars
2 parents 32e4897 + 55672e7 commit f6c4e30

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

clippy_lints/src/methods.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,11 +1899,8 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &
18991899
fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) {
19001900
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
19011901
if r.len() == 1 {
1902-
let c = r.chars().next().unwrap();
19031902
let snip = snippet(cx, arg.span, "..");
1904-
let hint = snip.replace(
1905-
&format!("\"{}\"", c.escape_default()),
1906-
&format!("'{}'", c.escape_default()));
1903+
let hint = format!("'{}'", &snip[1..snip.len() - 1]);
19071904
span_lint_and_sugg(
19081905
cx,
19091906
SINGLE_CHAR_PATTERN,

tests/ui/single_char_pattern.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ fn main() {
4242
h.contains("X"); // should not warn
4343

4444
x.replace(";", ",").split(","); // issue #2978
45+
x.starts_with("\x03"); // issue #2996
4546
}

tests/ui/single_char_pattern.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,11 @@ error: single-character string constant used as pattern
114114
44 | x.replace(";", ",").split(","); // issue #2978
115115
| ^^^ help: try using a char instead: `','`
116116

117-
error: aborting due to 19 previous errors
117+
error: single-character string constant used as pattern
118+
--> $DIR/single_char_pattern.rs:45:19
119+
|
120+
45 | x.starts_with("/x03"); // issue #2996
121+
| ^^^^^^ help: try using a char instead: `'/x03'`
122+
123+
error: aborting due to 20 previous errors
118124

0 commit comments

Comments
 (0)