Skip to content

Unused assignment warning should trigger for structs #16789

Open
@crumblingstatue

Description

@crumblingstatue

This code should ideally trigger warnings about unused assignments, but it doesn't.

struct Pos {
    x: i32
}

fn foo() -> Pos{
    Pos{x: 2}
}

fn main() {
    foo().x += 1; // Unused assignment
    let mut f = Pos { x: 2 };
    f.x = 2; // Unused assignment
}

It could be useful in situations like this:

struct Property {
    x: i32
}

struct Foo {
    prop: Property
}

impl Foo {
    fn prop(&self) -> Property {
        self.prop
    }
    fn mut_prop(&mut self) -> &mut Property {
        &mut self.prop
    }
}

fn main() {
    let mut foo = Foo{prop: Property{x: 2}};
    foo.prop().x += 2; // Oops, accidentally called prop() instead of mut_prop()
    foo.mut_prop().x += 2;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.L-unused_assignmentsLint: unused_assignmentsT-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