rust/compiler/rustc_lint/src
Matthias Krüger 4af1c31fcf
Rollup merge of #125156 - zachs18:for_loops_over_fallibles_behind_refs, r=Nilstrieb
Expand `for_loops_over_fallibles` lint to lint on fallibles behind references.

Extends the scope of the (warn-by-default) lint `for_loops_over_fallibles` from just `for _ in x` where `x: Option<_>/Result<_, _>` to also cover `x: &(mut) Option<_>/Result<_>`

```rs
fn main() {
    // Current lints
    for _ in Some(42) {}
    for _ in Ok::<_, i32>(42) {}

    // New lints
    for _ in &Some(42) {}
    for _ in &mut Some(42) {}
    for _ in &Ok::<_, i32>(42) {}
    for _ in &mut Ok::<_, i32>(42) {}

    // Should not lint
    for _ in Some(42).into_iter() {}
    for _ in Some(42).iter() {}
    for _ in Some(42).iter_mut() {}
    for _ in Ok::<_, i32>(42).into_iter() {}
    for _ in Ok::<_, i32>(42).iter() {}
    for _ in Ok::<_, i32>(42).iter_mut() {}
}
```

<details><summary><code>cargo build</code> diff</summary>

```diff
diff --git a/old.out b/new.out
index 84215aa..ca195a7 100644
--- a/old.out
+++ b/new.out
`@@` -1,33 +1,93 `@@`
 warning: for loop over an `Option`. This is more readably written as an `if let` statement
  --> src/main.rs:3:14
   |
 3 |     for _ in Some(42) {}
   |              ^^^^^^^^
   |
   = note: `#[warn(for_loops_over_fallibles)]` on by default
 help: to check pattern in a loop use `while let`
   |
 3 |     while let Some(_) = Some(42) {}
   |     ~~~~~~~~~~~~~~~ ~~~
 help: consider using `if let` to clear intent
   |
 3 |     if let Some(_) = Some(42) {}
   |     ~~~~~~~~~~~~ ~~~

 warning: for loop over a `Result`. This is more readably written as an `if let` statement
  --> src/main.rs:4:14
   |
 4 |     for _ in Ok::<_, i32>(42) {}
   |              ^^^^^^^^^^^^^^^^
   |
 help: to check pattern in a loop use `while let`
   |
 4 |     while let Ok(_) = Ok::<_, i32>(42) {}
   |     ~~~~~~~~~~~~~ ~~~
 help: consider using `if let` to clear intent
   |
 4 |     if let Ok(_) = Ok::<_, i32>(42) {}
   |     ~~~~~~~~~~ ~~~

-warning: `for-loops-over-fallibles` (bin "for-loops-over-fallibles") generated 2 warnings
-    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
+warning: for loop over a `&Option`. This is more readably written as an `if let` statement
+ --> src/main.rs:7:14
+  |
+7 |     for _ in &Some(42) {}
+  |              ^^^^^^^^^
+  |
+help: to check pattern in a loop use `while let`
+  |
+7 |     while let Some(_) = &Some(42) {}
+  |     ~~~~~~~~~~~~~~~ ~~~
+help: consider using `if let` to clear intent
+  |
+7 |     if let Some(_) = &Some(42) {}
+  |     ~~~~~~~~~~~~ ~~~
+
+warning: for loop over a `&mut Option`. This is more readably written as an `if let` statement
+ --> src/main.rs:8:14
+  |
+8 |     for _ in &mut Some(42) {}
+  |              ^^^^^^^^^^^^^
+  |
+help: to check pattern in a loop use `while let`
+  |
+8 |     while let Some(_) = &mut Some(42) {}
+  |     ~~~~~~~~~~~~~~~ ~~~
+help: consider using `if let` to clear intent
+  |
+8 |     if let Some(_) = &mut Some(42) {}
+  |     ~~~~~~~~~~~~ ~~~
+
+warning: for loop over a `&Result`. This is more readably written as an `if let` statement
+ --> src/main.rs:9:14
+  |
+9 |     for _ in &Ok::<_, i32>(42) {}
+  |              ^^^^^^^^^^^^^^^^^
+  |
+help: to check pattern in a loop use `while let`
+  |
+9 |     while let Ok(_) = &Ok::<_, i32>(42) {}
+  |     ~~~~~~~~~~~~~ ~~~
+help: consider using `if let` to clear intent
+  |
+9 |     if let Ok(_) = &Ok::<_, i32>(42) {}
+  |     ~~~~~~~~~~ ~~~
+
+warning: for loop over a `&mut Result`. This is more readably written as an `if let` statement
+  --> src/main.rs:10:14
+   |
+10 |     for _ in &mut Ok::<_, i32>(42) {}
+   |              ^^^^^^^^^^^^^^^^^^^^^
+   |
+help: to check pattern in a loop use `while let`
+   |
+10 |     while let Ok(_) = &mut Ok::<_, i32>(42) {}
+   |     ~~~~~~~~~~~~~ ~~~
+help: consider using `if let` to clear intent
+   |
+10 |     if let Ok(_) = &mut Ok::<_, i32>(42) {}
+   |     ~~~~~~~~~~ ~~~
+
+warning: `for-loops-over-fallibles` (bin "for-loops-over-fallibles") generated 6 warnings
+    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s

