]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/single_char_pattern.rs
Auto merge of #6957 - camsteffen:eq-ty-kind, r=flip1995
[rust.git] / clippy_lints / src / methods / single_char_pattern.rs
1 use super::utils::get_hint_if_single_char_arg;
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use if_chain::if_chain;
4 use rustc_errors::Applicability;
5 use rustc_hir as hir;
6 use rustc_lint::LateContext;
7 use rustc_middle::ty;
8 use rustc_span::symbol::Symbol;
9
10 use super::SINGLE_CHAR_PATTERN;
11
12 /// lint for length-1 `str`s for methods in `PATTERN_METHODS`
13 pub(super) fn check(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
14     for &(method, pos) in &crate::methods::PATTERN_METHODS {
15         if_chain! {
16             if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty_adjusted(&args[0]).kind();
17             if *ty.kind() == ty::Str;
18             if method_name.as_str() == method && args.len() > pos;
19             let arg = &args[pos];
20             let mut applicability = Applicability::MachineApplicable;
21             if let Some(hint) = get_hint_if_single_char_arg(cx, arg, &mut applicability);
22             then {
23                 span_lint_and_sugg(
24                     cx,
25                     SINGLE_CHAR_PATTERN,
26                     arg.span,
27                     "single-character string constant used as pattern",
28                     "try using a `char` instead",
29                     hint,
30                     applicability,
31                 );
32             }
33         }
34     }
35 }