rust-clippy/clippy_lints/src/methods/bytes_nth.rs

42 lines
1.4 KiB
Rust
Raw Normal View History

use crate::utils::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
2021-03-13 23:01:03 +00:00
use clippy_utils::ty::is_type_diagnostic_item;
2021-02-07 16:34:59 +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;
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>]) {
2021-02-07 16:34:59 +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",
2021-02-07 16:34:59 +00:00
format!(
"{}.as_bytes().get({})",
snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability),
snippet_with_applicability(cx, args[1].span, "..", &mut applicability)
),
applicability,
);
}
}
}