1 use crate::{LateContext, LateLintPass, LintContext};
4 use rustc_errors::{Applicability, DelayDm};
6 use rustc_infer::{infer::TyCtxtInferExt, traits::ObligationCause};
7 use rustc_middle::ty::{self, List};
8 use rustc_span::{sym, Span};
11 /// The `for_loops_over_fallibles` lint checks for `for` loops over `Option` or `Result` values.
16 /// let opt = Some(1);
17 /// for x in opt { /* ... */}
24 /// Both `Option` and `Result` implement `IntoIterator` trait, which allows using them in a `for` loop.
25 /// `for` loop over `Option` or `Result` will iterate either 0 (if the value is `None`/`Err(_)`)
26 /// or 1 time (if the value is `Some(_)`/`Ok(_)`). This is not very useful and is more clearly expressed
29 /// `for` loop can also be accidentally written with the intention to call a function multiple times,
30 /// while the function returns `Some(_)`, in these cases `while let` loop should be used instead.
32 /// The "intended" use of `IntoIterator` implementations for `Option` and `Result` is passing them to
33 /// generic code that expects something implementing `IntoIterator`. For example using `.chain(option)`
34 /// to optionally add a value to an iterator.
35 pub FOR_LOOPS_OVER_FALLIBLES,
37 "for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`"
40 declare_lint_pass!(ForLoopsOverFallibles => [FOR_LOOPS_OVER_FALLIBLES]);
42 impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
43 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
44 let Some((pat, arg)) = extract_for_loop(expr) else { return };
46 let ty = cx.typeck_results().expr_ty(arg);
48 let &ty::Adt(adt, substs) = ty.kind() else { return };
50 let (article, ty, var) = match adt.did() {
51 did if cx.tcx.is_diagnostic_item(sym::Option, did) => ("an", "Option", "Some"),
52 did if cx.tcx.is_diagnostic_item(sym::Result, did) => ("a", "Result", "Ok"),
56 let msg = DelayDm(|| {
58 "for loop over {article} `{ty}`. This is more readably written as an `if let` statement",
62 cx.struct_span_lint(FOR_LOOPS_OVER_FALLIBLES, arg.span, msg, |lint| {
63 if let Some(recv) = extract_iterator_next_call(cx, arg)
64 && let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
67 recv.span.between(arg.span.shrink_to_hi()),
68 format!("to iterate over `{recv_snip}` remove the call to `next`"),
70 Applicability::MaybeIncorrect
73 lint.multipart_suggestion_verbose(
74 "to check pattern in a loop use `while let`",
76 // NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts
77 (expr.span.with_hi(pat.span.lo()), format!("while let {var}(")),
78 (pat.span.between(arg.span), ") = ".to_string()),
80 Applicability::MaybeIncorrect
84 if suggest_question_mark(cx, adt, substs, expr.span) {
86 arg.span.shrink_to_hi(),
87 "consider unwrapping the `Result` with `?` to iterate over its contents",
89 Applicability::MaybeIncorrect,
93 lint.multipart_suggestion_verbose(
94 "consider using `if let` to clear intent",
96 // NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts
97 (expr.span.with_hi(pat.span.lo()), format!("if let {var}(")),
98 (pat.span.between(arg.span), ") = ".to_string()),
100 Applicability::MaybeIncorrect,
106 fn extract_for_loop<'tcx>(expr: &Expr<'tcx>) -> Option<(&'tcx Pat<'tcx>, &'tcx Expr<'tcx>)> {
107 if let hir::ExprKind::DropTemps(e) = expr.kind
108 && let hir::ExprKind::Match(iterexpr, [arm], hir::MatchSource::ForLoopDesugar) = e.kind
109 && let hir::ExprKind::Call(_, [arg]) = iterexpr.kind
110 && let hir::ExprKind::Loop(block, ..) = arm.body.kind
111 && let [stmt] = block.stmts
112 && let hir::StmtKind::Expr(e) = stmt.kind
113 && let hir::ExprKind::Match(_, [_, some_arm], _) = e.kind
114 && let hir::PatKind::Struct(_, [field], _) = some_arm.pat.kind
116 Some((field.pat, arg))
122 fn extract_iterator_next_call<'tcx>(
123 cx: &LateContext<'_>,
125 ) -> Option<&'tcx Expr<'tcx>> {
126 // This won't work for `Iterator::next(iter)`, is this an issue?
127 if let hir::ExprKind::MethodCall(_, recv, _, _) = expr.kind
128 && cx.typeck_results().type_dependent_def_id(expr.hir_id) == cx.tcx.lang_items().next_fn()
136 fn suggest_question_mark<'tcx>(
137 cx: &LateContext<'tcx>,
138 adt: ty::AdtDef<'tcx>,
139 substs: &List<ty::GenericArg<'tcx>>,
142 let Some(body_id) = cx.enclosing_body else { return false };
143 let Some(into_iterator_did) = cx.tcx.get_diagnostic_item(sym::IntoIterator) else { return false };
145 if !cx.tcx.is_diagnostic_item(sym::Result, adt.did()) {
149 // Check that the function/closure/constant we are in has a `Result` type.
150 // Otherwise suggesting using `?` may not be a good idea.
152 let ty = cx.typeck_results().expr_ty(&cx.tcx.hir().body(body_id).value);
153 let ty::Adt(ret_adt, ..) = ty.kind() else { return false };
154 if !cx.tcx.is_diagnostic_item(sym::Result, ret_adt.did()) {
159 let ty = substs.type_at(0);
160 let infcx = cx.tcx.infer_ctxt().build();
161 let cause = ObligationCause::new(
164 rustc_infer::traits::ObligationCauseCode::MiscObligation,
166 let errors = rustc_trait_selection::traits::fully_solve_bound(
169 ty::ParamEnv::empty(),
170 // Erase any region vids from the type, which may not be resolved
171 infcx.tcx.erase_regions(ty),