2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::method_chain_args;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_lint::Lint;
|
|
|
|
|
|
|
|
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints with `unwrap()`.
|
|
|
|
pub(super) fn check<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
info: &crate::methods::BinaryExprInfo<'_>,
|
|
|
|
chain_methods: &[&str],
|
|
|
|
lint: &'static Lint,
|
|
|
|
suggest: &str,
|
|
|
|
) -> bool {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(args) = method_chain_args(info.chain, chain_methods);
|
|
|
|
if let hir::ExprKind::Lit(ref lit) = info.other.kind;
|
|
|
|
if let ast::LitKind::Char(c) = lit.node;
|
|
|
|
then {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
lint,
|
|
|
|
info.expr.span,
|
2022-10-06 07:44:38 +00:00
|
|
|
&format!("you should use the `{suggest}` method"),
|
2021-03-25 18:29:11 +00:00
|
|
|
"like this",
|
2022-10-06 07:44:38 +00:00
|
|
|
format!("{}{}.{suggest}('{}')",
|
2021-03-25 18:29:11 +00:00
|
|
|
if info.eq { "" } else { "!" },
|
2022-09-01 09:43:35 +00:00
|
|
|
snippet_with_applicability(cx, args[0].0.span, "..", &mut applicability),
|
2022-02-10 17:40:06 +00:00
|
|
|
c.escape_default()),
|
2021-03-25 18:29:11 +00:00
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|