]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/array_into_iter.rs
b6075d165dd05a4cfebdfd458f188a08b4693ce7
[rust.git] / src / librustc_lint / array_into_iter.rs
1 use crate::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
2 use rustc::{
3     hir,
4     lint::FutureIncompatibleInfo,
5     ty::{
6         self,
7         adjustment::{Adjust, Adjustment},
8     },
9 };
10 use syntax::{errors::Applicability, symbol::sym};
11
12 declare_lint! {
13     pub ARRAY_INTO_ITER,
14     Warn,
15     "detects calling `into_iter` on arrays",
16     @future_incompatible = FutureIncompatibleInfo {
17         reference: "issue #66145 <https://github.com/rust-lang/rust/issues/66145>",
18         edition: None,
19     };
20 }
21
22 declare_lint_pass!(
23     /// Checks for instances of calling `into_iter` on arrays.
24     ArrayIntoIter => [ARRAY_INTO_ITER]
25 );
26
27 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIntoIter {
28     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
29         // We only care about method call expressions.
30         if let hir::ExprKind::MethodCall(call, span, args) = &expr.kind {
31             if call.ident.name != sym::into_iter {
32                 return;
33             }
34
35             // Check if the method call actually calls the libcore
36             // `IntoIterator::into_iter`.
37             let def_id = cx.tables.type_dependent_def_id(expr.hir_id).unwrap();
38             match cx.tcx.trait_of_item(def_id) {
39                 Some(trait_id) if cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_id) => {}
40                 _ => return,
41             };
42
43             // As this is a method call expression, we have at least one
44             // argument.
45             let receiver_arg = &args[0];
46
47             // Peel all `Box<_>` layers. We have to special case `Box` here as
48             // `Box` is the only thing that values can be moved out of via
49             // method call. `Box::new([1]).into_iter()` should trigger this
50             // lint.
51             let mut recv_ty = cx.tables.expr_ty(receiver_arg);
52             let mut num_box_derefs = 0;
53             while recv_ty.is_box() {
54                 num_box_derefs += 1;
55                 recv_ty = recv_ty.boxed_ty();
56             }
57
58             // Make sure we found an array after peeling the boxes.
59             if !matches!(recv_ty.kind, ty::Array(..)) {
60                 return;
61             }
62
63             // Make sure that there is an autoref coercion at the expected
64             // position. The first `num_box_derefs` adjustments are the derefs
65             // of the box.
66             match cx.tables.expr_adjustments(receiver_arg).get(num_box_derefs) {
67                 Some(Adjustment { kind: Adjust::Borrow(_), .. }) => {}
68                 _ => return,
69             }
70
71             // Emit lint diagnostic.
72             let target = match cx.tables.expr_ty_adjusted(receiver_arg).kind {
73                 ty::Ref(_, ty::TyS { kind: ty::Array(..), .. }, _) => "[T; N]",
74                 ty::Ref(_, ty::TyS { kind: ty::Slice(..), .. }, _) => "[T]",
75
76                 // We know the original first argument type is an array type,
77                 // we know that the first adjustment was an autoref coercion
78                 // and we know that `IntoIterator` is the trait involved. The
79                 // array cannot be coerced to something other than a reference
80                 // to an array or to a slice.
81                 _ => bug!("array type coerced to something other than array or slice"),
82             };
83             let msg = format!(
84                 "this method call currently resolves to `<&{} as IntoIterator>::into_iter` (due \
85                     to autoref coercions), but that might change in the future when \
86                     `IntoIterator` impls for arrays are added.",
87                 target,
88             );
89             cx.struct_span_lint(ARRAY_INTO_ITER, *span, &msg)
90                 .span_suggestion(
91                     call.ident.span,
92                     "use `.iter()` instead of `.into_iter()` to avoid ambiguity",
93                     "iter".into(),
94                     Applicability::MachineApplicable,
95                 )
96                 .emit();
97         }
98     }
99 }