]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 XL |
1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
2 | use clippy_utils::source::snippet_with_applicability; | |
3 | use clippy_utils::ty::is_type_diagnostic_item; | |
f20569fa | 4 | use rustc_errors::Applicability; |
cdc7bbd5 | 5 | use rustc_hir::Expr; |
f20569fa XL |
6 | use rustc_lint::LateContext; |
7 | use rustc_span::sym; | |
8 | ||
9 | use super::BYTES_NTH; | |
10 | ||
cdc7bbd5 XL |
11 | pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, n_arg: &'tcx Expr<'tcx>) { |
12 | let ty = cx.typeck_results().expr_ty(recv).peel_refs(); | |
13 | let caller_type = if ty.is_str() { | |
14 | "str" | |
c295e0f8 | 15 | } else if is_type_diagnostic_item(cx, ty, sym::String) { |
cdc7bbd5 XL |
16 | "String" |
17 | } else { | |
18 | return; | |
19 | }; | |
20 | let mut applicability = Applicability::MachineApplicable; | |
21 | span_lint_and_sugg( | |
22 | cx, | |
23 | BYTES_NTH, | |
24 | expr.span, | |
04454e1e | 25 | &format!("called `.bytes().nth()` on a `{}`", caller_type), |
cdc7bbd5 XL |
26 | "try", |
27 | format!( | |
28 | "{}.as_bytes().get({})", | |
29 | snippet_with_applicability(cx, recv.span, "..", &mut applicability), | |
30 | snippet_with_applicability(cx, n_arg.span, "..", &mut applicability) | |
31 | ), | |
32 | applicability, | |
33 | ); | |
f20569fa | 34 | } |