Skip to content

dead_code lint shouldn't warn about unused fields that implement Drop #21775

@simias

Description

@simias

I have a use case similar to this:

struct S(u32);

impl Drop for S {
    fn drop(&mut self) {
        println!("dropping {}", self.0);
    }
}

struct Container {
    s: S,
}

impl Container {
    fn new() -> Container {
        let s = S(4);

        Container { s: s } 
    }
}

fn main() {
    Container::new();
}

The struct S is just a thin wrapper around a foreign object, it's just used to free the object in the Drop. The dead_code lint says:

warning: struct field is never used: `s`, #[warn(dead_code)] on by default

IMHO that's bogus because it seems to imply that the member could be removed without changing the behaviour of the code which is clearly not the case. The whole point is that I want s to live as long as the Container object.

Therefore I think the lint shouldn't trigger when a seemingly unused field implements Drop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions