rustc_lint/
shadowed_into_iter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use rustc_hir as hir;
use rustc_middle::ty::{self, Ty};
use rustc_session::lint::FutureIncompatibilityReason;
use rustc_session::{declare_lint, impl_lint_pass};
use rustc_span::edition::Edition;

use crate::lints::{ShadowedIntoIterDiag, ShadowedIntoIterDiagSub};
use crate::{LateContext, LateLintPass, LintContext};

declare_lint! {
    /// The `array_into_iter` lint detects calling `into_iter` on arrays.
    ///
    /// ### Example
    ///
    /// ```rust,edition2018
    /// # #![allow(unused)]
    /// [1, 2, 3].into_iter().for_each(|n| { *n; });
    /// ```
    ///
    /// {{produces}}
    ///
    /// ### Explanation
    ///
    /// Since Rust 1.53, arrays implement `IntoIterator`. However, to avoid
    /// breakage, `array.into_iter()` in Rust 2015 and 2018 code will still
    /// behave as `(&array).into_iter()`, returning an iterator over
    /// references, just like in Rust 1.52 and earlier.
    /// This only applies to the method call syntax `array.into_iter()`, not to
    /// any other syntax such as `for _ in array` or `IntoIterator::into_iter(array)`.
    pub ARRAY_INTO_ITER,
    Warn,
    "detects calling `into_iter` on arrays in Rust 2015 and 2018",
    @future_incompatible = FutureIncompatibleInfo {
        reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2021),
        reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>",
    };
}

declare_lint! {
    /// The `boxed_slice_into_iter` lint detects calling `into_iter` on boxed slices.
    ///
    /// ### Example
    ///
    /// ```rust,edition2021
    /// # #![allow(unused)]
    /// vec![1, 2, 3].into_boxed_slice().into_iter().for_each(|n| { *n; });
    /// ```
    ///
    /// {{produces}}
    ///
    /// ### Explanation
    ///
    /// Since Rust 1.80.0, boxed slices implement `IntoIterator`. However, to avoid
    /// breakage, `boxed_slice.into_iter()` in Rust 2015, 2018, and 2021 code will still
    /// behave as `(&boxed_slice).into_iter()`, returning an iterator over
    /// references, just like in Rust 1.79.0 and earlier.
    /// This only applies to the method call syntax `boxed_slice.into_iter()`, not to
    /// any other syntax such as `for _ in boxed_slice` or `IntoIterator::into_iter(boxed_slice)`.
    pub BOXED_SLICE_INTO_ITER,
    Warn,
    "detects calling `into_iter` on boxed slices in Rust 2015, 2018, and 2021",
    @future_incompatible = FutureIncompatibleInfo {
        reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
    };
}

#[derive(Copy, Clone)]
pub(crate) struct ShadowedIntoIter;

impl_lint_pass!(ShadowedIntoIter => [ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER]);

impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
        let hir::ExprKind::MethodCall(call, receiver_arg, ..) = &expr.kind else {
            return;
        };

        // Check if the method call actually calls the libcore
        // `IntoIterator::into_iter`.
        let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) else {
            return;
        };
        if Some(method_def_id) != cx.tcx.lang_items().into_iter_fn() {
            return;
        }

        // As this is a method call expression, we have at least one argument.
        let receiver_ty = cx.typeck_results().expr_ty(receiver_arg);
        let adjustments = cx.typeck_results().expr_adjustments(receiver_arg);

        let adjusted_receiver_tys: Vec<_> =
            [receiver_ty].into_iter().chain(adjustments.iter().map(|adj| adj.target)).collect();

        fn is_ref_to_array(ty: Ty<'_>) -> bool {
            if let ty::Ref(_, pointee_ty, _) = *ty.kind() { pointee_ty.is_array() } else { false }
        }
        fn is_ref_to_boxed_slice(ty: Ty<'_>) -> bool {
            if let ty::Ref(_, pointee_ty, _) = *ty.kind() {
                pointee_ty.boxed_ty().is_some_and(Ty::is_slice)
            } else {
                false
            }
        }

        let (lint, target, edition, can_suggest_ufcs) =
            if is_ref_to_array(*adjusted_receiver_tys.last().unwrap())
                && let Some(idx) = adjusted_receiver_tys
                    .iter()
                    .copied()
                    .take_while(|ty| !is_ref_to_array(*ty))
                    .position(|ty| ty.is_array())
            {
                (ARRAY_INTO_ITER, "[T; N]", "2021", idx == 0)
            } else if is_ref_to_boxed_slice(*adjusted_receiver_tys.last().unwrap())
                && let Some(idx) = adjusted_receiver_tys
                    .iter()
                    .copied()
                    .take_while(|ty| !is_ref_to_boxed_slice(*ty))
                    .position(|ty| ty.boxed_ty().is_some_and(Ty::is_slice))
            {
                (BOXED_SLICE_INTO_ITER, "Box<[T]>", "2024", idx == 0)
            } else {
                return;
            };

        // If this expression comes from the `IntoIter::into_iter` inside of a for loop,
        // we should just suggest removing the `.into_iter()` or changing it to `.iter()`
        // to disambiguate if we want to iterate by-value or by-ref.
        let sub = if let Some((_, hir::Node::Expr(parent_expr))) =
            cx.tcx.hir().parent_iter(expr.hir_id).nth(1)
            && let hir::ExprKind::Match(arg, [_], hir::MatchSource::ForLoopDesugar) =
                &parent_expr.kind
            && let hir::ExprKind::Call(path, [_]) = &arg.kind
            && let hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::IntoIterIntoIter, ..)) =
                &path.kind
        {
            Some(ShadowedIntoIterDiagSub::RemoveIntoIter {
                span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
            })
        } else if can_suggest_ufcs {
            Some(ShadowedIntoIterDiagSub::UseExplicitIntoIter {
                start_span: expr.span.shrink_to_lo(),
                end_span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
            })
        } else {
            None
        };

        cx.emit_span_lint(lint, call.ident.span, ShadowedIntoIterDiag {
            target,
            edition,
            suggestion: call.ident.span,
            sub,
        });
    }
}