From ab71f0866323bac7255da8687a2850e1daddf816 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Sun, 23 Sep 2018 15:25:10 +0200 Subject: [PATCH] Fix single_char_pattern crash (#3204) This commit fixes the crash by removing constant checking from the lint. Closes #3204. --- clippy_lints/src/methods.rs | 8 +++++--- tests/ui/single_char_pattern.rs | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index 03e3d2628e8..54df9809a3e 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -16,7 +16,6 @@ use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_macro, i span_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq}; use crate::utils::paths; use crate::utils::sugg; -use crate::consts::{constant, Constant}; use crate::rustc_errors::Applicability; #[derive(Clone)] @@ -1914,8 +1913,11 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: & /// lint for length-1 `str`s for methods in `PATTERN_METHODS` 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 { + if_chain! { + if let hir::ExprKind::Lit(lit) = &arg.node; + if let ast::LitKind::Str(r, _) = lit.node; + if r.as_str().len() == 1; + then { let snip = snippet(cx, arg.span, ".."); let hint = format!("'{}'", &snip[1..snip.len() - 1]); span_lint_and_sugg( diff --git a/tests/ui/single_char_pattern.rs b/tests/ui/single_char_pattern.rs index 147f974b999..c4e88e9ee2b 100644 --- a/tests/ui/single_char_pattern.rs +++ b/tests/ui/single_char_pattern.rs @@ -45,4 +45,8 @@ fn main() { x.replace(";", ",").split(","); // issue #2978 x.starts_with("\x03"); // issue #2996 + + // Issue #3204 + const S: &str = "#"; + x.find(S); }