```

</details>

-----

Question:

* ~~Currently, the article `an` is used for `&Option`, and `&mut Option` in the lint diagnostic, since that's what `Option` uses. Is this okay or should it be changed? (likewise, `a` is used for `&Result` and `&mut Result`)~~ The article `a` is used for `&Option`, `&mut Option`, `&Result`, `&mut Result` and (as before) `Result`. Only `Option` uses `an` (as before).

`@rustbot` label +A-lint
2024-05-23 07:41:17 +02:00
..
context Make early lints translatable 2024-05-21 20:16:39 +00:00
nonstandard_style mv compiler to compiler/ 2020-08-30 18:45:07 +03:00
async_fn_in_trait.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
builtin.rs Convert NAMED_ASM_LABELS lint to diag struct 2024-05-21 20:11:42 +00:00
context.rs Make early lints translatable 2024-05-21 20:16:39 +00:00
deref_into_dyn_supertrait.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
drop_forget_useless.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
early.rs Make early lints translatable 2024-05-21 20:16:39 +00:00
enum_intrinsics_non_enums.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
errors.rs Pass translation closure to add_to_diag_with() as reference 2024-04-21 07:45:03 +00:00
expect.rs Rename TyCtxt::emit_spanned_lint as TyCtxt::emit_node_span_lint. 2024-01-23 08:09:05 +11:00
for_loops_over_fallibles.rs Use 'a' article for &Option. 2024-05-15 12:47:20 -05:00
foreign_modules.rs Rename Unsafe to Safety 2024-05-17 18:33:37 -03:00
hidden_unicode_codepoints.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
impl_trait_overcaptures.rs And finally add tests 2024-05-13 23:57:56 -04:00
internal.rs Allow multiple impl Into<{D,Subd}iagMessage> parameters in a function. 2024-03-11 16:42:10 +11:00
invalid_from_utf8.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
late.rs Avoid lots of hir::HirId{,Map,Set} qualifiers. 2024-04-16 16:29:15 +10:00
let_underscore.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
levels.rs Remove extern crate rustc_middle from numerous crates. 2024-04-29 14:50:45 +10:00
lib.rs Adjust the method ambiguity lint too 2024-05-20 19:21:38 -04:00
lints.rs Rollup merge of #125156 - zachs18:for_loops_over_fallibles_behind_refs, r=Nilstrieb 2024-05-23 07:41:17 +02:00
map_unit_fn.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
methods.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
multiple_supertrait_upcastable.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
non_ascii_idents.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
non_fmt_panic.rs Remove extern crate rustc_middle from numerous crates. 2024-04-29 14:50:45 +10:00
non_local_def.rs Fix typos (taking into account review comments) 2024-05-18 18:12:18 +02:00
nonstandard_style.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
noop_method_call.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
opaque_hidden_inferred_bound.rs split out AliasTy -> AliasTerm 2024-05-13 11:59:42 -04:00
pass_by_value.rs Auto merge of #124401 - oli-obk:some_hir_cleanups, r=cjgillot 2024-05-04 00:32:27 +00:00
passes.rs Rename {enter,exit}_lint_attrs to check_attributes{,_post} 2024-03-24 14:57:57 +00:00
ptr_nulls.rs Rename LintContext::emit_spanned_lint as LintContext::emit_span_lint. 2024-01-23 08:08:25 +11:00
redundant_semicolon.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
reference_casting.rs Handle Deref expressions in invalid_reference_casting 2024-05-10 12:33:07 -04:00
shadowed_into_iter.rs Implement BOXED_SLICE_INTO_ITER 2024-05-20 19:21:30 -04:00
tests.rs refactor(rustc_lint): inline check_lint_name_cmdline 2023-08-30 17:35:07 +01:00
traits.rs Remove extern crate rustc_session from rustc_lint. 2024-04-29 13:57:41 +10:00
types.rs Auto merge of #124747 - MasterAwesome:master, r=davidtwco 2024-05-06 00:55:49 +00:00
unit_bindings.rs Inline & delete Ty::new_unit, since it's just a field access 2024-05-02 17:49:23 +02:00
unused.rs Document the situation with unused_parens lint and braced macro calls 2024-05-11 15:49:03 -07:00