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