Skip to content

Commit ab71f08

Browse files
author
Michael Wright
committed
Fix single_char_pattern crash (#3204)
This commit fixes the crash by removing constant checking from the lint. Closes #3204.
1 parent bc6d85c commit ab71f08

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

clippy_lints/src/methods.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_macro, i
1616
span_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq};
1717
use crate::utils::paths;
1818
use crate::utils::sugg;
19-
use crate::consts::{constant, Constant};
2019
use crate::rustc_errors::Applicability;
2120

2221
#[derive(Clone)]
@@ -1914,8 +1913,11 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &
19141913

19151914
/// lint for length-1 `str`s for methods in `PATTERN_METHODS`
19161915
fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) {
1917-
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
1918-
if r.len() == 1 {
1916+
if_chain! {
1917+
if let hir::ExprKind::Lit(lit) = &arg.node;
1918+
if let ast::LitKind::Str(r, _) = lit.node;
1919+
if r.as_str().len() == 1;
1920+
then {
19191921
let snip = snippet(cx, arg.span, "..");
19201922
let hint = format!("'{}'", &snip[1..snip.len() - 1]);
19211923
span_lint_and_sugg(

tests/ui/single_char_pattern.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ fn main() {
4545

4646
x.replace(";", ",").split(","); // issue #2978
4747
x.starts_with("\x03"); // issue #2996
48+
49+
// Issue #3204
50+
const S: &str = "#";
51+
x.find(S);
4852
}

0 commit comments

Comments
 (0)