From 55672e7e496118ec2c59a4190464b7837d3dcfa4 Mon Sep 17 00:00:00 2001 From: flip1995 <9744647+flip1995@users.noreply.github.com> Date: Fri, 3 Aug 2018 10:19:29 +0200 Subject: [PATCH] Fix single_char_pattern lint for escaped chars --- clippy_lints/src/methods.rs | 5 +---- tests/ui/single_char_pattern.rs | 1 + tests/ui/single_char_pattern.stderr | 8 +++++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index 9a6d3df3088f..bbf1f984d31d 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -1888,11 +1888,8 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: & fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) { if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) { if r.len() == 1 { - let c = r.chars().next().unwrap(); let snip = snippet(cx, arg.span, ".."); - let hint = snip.replace( - &format!("\"{}\"", c.escape_default()), - &format!("'{}'", c.escape_default())); + let hint = format!("'{}'", &snip[1..snip.len() - 1]); span_lint_and_sugg( cx, SINGLE_CHAR_PATTERN, diff --git a/tests/ui/single_char_pattern.rs b/tests/ui/single_char_pattern.rs index 73d008574158..577a0e270906 100644 --- a/tests/ui/single_char_pattern.rs +++ b/tests/ui/single_char_pattern.rs @@ -42,4 +42,5 @@ fn main() { h.contains("X"); // should not warn x.replace(";", ",").split(","); // issue #2978 + x.starts_with("\x03"); // issue #2996 } diff --git a/tests/ui/single_char_pattern.stderr b/tests/ui/single_char_pattern.stderr index 1e7e9cf78cf1..044b4909a373 100644 --- a/tests/ui/single_char_pattern.stderr +++ b/tests/ui/single_char_pattern.stderr @@ -114,5 +114,11 @@ error: single-character string constant used as pattern 44 | x.replace(";", ",").split(","); // issue #2978 | ^^^ help: try using a char instead: `','` -error: aborting due to 19 previous errors +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:45:19 + | +45 | x.starts_with("/x03"); // issue #2996 + | ^^^^^^ help: try using a char instead: `'/x03'` + +error: aborting due to 20 previous errors