Skip to content

warning lint on chained assignment #63556

@hellow554

Description

@hellow554

(This is a first impl on https://users.rust-lang.org/t/warn-about-using-the-value-of-an-assignment-expression/31324, thanks to @sourcefrog )

Code like a = b = 3 is a "common" pattern in C, python and other languages. People who are coming to Rust may be surprised to see, that it doesn't work the intended way. There's no warning whatsoever, just a clippy lint:

warning: this let-binding has unit value
 --> src/main.rs:2:5
  |
2 |     let a;
  |     ^^^^^^

I managed to implement this:

warning: chaining assignment is not supported in Rust
 --> a.rs:4:5
  |
4 |     a = b = 3;
  |     ^^^^^^^^^
  |
  = note: `#[warn(chained_assignment)]` on by default

But I have a few questions:
What does the user actually want?
Does he want
1 a = 3; b = 3
2 a = b == 3

The first one only works if the value is Copy for obvious reasons.
The second one only works if the value implements PartialEq.

I would like to see this in the Rust compiler, what do you think? What is missing? Am I overlooking something? Is everything covered? Would it brake any existing code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions