Skip to content

Missing 'unused assignment' warning with += operator on String #102453

@jruderman

Description

@jruderman

Given the following code (playground):

fn main() {
    let mut s = "A".to_string();
    s += "B";
}

The current output shows no warnings.

Ideally the output should look like:

warning: modified value of `s` is never read
 --> src/main.rs:3:5
  |
3 |     s += "B";
  |     ^
  |
  = note: `#[warn(unused_assignments)]` on by default

Switching to s = s + "B", or changing the variable to u32, makes the desired warning appear. And indeed there are types for which += might be used for external side effects alone. But String does not really "do" anything, so s should be considered unused after s += "B".

Perhaps I want something like #[must_use_self] on String::add_assign: similar to #[must_use] except that it applies to self after the call, rather than to the method's return value.

Somewhat related to #63054.

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.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    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