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