2021-03-25 13:29:11 -05: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 08:04:38 -06:00
|
|
|
use rustc_errors::Applicability;
|
2021-04-08 10:50:13 -05:00
|
|
|
use rustc_hir::Expr;
|
2021-02-11 08:04:38 -06:00
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
use super::BYTES_NTH;
|
|
|
|
|
2021-04-08 10:50:13 -05: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 18:51:01 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::String) {
|
2021-04-08 10:50:13 -05:00
|
|
|
"String"
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
BYTES_NTH,
|
|
|
|
expr.span,
|
2022-02-08 05:21:02 -06:00
|
|
|
&format!("called `.bytes().nth()` on a `{}`", caller_type),
|
2021-04-08 10:50:13 -05:00
|
|
|
"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 08:04:38 -06:00
|
|
|
}
|