summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/methods/get_first.rs
blob: 945bbf53bcf358edaeb84ce8d32e3dc22285308a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_slice_of_primitives;
use clippy_utils::source::snippet_with_applicability;
use if_chain::if_chain;
use rustc_ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::source_map::Spanned;

use super::GET_FIRST;

pub(super) fn check<'tcx>(
    cx: &LateContext<'tcx>,
    expr: &'tcx hir::Expr<'_>,
    recv: &'tcx hir::Expr<'_>,
    arg: &'tcx hir::Expr<'_>,
) {
    if_chain! {
        if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
        if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
        if cx.tcx.type_of(impl_id).subst_identity().is_slice();
        if let Some(_) = is_slice_of_primitives(cx, recv);
        if let hir::ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = arg.kind;
        then {
            let mut app = Applicability::MachineApplicable;
            let slice_name = snippet_with_applicability(cx, recv.span, "..", &mut app);
            span_lint_and_sugg(
                cx,
                GET_FIRST,
                expr.span,
                &format!("accessing first element with `{slice_name}.get(0)`"),
                "try",
                format!("{slice_name}.first()"),
                app,
            );
        }
    }
}