Skip to content

Detect call to _::foo(&mut self) confused with _::foo(&self) -> Self #159486

Description

@estebank

Code

fn main() {
    let mut values = vec![3, 1, 2];
    let sorted = values.sort();
    println!("{}", sorted[0]);
    //~^ ERROR cannot index into a value of type `()`
}

Current output

error[E0608]: cannot index into a value of type `()`
 --> src/main.rs:4:26
  |
4 |     println!("{}", sorted[0]);
  |                          ^^^
  |
  = help: tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.

Desired output

error[E0608]: cannot index into a value of type `()`
 --> src/main.rs:4:26
  |
2 |     let mut values = vec![3, 1, 2];
3 |     let sorted = values.sort();
  |                         ------ this method call modifies `values` in-place
4 |     println!("{}", sorted[0]);
  |                          ^^^
  |
note: `Vec::sort` takes `&mut Self` and returns `()`
   |
LL | ...
   |
help: you might have meant to use `values` after calling `sort`
  |
3 -     let sorted = values.sort();
4 -     println!("{}", sorted[0]);
3 +     values.sort();
4 +     println!("{}", values);
  |

Rationale and extra context

At the very least we should be pointing at the place where a binding was turned into ().

Other cases

We need to account for this in assignments, binops, unops, method calls and returns.

Rust Version

1.97

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions