Skip to main content

rustc_lint/
for_loops_over_fallibles.rs

1use hir::{Expr, Pat};
2use rustc_hir::{self as hir, LangItem};
3use rustc_infer::infer::TyCtxtInferExt;
4use rustc_infer::traits::ObligationCause;
5use rustc_middle::ty;
6use rustc_session::{declare_lint, declare_lint_pass};
7use rustc_span::{Span, sym};
8use rustc_trait_selection::traits::ObligationCtxt;
9
10use crate::lints::{
11    ForLoopsOverFalliblesDiag, ForLoopsOverFalliblesLoopSub, ForLoopsOverFalliblesQuestionMark,
12    ForLoopsOverFalliblesSuggestion,
13};
14use crate::{LateContext, LateLintPass, LintContext};
15
16#[doc =
r" The `for_loops_over_fallibles` lint checks for `for` loops over `Option` or `Result` values."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" let opt = Some(1);"]
#[doc = r" for x in opt { /* ... */}"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" Both `Option` and `Result` implement `IntoIterator` trait, which allows using them in a `for` loop."]
#[doc =
r" `for` loop over `Option` or `Result` will iterate either 0 (if the value is `None`/`Err(_)`)"]
#[doc =
r" or 1 time (if the value is `Some(_)`/`Ok(_)`). This is not very useful and is more clearly expressed"]
#[doc = r" via `if let`."]
#[doc = r""]
#[doc =
r" `for` loop can also be accidentally written with the intention to call a function multiple times,"]
#[doc =
r" while the function returns `Some(_)`, in these cases `while let` loop should be used instead."]
#[doc = r""]
#[doc =
r#" The "intended" use of `IntoIterator` implementations for `Option` and `Result` is passing them to"#]
#[doc =
r" generic code that expects something implementing `IntoIterator`. For example using `.chain(option)`"]
#[doc = r" to optionally add a value to an iterator."]
pub static FOR_LOOPS_OVER_FALLIBLES: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "FOR_LOOPS_OVER_FALLIBLES",
            default_level: ::rustc_lint_defs::Warn,
            desc: "for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
