]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/loops/single_element_loop.rs
clippy: BindingAnnotation change
[rust.git] / clippy_lints / src / loops / single_element_loop.rs
1 use super::SINGLE_ELEMENT_LOOP;
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::source::{indent_of, snippet_with_applicability};
4 use if_chain::if_chain;
5 use rustc_ast::util::parser::PREC_PREFIX;
6 use rustc_ast::Mutability;
7 use rustc_errors::Applicability;
8 use rustc_hir::{is_range_literal, BorrowKind, Expr, ExprKind, Pat};
9 use rustc_lint::LateContext;
10 use rustc_span::edition::Edition;
11
12 pub(super) fn check<'tcx>(
13     cx: &LateContext<'tcx>,
14     pat: &'tcx Pat<'_>,
15     arg: &'tcx Expr<'_>,
16     body: &'tcx Expr<'_>,
17     expr: &'tcx Expr<'_>,
18 ) {
19     let (arg_expression, prefix) = match arg.kind {
20         ExprKind::AddrOf(
21             BorrowKind::Ref,
22             Mutability::Not,
23             Expr {
24                 kind: ExprKind::Array([arg]),
25                 ..
26             },
27         ) => (arg, "&"),
28         ExprKind::AddrOf(
29             BorrowKind::Ref,
30             Mutability::Mut,
31             Expr {
32                 kind: ExprKind::Array([arg]),
33                 ..
34             },
35         ) => (arg, "&mut "),
36         ExprKind::MethodCall(
37             method,
38             [
39                 Expr {
40                     kind: ExprKind::Array([arg]),
41                     ..
42                 },
43             ],
44             _,
45         ) if method.ident.name == rustc_span::sym::iter => (arg, "&"),
46         ExprKind::MethodCall(
47             method,
48             [
49                 Expr {
50                     kind: ExprKind::Array([arg]),
51                     ..
52                 },
53             ],
54             _,
55         ) if method.ident.name.as_str() == "iter_mut" => (arg, "&mut "),
56         ExprKind::MethodCall(
57             method,
58             [
59                 Expr {
60                     kind: ExprKind::Array([arg]),
61                     ..
62                 },
63             ],
64             _,
65         ) if method.ident.name == rustc_span::sym::into_iter => (arg, ""),
66         // Only check for arrays edition 2021 or later, as this case will trigger a compiler error otherwise.
67         ExprKind::Array([arg]) if cx.tcx.sess.edition() >= Edition::Edition2021 => (arg, ""),
68         _ => return,
69     };
70     if_chain! {
71         if let ExprKind::Block(block, _) = body.kind;
72         if !block.stmts.is_empty();
73         then {
74             let mut applicability = Applicability::MachineApplicable;
75             let pat_snip = snippet_with_applicability(cx, pat.span, "..", &mut applicability);
76             let mut arg_snip = snippet_with_applicability(cx, arg_expression.span, "..", &mut applicability);
77             let mut block_str = snippet_with_applicability(cx, block.span, "..", &mut applicability).into_owned();
78             block_str.remove(0);
79             block_str.pop();
80             let indent = " ".repeat(indent_of(cx, block.stmts[0].span).unwrap_or(0));
81
82             // Reference iterator from `&(mut) []` or `[].iter(_mut)()`.
83             if !prefix.is_empty() && (
84                 // Precedence of internal expression is less than or equal to precedence of `&expr`.
85                 arg_expression.precedence().order() <= PREC_PREFIX || is_range_literal(arg_expression)
86             ) {
87                 arg_snip = format!("({arg_snip})").into();
88             }
89
90             span_lint_and_sugg(
91                 cx,
92                 SINGLE_ELEMENT_LOOP,
93                 expr.span,
94                 "for loop over a single element",
95                 "try",
96                 format!("{{\n{indent}let {pat_snip} = {prefix}{arg_snip};{block_str}}}"),
97                 applicability,
98             )
99         }
100     }
101 }