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