2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2022-02-10 11:40:06 -06:00
|
|
|
use clippy_utils::{method_chain_args, path_def_id};
|
2021-03-25 13:29:11 -05:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_lint::Lint;
|
2022-02-10 11:40:06 -06:00
|
|
|
use rustc_middle::ty::{self, DefIdTree};
|
2021-03-25 13:29:11 -05:00
|
|
|
|
|
|
|
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints.
|
|
|
|
pub(super) fn check(
|
|
|
|
cx: &LateContext<'_>,
|
|
|
|
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);
|
2022-02-10 11:40:06 -06:00
|
|
|
if let hir::ExprKind::Call(fun, [arg_char]) = info.other.kind;
|
2022-04-25 14:08:45 -05:00
|
|
|
if let Some(id) = path_def_id(cx, fun).map(|ctor_id| cx.tcx.parent(ctor_id));
|
2022-02-10 11:40:06 -06:00
|
|
|
if Some(id) == cx.tcx.lang_items().option_some_variant();
|
2021-03-25 13:29:11 -05:00
|
|
|
then {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2022-09-08 14:27:09 -05:00
|
|
|
let self_ty = cx.typeck_results().expr_ty_adjusted(args[0].0).peel_refs();
|
2021-03-25 13:29:11 -05:00
|
|
|
|
|
|
|
if *self_ty.kind() != ty::Str {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
lint,
|
|
|
|
info.expr.span,
|
|
|
|
&format!("you should use the `{}` method", suggest),
|
|
|
|
"like this",
|
|
|
|
format!("{}{}.{}({})",
|
|
|
|
if info.eq { "" } else { "!" },
|
2022-09-01 04:43:35 -05:00
|
|
|
snippet_with_applicability(cx, args[0].0.span, "..", &mut applicability),
|
2021-03-25 13:29:11 -05:00
|
|
|
suggest,
|
2022-02-10 11:40:06 -06:00
|
|
|
snippet_with_applicability(cx, arg_char.span, "..", &mut applicability)),
|
2021-03-25 13:29:11 -05:00
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|