2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-02-11 14:04:38 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{Expr, ExprKind};
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
use super::BYTES_NTH;
|
|
|
|
|
2021-03-12 14:30:50 +00:00
|
|
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>]) {
|
2021-02-11 14:04:38 +00:00
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::MethodCall(_, _, ref args, _) = expr.kind;
|
|
|
|
let ty = cx.typeck_results().expr_ty(&iter_args[0]).peel_refs();
|
|
|
|
let caller_type = if is_type_diagnostic_item(cx, ty, sym::string_type) {
|
|
|
|
Some("String")
|
|
|
|
} else if ty.is_str() {
|
|
|
|
Some("str")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some(caller_type) = caller_type;
|
|
|
|
then {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
BYTES_NTH,
|
|
|
|
expr.span,
|
|
|
|
&format!("called `.byte().nth()` on a `{}`", caller_type),
|
|
|
|
"try",
|
|
|
|
format!(
|
|
|
|
"{}.as_bytes().get({})",
|
|
|
|
snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability),
|
|
|
|
snippet_with_applicability(cx, args[1].span, "..", &mut applicability)
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|