]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/bytes_nth.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / bytes_nth.rs
CommitLineData
f20569fa
XL
1use crate::utils::{is_type_diagnostic_item, snippet_with_applicability, span_lint_and_sugg};
2use if_chain::if_chain;
3use rustc_errors::Applicability;
4use rustc_hir::{Expr, ExprKind};
5use rustc_lint::LateContext;
6use rustc_span::sym;
7
8use super::BYTES_NTH;
9
10pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>]) {
11 if_chain! {
12 if let ExprKind::MethodCall(_, _, ref args, _) = expr.kind;
13 let ty = cx.typeck_results().expr_ty(&iter_args[0]).peel_refs();
14 let caller_type = if is_type_diagnostic_item(cx, ty, sym::string_type) {
15 Some("String")
16 } else if ty.is_str() {
17 Some("str")
18 } else {
19 None
20 };
21 if let Some(caller_type) = caller_type;
22 then {
23 let mut applicability = Applicability::MachineApplicable;
24 span_lint_and_sugg(
25 cx,
26 BYTES_NTH,
27 expr.span,
28 &format!("called `.byte().nth()` on a `{}`", caller_type),
29 "try",
30 format!(
31 "{}.as_bytes().get({})",
32 snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability),
33 snippet_with_applicability(cx, args[1].span, "..", &mut applicability)
34 ),
35 applicability,
36 );
37 }
38 }
39}