Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nuance to suggestion of !Copy expressions in for loops #82916

Open
estebank opened this issue Mar 8, 2021 · 1 comment
Open

Add nuance to suggestion of !Copy expressions in for loops #82916

estebank opened this issue Mar 8, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-iterators Area: Iterators A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Mar 8, 2021

Given

struct S(i32);
fn foo(x: Vec<S>) {
    for y in x {
        
    }
    let z = x;
}

we currently suggest borrowing x:

error[E0382]: use of moved value: `x`
   --> src/lib.rs:6:13
    |
2   | fn foo(x: Vec<S>) {
    |        - move occurs because `x` has type `Vec<S>`, which does not implement the `Copy` trait
3   |     for y in x {
    |              -
    |              |
    |              `x` moved due to this implicit call to `.into_iter()`
    |              help: consider borrowing to avoid moving into the for loop: `&x`
...
6   |     let z = x;
    |             ^ value used here after move
    |
note: this function takes ownership of the receiver `self`, which moves `x`

The note doesn't have a span, so we should remove it when it is DUMMY_SP.

When we make the y pattern mut, we get the same output.

We should likely suggest .iter_mut() when the pattern is mutable, and the expression isn't Copy.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-iterators Area: Iterators A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Mar 8, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 12, 2021
…r=estebank

Diagnostic cleanups

Follow up to rust-lang#81503
Helps with rust-lang#82916 (don't show note if `span` is `DUMMY_SP`)
@estebank
Copy link
Contributor Author

Current output:

error[[E0382]](https://doc.rust-lang.org/stable/error_codes/E0382.html): use of moved value: `x`
 --> src/lib.rs:8:13
  |
4 | fn foo(x: Vec<S>) {
  |        - move occurs because `x` has type `Vec<S>`, which does not implement the `Copy` trait
5 |     for y in x {
  |              - `x` moved due to this implicit call to `.into_iter()`
...
8 |     let z = x;
  |             ^ value used here after move
  |
note: `into_iter` takes ownership of the receiver `self`, which moves `x`
 --> /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/iter/traits/collect.rs:271:18
help: consider iterating over a slice of the `Vec<S>`'s content to avoid moving into the `for` loop
  |
5 |     for y in &x {
  |              +

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-iterators Area: Iterators A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant