]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/loops/single_element_loop.rs
Remove a span from hir::ExprKind::MethodCall
[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::single_segment_path;
4 use clippy_utils::source::{indent_of, snippet};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::{BorrowKind, Expr, ExprKind, Pat, PatKind};
8 use rustc_lint::LateContext;
9
10 pub(super) fn check<'tcx>(
11     cx: &LateContext<'tcx>,
12     pat: &'tcx Pat<'_>,
13     arg: &'tcx Expr<'_>,
14     body: &'tcx Expr<'_>,
15     expr: &'tcx Expr<'_>,
16 ) {
17     let arg_expr = match arg.kind {
18         ExprKind::AddrOf(BorrowKind::Ref, _, ref_arg) => ref_arg,
19         ExprKind::MethodCall(method, args, _) if args.len() == 1 && method.ident.name == rustc_span::sym::iter => {
20             &args[0]
21         },
22         _ => return,
23     };
24     if_chain! {
25         if let PatKind::Binding(.., target, _) = pat.kind;
26         if let ExprKind::Array([arg_expression]) = arg_expr.kind;
27         if let ExprKind::Path(ref list_item) = arg_expression.kind;
28         if let Some(list_item_name) = single_segment_path(list_item).map(|ps| ps.ident.name);
29         if let ExprKind::Block(block, _) = body.kind;
30         if !block.stmts.is_empty();
31
32         then {
33             let mut block_str = snippet(cx, block.span, "..").into_owned();
34             block_str.remove(0);
35             block_str.pop();
36
37
38             span_lint_and_sugg(
39                 cx,
40                 SINGLE_ELEMENT_LOOP,
41                 expr.span,
42                 "for loop over a single element",
43                 "try",
44                 format!("{{\n{}let {} = &{};{}}}", " ".repeat(indent_of(cx, block.stmts[0].span).unwrap_or(0)), target.name, list_item_name, block_str),
45                 Applicability::MachineApplicable
46             )
47         }
48     }
49 }