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 rustc_errors::Applicability;
|
2021-04-08 15:50:13 +00:00
|
|
|
use rustc_hir::Expr;
|
2021-02-11 14:04:38 +00:00
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
use super::BYTES_NTH;
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, n_arg: &'tcx Expr<'tcx>) {
|
|
|
|
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
|
|
|
|
let caller_type = if ty.is_str() {
|
|
|
|
"str"
|
2021-10-02 23:51:01 +00:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::String) {
|
2021-04-08 15:50:13 +00:00
|
|
|
"String"
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
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, recv.span, "..", &mut applicability),
|
|
|
|
snippet_with_applicability(cx, n_arg.span, "..", &mut applicability)
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
2021-02-11 14:04:38 +00:00
|
|
|
}
|