]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/chars_cmp_with_unwrap.rs
Merge commit 'f4850f7292efa33759b4f7f9b7621268979e9914' into clippyup
[rust.git] / clippy_lints / src / methods / chars_cmp_with_unwrap.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::method_chain_args;
3 use clippy_utils::source::snippet_with_applicability;
4 use if_chain::if_chain;
5 use rustc_ast::ast;
6 use rustc_errors::Applicability;
7 use rustc_hir as hir;
8 use rustc_lint::LateContext;
9 use rustc_lint::Lint;
10
11 /// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints with `unwrap()`.
12 pub(super) fn check(
13     cx: &LateContext<'_>,
14     info: &crate::methods::BinaryExprInfo<'_>,
15     chain_methods: &[&str],
16     lint: &'static Lint,
17     suggest: &str,
18 ) -> bool {
19     if_chain! {
20         if let Some(args) = method_chain_args(info.chain, chain_methods);
21         if let hir::ExprKind::Lit(ref lit) = info.other.kind;
22         if let ast::LitKind::Char(c) = lit.node;
23         then {
24             let mut applicability = Applicability::MachineApplicable;
25             span_lint_and_sugg(
26                 cx,
27                 lint,
28                 info.expr.span,
29                 &format!("you should use the `{suggest}` method"),
30                 "like this",
31                 format!("{}{}.{suggest}('{}')",
32                         if info.eq { "" } else { "!" },
33                         snippet_with_applicability(cx, args[0].0.span, "..", &mut applicability),
34                         c.escape_default()),
35                 applicability,
36             );
37
38             true
39         } else {
40             false
41         }
42     }
43 }