Unused assignment warning should trigger for structs

f7b5d76
Opened by crumblingstatue at 2025-03-01 11:36:30

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;
}
  1. 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;
    }
    
    <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_ASSIGN_START --> <!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"LeSeulArtichaut"}$$TRIAGEBOT_ASSIGN_DATA_END --> <!-- TRIAGEBOT_ASSIGN_END --> <!-- TRIAGEBOT_END -->

    rustbot at 2020-04-24 17:49:56

  2. Triage: no change.

    Steve Klabnik at 2016-02-02 21:48:50

  3. Triage: no change

    Steve Klabnik at 2018-09-24 17:49:58

  4. I’ll try to take a look at this! @rustbot claim

    Léo Lanteri Thauvin at 2020-04-24 17:49:55