17    /// The `for_loops_over_fallibles` lint checks for `for` loops over `Option` or `Result` values.
18    ///
19    /// ### Example
20    ///
21    /// ```rust
22    /// let opt = Some(1);
23    /// for x in opt { /* ... */}
24    /// ```
25    ///
26    /// {{produces}}
27    ///
28    /// ### Explanation
29    ///
30    /// Both `Option` and `Result` implement `IntoIterator` trait, which allows using them in a `for` loop.
31    /// `for` loop over `Option` or `Result` will iterate either 0 (if the value is `None`/`Err(_)`)
32    /// or 1 time (if the value is `Some(_)`/`Ok(_)`). This is not very useful and is more clearly expressed
33    /// via `if let`.
34    ///
35    /// `for` loop can also be accidentally written with the intention to call a function multiple times,
36    /// while the function returns `Some(_)`, in these cases `while let` loop should be used instead.
37    ///
38    /// The "intended" use of `IntoIterator` implementations for `Option` and `Result` is passing them to
39    /// generic code that expects something implementing `IntoIterator`. For example using `.chain(option)`
40    /// to optionally add a value to an iterator.
41    pub FOR_LOOPS_OVER_FALLIBLES,
42    Warn,
43    "for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`"
44}
45
46pub struct ForLoopsOverFallibles;
#[automatically_derived]
impl ::core::marker::Copy for ForLoopsOverFallibles { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for ForLoopsOverFallibles { }
#[automatically_derived]
impl ::core::clone::Clone for ForLoopsOverFallibles {
    #[inline]
    fn clone(&self) -> ForLoopsOverFallibles { *self }
}
impl ::rustc_lint_defs::LintPass for ForLoopsOverFallibles {
    fn name(&self) -> &'static str { "ForLoopsOverFallibles" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [FOR_LOOPS_OVER_FALLIBLES]))
    }
}
impl ForLoopsOverFallibles {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [FOR_LOOPS_OVER_FALLIBLES]))
    }
}declare_lint_pass!(ForLoopsOverFallibles => [FOR_LOOPS_OVER_FALLIBLES]);
47
48impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
49    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
50        let Some((pat, arg)) = extract_for_loop(expr) else { return };
51
52        // Do not put suggestions for external macros.
53        if pat.span.from_expansion() {
54            return;
55        }
56
57        let arg_span = arg.span.source_callsite();
58
59        let ty = cx.typeck_results().expr_ty(arg);
60
61        let (adt, args, ref_mutability) = match ty.kind() {
62            &ty::Adt(adt, args) => (adt, args, None),
63            &ty::Ref(_, ty, mutability) => match ty.kind() {
64                &ty::Adt(adt, args) => (adt, args, Some(mutability)),
65                _ => return,
66            },
67            _ => return,
68        };
69
70        let (article, ty, var) = match adt.did() {
71            did if cx.tcx.is_diagnostic_item(sym::Option, did) && ref_mutability.is_some() => {
72                ("a", "Option", "Some")
73            }
74            did if cx.tcx.is_diagnostic_item(sym::Option, did) => ("an", "Option", "Some"),
75            did if cx.tcx.is_diagnostic_item(sym::Result, did) => ("a", "Result", "Ok"),
76            _ => return,
77        };
78
79        let ref_prefix = match ref_mutability {
80            None => "",
81            Some(ref_mutability) => ref_mutability.ref_prefix_str(),
82        };
83
84        let sub = if let Some(recv) = extract_iterator_next_call(cx, arg)
85            && recv.span.can_be_used_for_suggestions()
86            && recv.span.between(arg_span.shrink_to_hi()).can_be_used_for_suggestions()
87            && let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
88        {
89            ForLoopsOverFalliblesLoopSub::RemoveNext {
90                suggestion: recv.span.between(arg_span.shrink_to_hi()),
91                recv_snip,
92            }
93        } else {
94            ForLoopsOverFalliblesLoopSub::UseWhileLet {
95                start_span: expr.span.with_hi(pat.span.lo()),
96                end_span: pat.span.between(arg_span),
97                var,
98            }
99        };
100        let question_mark = suggest_question_mark(cx, adt, args, expr.span)
101            .then(|| ForLoopsOverFalliblesQuestionMark { suggestion: arg_span.shrink_to_hi() });
102        let suggestion = ForLoopsOverFalliblesSuggestion {
103            var,
104            start_span: expr.span.with_hi(pat.span.lo()),
105            end_span: pat.span.between(arg_span),
106        };
107
108        cx.emit_span_lint(
109            FOR_LOOPS_OVER_FALLIBLES,
110            arg_span,
111            ForLoopsOverFalliblesDiag { article, ref_prefix, ty, sub, question_mark, suggestion },
112        );
113    }
114}
115
116fn extract_for_loop<'tcx>(expr: &Expr<'tcx>) -> Option<(&'tcx Pat<'tcx>, &'tcx Expr<'tcx>)> {
117    if let hir::ExprKind::DropTemps(e) = expr.kind
118        && let hir::ExprKind::Match(iterexpr, [arm], hir::MatchSource::ForLoopDesugar) = e.kind
119        && let hir::ExprKind::Call(_, [arg]) = iterexpr.kind
120        && let hir::ExprKind::Loop(block, ..) = arm.body.kind
121        && let [stmt] = block.stmts
122        && let hir::StmtKind::Expr(e) = stmt.kind
123        && let hir::ExprKind::Match(_, [_, some_arm], _) = e.kind
124        && let hir::PatKind::Struct(_, [field], _) = some_arm.pat.kind
125    {
126        Some((field.pat, arg))
127    } else {
128        None
129    }
130}
131
132fn extract_iterator_next_call<'tcx>(
133    cx: &LateContext<'_>,
134    expr: &Expr<'tcx>,
135) -> Option<&'tcx Expr<'tcx>> {
136    // This won't work for `Iterator::next(iter)`, is this an issue?
137    if let hir::ExprKind::MethodCall(_, recv, _, _) = expr.kind
138        && cx
139            .typeck_results()
140            .type_dependent_def_id(expr.hir_id)
141            .is_some_and(|def_id| cx.tcx.is_lang_item(def_id, LangItem::IteratorNext))
142    {
143        Some(recv)
144    } else {
145        None
146    }
147}
148
149fn suggest_question_mark<'tcx>(
150    cx: &LateContext<'tcx>,
151    adt: ty::AdtDef<'tcx>,
152    args: ty::GenericArgsRef<'tcx>,
153    span: Span,
154) -> bool {
155    let Some(body_id) = cx.enclosing_body else { return false };
156    let Some(into_iterator_did) = cx.tcx.get_diagnostic_item(sym::IntoIterator) else {
157        return false;
158    };
159
160    if !cx.tcx.is_diagnostic_item(sym::Result, adt.did()) {
161        return false;
162    }
163
164    // Check that the function/closure/constant we are in has a `Result` type.
165    // Otherwise suggesting using `?` may not be a good idea.
166    {
167        let ty = cx.typeck_results().expr_ty(cx.tcx.hir_body(body_id).value);
168        let ty::Adt(ret_adt, ..) = ty.kind() else { return false };
169        if !cx.tcx.is_diagnostic_item(sym::Result, ret_adt.did()) {
170            return false;
171        }
172    }
173
174    let ty = args.type_at(0);
175    let (infcx, param_env) = cx.tcx.infer_ctxt().build_with_typing_env(cx.typing_env());
176    let ocx = ObligationCtxt::new(&infcx);
177
178    let body_def_id = cx.tcx.hir_body_owner_def_id(body_id);
179    let cause =
180        ObligationCause::new(span, body_def_id, rustc_infer::traits::ObligationCauseCode::Misc);
181
182    ocx.register_bound(
183        cause,
184        param_env,
185        // Erase any region vids from the type, which may not be resolved
186        infcx.tcx.erase_and_anonymize_regions(ty),
187        into_iterator_did,
188    );
189
190    ocx.evaluate_obligations_error_on_ambiguity().is_empty()
191}