Skip to content

Unused assignment warning should trigger for structs #16789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
crumblingstatue opened this issue Aug 27, 2014 · 3 comments
Open

Unused assignment warning should trigger for structs #16789

crumblingstatue opened this issue Aug 27, 2014 · 3 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. L-unused_assignments Lint: unused_assignments T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@crumblingstatue
Copy link
Contributor

crumblingstatue commented Aug 27, 2014

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;
}
@crumblingstatue crumblingstatue changed the title Unused assignment warning should trigger for struct fields Unused assignment warning should trigger for structs Aug 27, 2014
@sanxiyn sanxiyn added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Jan 27, 2015
@steveklabnik
Copy link
Member

Triage: no change.

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 21, 2017
@steveklabnik
Copy link
Member

Triage: no change

@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 12, 2020
@LeSeulArtichaut
Copy link
Contributor

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

@LeSeulArtichaut LeSeulArtichaut removed their assignment Sep 5, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 17, 2024
cfg Attribute Stripping for Proc Macro Expansion

This will attempt to process cfg attributes and cfg_attr attributes for proc macro expansion.
![image](https://github.com/rust-lang/rust-analyzer/assets/11785959/b85ef203-14a5-44c9-9b67-59a65a5f2d96)

Closes rust-lang#8434 , rust-lang#11657, and rust-lang#13904
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
@jieyouxu jieyouxu added L-unused_assignments Lint: unused_assignments C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. L-unused_assignments Lint: unused_assignments T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants