Skip to content

Inconsistent "unused mut" warning on mutable references #30280

Closed
@mkpankov

Description

@mkpankov

Consider this code (playpen):

fn main() {
    // types for clarity
    let mut x: String = "hello".to_string();
    x.push('x');
    {
        let y: &mut String = &mut x;
        // y.push('x');
        println!("{}", y);
    }
    {
        // warning: variable does not need to be mutable
        let mut z: &mut String = &mut x;
        // z.push('x'); // this line disables the warning
        println!("{}", z);
    }
}

Mutability of the reference itself isn't needed for z.push('x'). Then the behavior in two nested blocks is inconsistent: mut in let mut z is, in fact, unneeded, either when doing z.push('x') or when not doing it. Only the underlying String has to be mutable. But the compiler doesn't warn about it.

I expected the warning "variable does not need to be mutable" to be emitted even when z.push('x'); line is uncommented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions