rust/src/tools
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
..
build-manifest Use generic NonZero. 2024-05-08 21:37:55 +02:00
build_helper use shared stage0 parser from build_helper 2024-05-11 20:48:41 +03:00
bump-stage0 move comments position in src/stage0 2024-05-11 20:49:01 +03:00
cargo@84dc5dc11a Update cargo 2024-05-21 18:53:54 -04:00
cargotest Don't ask for a specific branch in cargotest 2023-12-04 12:11:44 +02:00
clippy Merge commit '2efebd2f0c' into clippy-subtree-update 2024-05-21 10:39:30 -07:00
collect-license-metadata
compiletest Rollup merge of #125156 - zachs18:for_loops_over_fallibles_behind_refs, r=Nilstrieb 2024-05-23 07:41:17 +02:00
coverage-dump Add MC/DC support to coverage test tools 2024-04-08 21:30:03 +10:00
error_index_generator Stop using String for error codes. 2024-01-29 07:41:41 +11:00
generate-copyright
generate-windows-sys Add support for Arm64EC to the Standard Library 2024-04-15 16:05:16 -07:00
html-checker rustdoc: allow custom element rustdoc-search 2024-05-06 21:50:27 -07:00
jsondocck Remove direct dependencies on lazy_static, once_cell and byteorder 2024-04-28 14:35:00 +01:00
jsondoclint Fix typos (taking into account review comments) 2024-05-18 18:12:18 +02:00
linkchecker Remove direct dependencies on lazy_static, once_cell and byteorder 2024-04-28 14:35:00 +01:00
lint-docs Deny gen keyword in edition_2024_compat lints 2024-04-22 11:51:50 -04:00
lld-wrapper Fix typos (taking into account review comments) 2024-05-18 18:12:18 +02:00
llvm-bitcode-linker Distribute LLVM bitcode linker as a preview component 2024-04-15 15:11:28 +02:00
miri Rollup merge of #124896 - RalfJung:miri-intrinsic-fallback, r=oli-obk 2024-05-22 19:04:43 +02:00
miropt-test-tools tidy: wrap regexes with lazy_static 2024-02-17 12:29:05 +03:00
opt-dist refactor(opt-dist): use rustc-perf from rustc checkout 2024-05-20 15:01:10 +00:00
remote-test-client use u64 to represent file size 2024-01-21 18:16:22 +03:00
remote-test-server use u64 to represent file size 2024-01-21 18:16:22 +03:00
replace-version-placeholder
rls
run-make-support rewrite issue64319 and rename 2024-05-17 11:49:20 -04:00
rust-analyzer Bump rustc crates 2024-05-19 11:22:58 +03:00
rust-demangler
rust-installer Sort directories when generating tarballs 2024-03-31 12:56:05 +02:00
rustbook dedupe env_logger, drop is-terminal 2024-02-22 14:35:21 +03:00
rustc-perf@4f313add60 refactor: add rustc-perf submodule to src/tools 2024-05-20 14:56:49 +00:00
rustdoc Change SIGPIPE ui from #[unix_sigpipe = "..."] to -Zon-broken-pipe=... 2024-05-02 19:48:29 +02:00
rustdoc-gui
rustdoc-gui-test
rustdoc-js rustdoc-search: compressed bitmap to sort, then load desc 2024-03-21 17:57:01 -07:00
rustdoc-themes
rustfmt Rename Unsafe to Safety 2024-05-17 18:33:37 -03:00
suggest-tests Remove direct dependencies on lazy_static, once_cell and byteorder 2024-04-28 14:35:00 +01:00
tidy Rollup merge of #125357 - GuillaumeGomez:migrate-rustdoc-scrape-examples-multiple, r=jieyouxu 2024-05-21 20:28:49 +02:00
tier-check
unicode-table-generator Add a lower bound check to unicode-table-generator output 2024-04-20 10:16:45 +02:00
unstable-book-gen
x
cherry-pick.sh
publish_toolstate.py CI: fix toolstate publishing 2024-05-15 15:48:52 +02:00