From 7b4cd17f81e9350ab2d267668e945ccb3c988425 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 18 Jul 2022 02:58:59 +0400 Subject: [PATCH 01/14] Start uplifting `clippy::for_loops_over_fallibles` I refactored the code: - Removed handling of methods, as it felt entirely unnecessary - Removed clippy utils (obviously...) - Used some shiny compiler features (let-else is very handy for lints :eyes:) - I also renamed the lint to `for_loop_over_fallibles` (note: no `s`). I'm not sure what's the naming convention here, so maybe I'm wrong. --- .../rustc_lint/src/for_loop_over_fallibles.rs | 99 +++++++++++++++++++ compiler/rustc_lint/src/lib.rs | 3 + 2 files changed, 102 insertions(+) create mode 100644 compiler/rustc_lint/src/for_loop_over_fallibles.rs diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs new file mode 100644 index 0000000000000..c96c9efe1d8a1 --- /dev/null +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -0,0 +1,99 @@ +use crate::{LateContext, LateLintPass, LintContext}; + +use hir::{Expr, Pat}; +use rustc_hir as hir; +use rustc_middle::ty; +use rustc_span::sym; + +declare_lint! { + /// ### What it does + /// + /// Checks for `for` loops over `Option` or `Result` values. + /// + /// ### Why is this bad? + /// Readability. This is more clearly expressed as an `if + /// let`. + /// + /// ### Example + /// + /// ```rust + /// # let opt = Some(1); + /// # let res: Result = Ok(1); + /// for x in opt { + /// // .. + /// } + /// + /// for x in &res { + /// // .. + /// } + /// + /// for x in res.iter() { + /// // .. + /// } + /// ``` + /// + /// Use instead: + /// ```rust + /// # let opt = Some(1); + /// # let res: Result = Ok(1); + /// if let Some(x) = opt { + /// // .. + /// } + /// + /// if let Ok(x) = res { + /// // .. + /// } + /// ``` + pub FOR_LOOP_OVER_FALLIBLES, + Warn, + "for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`" +} + +declare_lint_pass!(ForLoopOverFallibles => [FOR_LOOP_OVER_FALLIBLES]); + +impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { + let Some((pat, arg)) = extract_for_loop(expr) else { return }; + + let ty = cx.typeck_results().expr_ty(arg); + + let ty::Adt(adt, _) = ty.kind() else { return }; + + let (article, ty, var) = match adt.did() { + did if cx.tcx.is_diagnostic_item(sym::Option, did) => ("an", "Option", "Some"), + did if cx.tcx.is_diagnostic_item(sym::Result, did) => ("a", "Result", "Ok"), + _ => return, + }; + + let Ok(pat_snip) = cx.sess().source_map().span_to_snippet(pat.span) else { return }; + let Ok(arg_snip) = cx.sess().source_map().span_to_snippet(arg.span) else { return }; + + let help_string = format!( + "consider replacing `for {pat_snip} in {arg_snip}` with `if let {var}({pat_snip}) = {arg_snip}`" + ); + let msg = format!( + "for loop over `{arg_snip}`, which is {article} `{ty}`. This is more readably written as an `if let` statement", + ); + + cx.struct_span_lint(FOR_LOOP_OVER_FALLIBLES, arg.span, |diag| { + diag.build(msg).help(help_string).emit() + }) + } +} + +fn extract_for_loop<'tcx>(expr: &Expr<'tcx>) -> Option<(&'tcx Pat<'tcx>, &'tcx Expr<'tcx>)> { + if let hir::ExprKind::DropTemps(e) = expr.kind + && let hir::ExprKind::Match(iterexpr, [arm], hir::MatchSource::ForLoopDesugar) = e.kind + && let hir::ExprKind::Call(_, [arg]) = iterexpr.kind + && let hir::ExprKind::Loop(block, ..) = arm.body.kind + && let [stmt] = block.stmts + && let hir::StmtKind::Expr(e) = stmt.kind + && let hir::ExprKind::Match(_, [_, some_arm], _) = e.kind + && let hir::PatKind::Struct(_, [field], _) = some_arm.pat.kind + { + Some((field.pat, arg)) + } else { + None + } + +} \ No newline at end of file diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index f087c624917e8..d9c5d47cadbf5 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -48,6 +48,7 @@ mod context; mod early; mod enum_intrinsics_non_enums; mod expect; +mod for_loop_over_fallibles; pub mod hidden_unicode_codepoints; mod internal; mod late; @@ -80,6 +81,7 @@ use rustc_span::Span; use array_into_iter::ArrayIntoIter; use builtin::*; use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums; +use for_loop_over_fallibles::*; use hidden_unicode_codepoints::*; use internal::*; use methods::*; @@ -178,6 +180,7 @@ macro_rules! late_lint_mod_passes { $macro!( $args, [ + ForLoopOverFallibles: ForLoopOverFallibles, HardwiredLints: HardwiredLints, ImproperCTypesDeclarations: ImproperCTypesDeclarations, ImproperCTypesDefinitions: ImproperCTypesDefinitions, From 810cf60fddb56056759d1a80b41c7d9dc12ad28d Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 18 Jul 2022 21:08:59 +0400 Subject: [PATCH 02/14] Use structured suggestions for `for_loop_over_fallibles` lint --- .../rustc_lint/src/for_loop_over_fallibles.rs | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index c96c9efe1d8a1..d765131442f6f 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -1,6 +1,7 @@ use crate::{LateContext, LateLintPass, LintContext}; use hir::{Expr, Pat}; +use rustc_errors::Applicability; use rustc_hir as hir; use rustc_middle::ty; use rustc_span::sym; @@ -65,18 +66,24 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { _ => return, }; - let Ok(pat_snip) = cx.sess().source_map().span_to_snippet(pat.span) else { return }; let Ok(arg_snip) = cx.sess().source_map().span_to_snippet(arg.span) else { return }; - let help_string = format!( - "consider replacing `for {pat_snip} in {arg_snip}` with `if let {var}({pat_snip}) = {arg_snip}`" - ); let msg = format!( "for loop over `{arg_snip}`, which is {article} `{ty}`. This is more readably written as an `if let` statement", ); cx.struct_span_lint(FOR_LOOP_OVER_FALLIBLES, arg.span, |diag| { - diag.build(msg).help(help_string).emit() + diag.build(msg) + .multipart_suggestion_verbose( + "consider using `if let` to clear intent", + vec![ + // NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts + (expr.span.with_hi(pat.span.lo()), format!("if let {var}(")), + (pat.span.between(arg.span), format!(") = ")), + ], + Applicability::MachineApplicable, + ) + .emit() }) } } @@ -89,11 +96,10 @@ fn extract_for_loop<'tcx>(expr: &Expr<'tcx>) -> Option<(&'tcx Pat<'tcx>, &'tcx E && let [stmt] = block.stmts && let hir::StmtKind::Expr(e) = stmt.kind && let hir::ExprKind::Match(_, [_, some_arm], _) = e.kind - && let hir::PatKind::Struct(_, [field], _) = some_arm.pat.kind + && let hir::PatKind::Struct(_, [field], _) = some_arm.pat.kind { Some((field.pat, arg)) } else { None } - -} \ No newline at end of file +} From b6611571c4ff709e03df8089401a49808ca583ab Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 24 Jul 2022 21:07:23 +0400 Subject: [PATCH 03/14] `for_loop_over_fallibles`: Suggest removing `.next()` --- .../rustc_lint/src/for_loop_over_fallibles.rs | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index d765131442f6f..78b6ca6a653c0 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -73,17 +73,30 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { ); cx.struct_span_lint(FOR_LOOP_OVER_FALLIBLES, arg.span, |diag| { - diag.build(msg) - .multipart_suggestion_verbose( - "consider using `if let` to clear intent", - vec![ - // NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts - (expr.span.with_hi(pat.span.lo()), format!("if let {var}(")), - (pat.span.between(arg.span), format!(") = ")), - ], - Applicability::MachineApplicable, - ) - .emit() + let mut warn = diag.build(msg); + + if let Some(recv) = extract_iterator_next_call(cx, arg) + && let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span) + { + warn.span_suggestion( + recv.span.between(arg.span.shrink_to_hi()), + format!("to iterate over `{recv_snip}` remove the call to `next`"), + "", + Applicability::MaybeIncorrect + ); + } + + warn.multipart_suggestion_verbose( + "consider using `if let` to clear intent", + vec![ + // NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts + (expr.span.with_hi(pat.span.lo()), format!("if let {var}(")), + (pat.span.between(arg.span), format!(") = ")), + ], + Applicability::MachineApplicable, + ); + + warn.emit() }) } } @@ -103,3 +116,17 @@ fn extract_for_loop<'tcx>(expr: &Expr<'tcx>) -> Option<(&'tcx Pat<'tcx>, &'tcx E None } } + +fn extract_iterator_next_call<'tcx>( + cx: &LateContext<'_>, + expr: &Expr<'tcx>, +) -> Option<&'tcx Expr<'tcx>> { + // This won't work for `Iterator::next(iter)`, is this an issue? + if let hir::ExprKind::MethodCall(_, [recv], _) = expr.kind + && cx.typeck_results().type_dependent_def_id(expr.hir_id) == cx.tcx.lang_items().next_fn() + { + Some(recv) + } else { + return None + } +} From 7cf94ad91568cc1f2e94c0cca4987dc40aa4e452 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 24 Jul 2022 21:16:44 +0400 Subject: [PATCH 04/14] `for_loop_over_fallibles`: suggest `while let` loop --- compiler/rustc_lint/src/for_loop_over_fallibles.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index 78b6ca6a653c0..1a6188827819c 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -84,6 +84,16 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { "", Applicability::MaybeIncorrect ); + } else { + warn.multipart_suggestion_verbose( + format!("to check pattern in a loop use `while let`"), + vec![ + // NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts + (expr.span.with_hi(pat.span.lo()), format!("while let {var}(")), + (pat.span.between(arg.span), format!(") = ")), + ], + Applicability::MaybeIncorrect + ); } warn.multipart_suggestion_verbose( From 14b8f241cb0797032c43c1c85bb0be0f80ba2c0d Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 24 Jul 2022 23:46:04 +0400 Subject: [PATCH 05/14] `for_loop_over_fallibles`: suggest using `?` in some cases --- .../rustc_lint/src/for_loop_over_fallibles.rs | 68 ++++++++++++++++++- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index 1a6188827819c..7807fd3a2807a 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -3,8 +3,11 @@ use crate::{LateContext, LateLintPass, LintContext}; use hir::{Expr, Pat}; use rustc_errors::Applicability; use rustc_hir as hir; -use rustc_middle::ty; -use rustc_span::sym; +use rustc_infer::traits::TraitEngine; +use rustc_infer::{infer::TyCtxtInferExt, traits::ObligationCause}; +use rustc_middle::ty::{self, List}; +use rustc_span::{sym, Span}; +use rustc_trait_selection::traits::TraitEngineExt; declare_lint! { /// ### What it does @@ -58,7 +61,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { let ty = cx.typeck_results().expr_ty(arg); - let ty::Adt(adt, _) = ty.kind() else { return }; + let &ty::Adt(adt, substs) = ty.kind() else { return }; let (article, ty, var) = match adt.did() { did if cx.tcx.is_diagnostic_item(sym::Option, did) => ("an", "Option", "Some"), @@ -96,6 +99,15 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { ); } + if suggest_question_mark(cx, adt, substs, expr.span) { + warn.span_suggestion( + arg.span.shrink_to_hi(), + "consider unwrapping the `Result` with `?` to iterate over its contents", + "?", + Applicability::MaybeIncorrect, + ); + } + warn.multipart_suggestion_verbose( "consider using `if let` to clear intent", vec![ @@ -140,3 +152,53 @@ fn extract_iterator_next_call<'tcx>( return None } } + +fn suggest_question_mark<'tcx>( + cx: &LateContext<'tcx>, + adt: ty::AdtDef<'tcx>, + substs: &List>, + span: Span, +) -> bool { + let Some(body_id) = cx.enclosing_body else { return false }; + let Some(into_iterator_did) = cx.tcx.get_diagnostic_item(sym::IntoIterator) else { return false }; + + if !cx.tcx.is_diagnostic_item(sym::Result, adt.did()) { + return false; + } + + // Check that the function/closure/constant we are in has a `Result` type. + // Otherwise suggesting using `?` may not be a good idea. + { + let ty = cx.typeck_results().expr_ty(&cx.tcx.hir().body(body_id).value); + let ty::Adt(ret_adt, ..) = ty.kind() else { return false }; + if !cx.tcx.is_diagnostic_item(sym::Result, ret_adt.did()) { + return false; + } + } + + let ty = substs.type_at(0); + let is_iterator = cx.tcx.infer_ctxt().enter(|infcx| { + let mut fulfill_cx = >::new(infcx.tcx); + + let cause = ObligationCause::new( + span, + body_id.hir_id, + rustc_infer::traits::ObligationCauseCode::MiscObligation, + ); + fulfill_cx.register_bound( + &infcx, + ty::ParamEnv::empty(), + // Erase any region vids from the type, which may not be resolved + infcx.tcx.erase_regions(ty), + into_iterator_did, + cause, + ); + + // Select all, including ambiguous predicates + let errors = fulfill_cx.select_all_or_error(&infcx); + + errors.is_empty() + }); + + is_iterator +} From 2bf213bb0852e8109253838a403d042b203f807a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 25 Jul 2022 00:17:18 +0400 Subject: [PATCH 06/14] `for_loop_over_fallibles`: remove duplication from the message --- compiler/rustc_lint/src/for_loop_over_fallibles.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index 7807fd3a2807a..69d8fd84b64d5 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -69,10 +69,8 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { _ => return, }; - let Ok(arg_snip) = cx.sess().source_map().span_to_snippet(arg.span) else { return }; - let msg = format!( - "for loop over `{arg_snip}`, which is {article} `{ty}`. This is more readably written as an `if let` statement", + "for loop over {article} `{ty}`. This is more readably written as an `if let` statement", ); cx.struct_span_lint(FOR_LOOP_OVER_FALLIBLES, arg.span, |diag| { From 5128140b63bab45d6e8cba34f5a2231a5bb3d22d Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 25 Jul 2022 00:37:11 +0400 Subject: [PATCH 07/14] Add a test for the `for_loop_over_fallibles` lint --- src/test/ui/lint/for_loop_over_fallibles.rs | 43 ++++++++ .../ui/lint/for_loop_over_fallibles.stderr | 102 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/test/ui/lint/for_loop_over_fallibles.rs create mode 100644 src/test/ui/lint/for_loop_over_fallibles.stderr diff --git a/src/test/ui/lint/for_loop_over_fallibles.rs b/src/test/ui/lint/for_loop_over_fallibles.rs new file mode 100644 index 0000000000000..43d71c2e808a9 --- /dev/null +++ b/src/test/ui/lint/for_loop_over_fallibles.rs @@ -0,0 +1,43 @@ +// check-pass + +fn main() { + // Common + for _ in Some(1) {} + //~^ WARN for loop over an `Option`. This is more readably written as an `if let` statement + //~| HELP to check pattern in a loop use `while let` + //~| HELP consider using `if let` to clear intent + for _ in Ok::<_, ()>(1) {} + //~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement + //~| HELP to check pattern in a loop use `while let` + //~| HELP consider using `if let` to clear intent + + // `Iterator::next` specific + for _ in [0; 0].iter().next() {} + //~^ WARN for loop over an `Option`. This is more readably written as an `if let` statement + //~| HELP to iterate over `[0; 0].iter()` remove the call to `next` + //~| HELP consider using `if let` to clear intent + + // `Result`, but function doesn't return `Result` + for _ in Ok::<_, ()>([0; 0].iter()) {} + //~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement + //~| HELP to check pattern in a loop use `while let` + //~| HELP consider using `if let` to clear intent +} + +fn _returns_result() -> Result<(), ()> { + // `Result` + for _ in Ok::<_, ()>([0; 0].iter()) {} + //~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement + //~| HELP to check pattern in a loop use `while let` + //~| HELP consider unwrapping the `Result` with `?` to iterate over its contents + //~| HELP consider using `if let` to clear intent + + // `Result` + for _ in Ok::<_, ()>([0; 0]) {} + //~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement + //~| HELP to check pattern in a loop use `while let` + //~| HELP consider unwrapping the `Result` with `?` to iterate over its contents + //~| HELP consider using `if let` to clear intent + + Ok(()) +} diff --git a/src/test/ui/lint/for_loop_over_fallibles.stderr b/src/test/ui/lint/for_loop_over_fallibles.stderr new file mode 100644 index 0000000000000..52eac945d8571 --- /dev/null +++ b/src/test/ui/lint/for_loop_over_fallibles.stderr @@ -0,0 +1,102 @@ +warning: for loop over an `Option`. This is more readably written as an `if let` statement + --> $DIR/for_loop_over_fallibles.rs:5:14 + | +LL | for _ in Some(1) {} + | ^^^^^^^ + | + = note: `#[warn(for_loop_over_fallibles)]` on by default +help: to check pattern in a loop use `while let` + | +LL | while let Some(_) = Some(1) {} + | ~~~~~~~~~~~~~~~ ~~~ +help: consider using `if let` to clear intent + | +LL | if let Some(_) = Some(1) {} + | ~~~~~~~~~~~~ ~~~ + +warning: for loop over a `Result`. This is more readably written as an `if let` statement + --> $DIR/for_loop_over_fallibles.rs:9:14 + | +LL | for _ in Ok::<_, ()>(1) {} + | ^^^^^^^^^^^^^^ + | +help: to check pattern in a loop use `while let` + | +LL | while let Ok(_) = Ok::<_, ()>(1) {} + | ~~~~~~~~~~~~~ ~~~ +help: consider using `if let` to clear intent + | +LL | if let Ok(_) = Ok::<_, ()>(1) {} + | ~~~~~~~~~~ ~~~ + +warning: for loop over an `Option`. This is more readably written as an `if let` statement + --> $DIR/for_loop_over_fallibles.rs:15:14 + | +LL | for _ in [0; 0].iter().next() {} + | ^^^^^^^^^^^^^^^^^^^^ + | +help: to iterate over `[0; 0].iter()` remove the call to `next` + | +LL - for _ in [0; 0].iter().next() {} +LL + for _ in [0; 0].iter() {} + | +help: consider using `if let` to clear intent + | +LL | if let Some(_) = [0; 0].iter().next() {} + | ~~~~~~~~~~~~ ~~~ + +warning: for loop over a `Result`. This is more readably written as an `if let` statement + --> $DIR/for_loop_over_fallibles.rs:21:14 + | +LL | for _ in Ok::<_, ()>([0; 0].iter()) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: to check pattern in a loop use `while let` + | +LL | while let Ok(_) = Ok::<_, ()>([0; 0].iter()) {} + | ~~~~~~~~~~~~~ ~~~ +help: consider using `if let` to clear intent + | +LL | if let Ok(_) = Ok::<_, ()>([0; 0].iter()) {} + | ~~~~~~~~~~ ~~~ + +warning: for loop over a `Result`. This is more readably written as an `if let` statement + --> $DIR/for_loop_over_fallibles.rs:29:14 + | +LL | for _ in Ok::<_, ()>([0; 0].iter()) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: to check pattern in a loop use `while let` + | +LL | while let Ok(_) = Ok::<_, ()>([0; 0].iter()) {} + | ~~~~~~~~~~~~~ ~~~ +help: consider unwrapping the `Result` with `?` to iterate over its contents + | +LL | for _ in Ok::<_, ()>([0; 0].iter())? {} + | + +help: consider using `if let` to clear intent + | +LL | if let Ok(_) = Ok::<_, ()>([0; 0].iter()) {} + | ~~~~~~~~~~ ~~~ + +warning: for loop over a `Result`. This is more readably written as an `if let` statement + --> $DIR/for_loop_over_fallibles.rs:36:14 + | +LL | for _ in Ok::<_, ()>([0; 0]) {} + | ^^^^^^^^^^^^^^^^^^^ + | +help: to check pattern in a loop use `while let` + | +LL | while let Ok(_) = Ok::<_, ()>([0; 0]) {} + | ~~~~~~~~~~~~~ ~~~ +help: consider unwrapping the `Result` with `?` to iterate over its contents + | +LL | for _ in Ok::<_, ()>([0; 0])? {} + | + +help: consider using `if let` to clear intent + | +LL | if let Ok(_) = Ok::<_, ()>([0; 0]) {} + | ~~~~~~~~~~ ~~~ + +warning: 6 warnings emitted + From 34815a90dd06af1bdb3504caf59138c2c5a6202f Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 25 Jul 2022 00:58:04 +0400 Subject: [PATCH 08/14] `for_loop_over_fallibles`: fix suggestion for "remove `.next()`" case if the iterator is used after the loop, we need to use `.by_ref()` --- compiler/rustc_lint/src/for_loop_over_fallibles.rs | 2 +- src/test/ui/lint/for_loop_over_fallibles.stderr | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index 69d8fd84b64d5..c8d5586d39f9c 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -82,7 +82,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { warn.span_suggestion( recv.span.between(arg.span.shrink_to_hi()), format!("to iterate over `{recv_snip}` remove the call to `next`"), - "", + ".by_ref()", Applicability::MaybeIncorrect ); } else { diff --git a/src/test/ui/lint/for_loop_over_fallibles.stderr b/src/test/ui/lint/for_loop_over_fallibles.stderr index 52eac945d8571..56e3126dc09a4 100644 --- a/src/test/ui/lint/for_loop_over_fallibles.stderr +++ b/src/test/ui/lint/for_loop_over_fallibles.stderr @@ -37,9 +37,8 @@ LL | for _ in [0; 0].iter().next() {} | help: to iterate over `[0; 0].iter()` remove the call to `next` | -LL - for _ in [0; 0].iter().next() {} -LL + for _ in [0; 0].iter() {} - | +LL | for _ in [0; 0].iter().by_ref() {} + | ~~~~~~~~~ help: consider using `if let` to clear intent | LL | if let Some(_) = [0; 0].iter().next() {} From c4ab59e1921262cb9e07eaeb193192883317af04 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 25 Jul 2022 01:04:27 +0400 Subject: [PATCH 09/14] `for_loop_over_fallibles`: don't use `MachineApplicable` The loop could contain `break;` that won't work with an `if let` --- compiler/rustc_lint/src/for_loop_over_fallibles.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index c8d5586d39f9c..6870942af941f 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -113,7 +113,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles { (expr.span.with_hi(pat.span.lo()), format!("if let {var}(")), (pat.span.between(arg.span), format!(") = ")), ], - Applicability::MachineApplicable, + Applicability::MaybeIncorrect, ); warn.emit() From 41fccb1517db488acd12ba8d962157da88bc4bd4 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 26 Jul 2022 14:17:15 +0400 Subject: [PATCH 10/14] allow or avoid for loops over option in compiler and tests --- compiler/rustc_ast/src/visit.rs | 14 ++++++-------- .../rustc_borrowck/src/type_check/input_output.rs | 2 +- src/test/ui/drop/dropck_legal_cycles.rs | 14 +++++++------- src/test/ui/issues/issue-30371.rs | 1 + 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 4b485b547f495..5820776d04f3d 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -244,14 +244,12 @@ pub trait Visitor<'ast>: Sized { #[macro_export] macro_rules! walk_list { - ($visitor: expr, $method: ident, $list: expr) => { - for elem in $list { - $visitor.$method(elem) - } - }; - ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => { - for elem in $list { - $visitor.$method(elem, $($extra_args,)*) + ($visitor: expr, $method: ident, $list: expr $(, $($extra_args: expr),* )?) => { + { + #[cfg_attr(not(bootstrap), allow(for_loop_over_fallibles))] + for elem in $list { + $visitor.$method(elem $(, $($extra_args,)* )?) + } } } } diff --git a/compiler/rustc_borrowck/src/type_check/input_output.rs b/compiler/rustc_borrowck/src/type_check/input_output.rs index 4431a2e8ec60d..84d2bfe04de05 100644 --- a/compiler/rustc_borrowck/src/type_check/input_output.rs +++ b/compiler/rustc_borrowck/src/type_check/input_output.rs @@ -225,7 +225,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { debug!("{:?} normalized to {:?}", t, norm_ty); - for data in constraints { + if let Some(data) = constraints { ConstraintConversion::new( self.infcx, &self.borrowck_context.universal_regions, diff --git a/src/test/ui/drop/dropck_legal_cycles.rs b/src/test/ui/drop/dropck_legal_cycles.rs index 27a599315dc1c..6a0fe7784fbcc 100644 --- a/src/test/ui/drop/dropck_legal_cycles.rs +++ b/src/test/ui/drop/dropck_legal_cycles.rs @@ -1017,7 +1017,7 @@ impl<'a> Children<'a> for HM<'a> { where C: Context + PrePost, Self: Sized { if let Some(ref hm) = self.contents.get() { - for (k, v) in hm.iter().nth(index / 2) { + if let Some((k, v)) = hm.iter().nth(index / 2) { [k, v][index % 2].descend_into_self(context); } } @@ -1032,7 +1032,7 @@ impl<'a> Children<'a> for VD<'a> { where C: Context + PrePost, Self: Sized { if let Some(ref vd) = self.contents.get() { - for r in vd.iter().nth(index) { + if let Some(r) = vd.iter().nth(index) { r.descend_into_self(context); } } @@ -1047,7 +1047,7 @@ impl<'a> Children<'a> for VM<'a> { where C: Context + PrePost> { if let Some(ref vd) = self.contents.get() { - for (_idx, r) in vd.iter().nth(index) { + if let Some((_idx, r)) = vd.iter().nth(index) { r.descend_into_self(context); } } @@ -1062,7 +1062,7 @@ impl<'a> Children<'a> for LL<'a> { where C: Context + PrePost> { if let Some(ref ll) = self.contents.get() { - for r in ll.iter().nth(index) { + if let Some(r) = ll.iter().nth(index) { r.descend_into_self(context); } } @@ -1077,7 +1077,7 @@ impl<'a> Children<'a> for BH<'a> { where C: Context + PrePost> { if let Some(ref bh) = self.contents.get() { - for r in bh.iter().nth(index) { + if let Some(r) = bh.iter().nth(index) { r.descend_into_self(context); } } @@ -1092,7 +1092,7 @@ impl<'a> Children<'a> for BTM<'a> { where C: Context + PrePost> { if let Some(ref bh) = self.contents.get() { - for (k, v) in bh.iter().nth(index / 2) { + if let Some((k, v)) = bh.iter().nth(index / 2) { [k, v][index % 2].descend_into_self(context); } } @@ -1107,7 +1107,7 @@ impl<'a> Children<'a> for BTS<'a> { where C: Context + PrePost> { if let Some(ref bh) = self.contents.get() { - for r in bh.iter().nth(index) { + if let Some(r) = bh.iter().nth(index) { r.descend_into_self(context); } } diff --git a/src/test/ui/issues/issue-30371.rs b/src/test/ui/issues/issue-30371.rs index a1ae9a36bc1d7..880558eb5b697 100644 --- a/src/test/ui/issues/issue-30371.rs +++ b/src/test/ui/issues/issue-30371.rs @@ -1,5 +1,6 @@ // run-pass #![allow(unreachable_code)] +#![allow(for_loop_over_fallibles)] #![deny(unused_variables)] fn main() { From 86360f41d9f350273d480986526d1c5a15673f1c Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 26 Jul 2022 16:19:58 +0400 Subject: [PATCH 11/14] allow `for_loop_over_fallibles` in a `core` test --- library/core/tests/option.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/tests/option.rs b/library/core/tests/option.rs index 9f5e537dcefc0..84eb4fc0aa329 100644 --- a/library/core/tests/option.rs +++ b/library/core/tests/option.rs @@ -57,6 +57,7 @@ fn test_get_resource() { } #[test] +#[cfg_attr(not(bootstrap), allow(for_loop_over_fallibles))] fn test_option_dance() { let x = Some(()); let mut y = Some(5); From d7b8a65c3aec77b8d6ccf7127cbdfdcf085a6873 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 14 Aug 2022 21:42:29 +0400 Subject: [PATCH 12/14] Edit documentation for `for_loop_over_fallibles` lint --- .../rustc_lint/src/for_loop_over_fallibles.rs | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index 6870942af941f..48a876b157bda 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -10,43 +10,41 @@ use rustc_span::{sym, Span}; use rustc_trait_selection::traits::TraitEngineExt; declare_lint! { - /// ### What it does - /// /// Checks for `for` loops over `Option` or `Result` values. /// - /// ### Why is this bad? - /// Readability. This is more clearly expressed as an `if - /// let`. + /// ### Explanation + /// + /// Both `Option` and `Result` implement `IntoIterator` trait, which allows using them in a `for` loop. + /// `for` loop over `Option` or `Result` will iterate either 0 (if the value is `None`/`Err(_)`) + /// or 1 time (if the value is `Some(_)`/`Ok(_)`). This is not very useful and is more clearly expressed + /// via `if let`. + /// + /// `for` loop can also be accidentally written with the intention to call a function multiple times, + /// while the function returns `Some(_)`, in these cases `while let` loop should be used instead. + /// + /// The "intended" use of `IntoIterator` implementations for `Option` and `Result` is passing them to + /// generic code that expects something implementing `IntoIterator`. For example using `.chain(option)` + /// to optionally add a value to an iterator. /// /// ### Example /// /// ```rust /// # let opt = Some(1); /// # let res: Result = Ok(1); - /// for x in opt { - /// // .. - /// } - /// - /// for x in &res { - /// // .. - /// } - /// - /// for x in res.iter() { - /// // .. - /// } + /// # let recv = || Some(1); + /// for x in opt { /* ... */} + /// for x in res { /* ... */ } + /// for x in recv() { /* ... */ } /// ``` /// /// Use instead: /// ```rust /// # let opt = Some(1); /// # let res: Result = Ok(1); - /// if let Some(x) = opt { - /// // .. - /// } - /// - /// if let Ok(x) = res { - /// // .. - /// } + /// # let recv = || Some(1); + /// if let Some(x) = opt { /* ... */} + /// if let Ok(x) = res { /* ... */ } + /// while let Some(x) = recv() { /* ... */ } /// ``` pub FOR_LOOP_OVER_FALLIBLES, Warn, From aed1ae44eb70f8e7c3a859d3e46d4533b2941066 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 15 Aug 2022 05:22:00 +0400 Subject: [PATCH 13/14] remove an infinite loop --- compiler/rustc_lint/src/for_loop_over_fallibles.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index 48a876b157bda..0a6b6e4163622 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -31,7 +31,7 @@ declare_lint! { /// ```rust /// # let opt = Some(1); /// # let res: Result = Ok(1); - /// # let recv = || Some(1); + /// # let recv = || None::; /// for x in opt { /* ... */} /// for x in res { /* ... */ } /// for x in recv() { /* ... */ } @@ -41,7 +41,7 @@ declare_lint! { /// ```rust /// # let opt = Some(1); /// # let res: Result = Ok(1); - /// # let recv = || Some(1); + /// # let recv = || None::; /// if let Some(x) = opt { /* ... */} /// if let Ok(x) = res { /* ... */ } /// while let Some(x) = recv() { /* ... */ } From 71b8c89a5b467dda27ca29a91a17b8c933d5ce25 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 18 Aug 2022 11:43:10 +0400 Subject: [PATCH 14/14] fix `for_loop_over_fallibles` lint docs --- .../rustc_lint/src/for_loop_over_fallibles.rs | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_lint/src/for_loop_over_fallibles.rs b/compiler/rustc_lint/src/for_loop_over_fallibles.rs index 0a6b6e4163622..2253546b5d357 100644 --- a/compiler/rustc_lint/src/for_loop_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loop_over_fallibles.rs @@ -10,7 +10,16 @@ use rustc_span::{sym, Span}; use rustc_trait_selection::traits::TraitEngineExt; declare_lint! { - /// Checks for `for` loops over `Option` or `Result` values. + /// The `for_loop_over_fallibles` lint checks for `for` loops over `Option` or `Result` values. + /// + /// ### Example + /// + /// ```rust + /// let opt = Some(1); + /// for x in opt { /* ... */} + /// ``` + /// + /// {{produces}} /// /// ### Explanation /// @@ -25,27 +34,6 @@ declare_lint! { /// The "intended" use of `IntoIterator` implementations for `Option` and `Result` is passing them to /// generic code that expects something implementing `IntoIterator`. For example using `.chain(option)` /// to optionally add a value to an iterator. - /// - /// ### Example - /// - /// ```rust - /// # let opt = Some(1); - /// # let res: Result = Ok(1); - /// # let recv = || None::; - /// for x in opt { /* ... */} - /// for x in res { /* ... */ } - /// for x in recv() { /* ... */ } - /// ``` - /// - /// Use instead: - /// ```rust - /// # let opt = Some(1); - /// # let res: Result = Ok(1); - /// # let recv = || None::; - /// if let Some(x) = opt { /* ... */} - /// if let Ok(x) = res { /* ... */ } - /// while let Some(x) = recv() { /* ... */ } - /// ``` pub FOR_LOOP_OVER_FALLIBLES, Warn, "for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`"