Skip to content

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

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

Closed
simias opened this issue Jan 30, 2015 · 14 comments
Closed

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

simias opened this issue Jan 30, 2015 · 14 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@simias
Copy link

simias commented Jan 30, 2015

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.

@ghost
Copy link

ghost commented Jan 30, 2015

This should go even further - the lint should not trigger if the field is of a type that hierarchically contains any fields implementing Drop.

@kmcallister
Copy link
Contributor

Yeah I ran into this the other day, holding on to a mutex guard in a struct.

@kmcallister
Copy link
Contributor

@jakub-: I think there's some "needs cleanup glue" predicate on types that we could query, since linting happens after type checking.

@kmcallister kmcallister added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Jan 30, 2015
@llogiq
Copy link
Contributor

llogiq commented Jun 19, 2015

@kmcallister We should be able to query the rustc::middle::ty::destructor_for_type map, which should work on both structs and enums. However, I'm not sure if it will pick up structs of which parts need a destructor or enums of which only some variants need a destructor.

@TimNN
Copy link
Contributor

TimNN commented Aug 23, 2015

How would this interact with generics?

Let's say:

struct Container<T> {
    s: T,
}

Should this warn because T may not implement drop or should this not warn because T may implement drop?

@llogiq
Copy link
Contributor

llogiq commented Aug 23, 2015

@TimNN: this lint checks at use site, so unless the code using it is generic itself, T will be replaced by some concrete type.

I think the lint should warn on fully generic instances, though possibly with another lint name and message.

@Manishearth
Copy link
Member

Code:

fn should_warn_about_field(&mut self, node: &hir::StructField_) -> bool {
(should_warn_about_field)

Useful methods: TyS::ty_adt_def, AdtDefData::has_dtor()

@cristicbz
Copy link
Contributor

Isn't the right approach for this to just name the field _some_guard? This is similar to what one does inside a function let _guard = foo.lock() or whatever. Otherwise once could also argue that PhantomData fields shouldn't warn, but doing _dummy: PhantomData<T> instead is easy enough.

@simias
Copy link
Author

simias commented Oct 16, 2015

@cristicbz that's reasonable IMO but the error message itself is still technically incorrect and potentially misleading.

@brson
Copy link
Contributor

brson commented Oct 16, 2015

Discussion on IRC looks like it leans toward leaving the warning for Drop fields, but mentioning that _ prefix will quiet it. I also feel like this is a prudent, incremental improvement.

@cristicbz
Copy link
Contributor

In general or only for Drop fields? I lean towards "in general".

@Manishearth
Copy link
Member

Only for Drop fields. In general this will occur during development when warnings are okayish. And the allow() solution is already hinted at.

@cristicbz
Copy link
Contributor

Sounds good, I'll have a go at fixing this then.

@steveklabnik
Copy link
Member

Since new lints have a big impact on users of rustc, the policy is that they should go through the RFC process like other user-facing changes. As such, I'm going to give this one a close, but if anyone comes across this ticket and wants this lint, consider adding it to clippy and/or writing up an RFC. Thanks!

damyanp added a commit to damyanp/directx-graphics-samples-rs that referenced this issue Jun 17, 2021
This is a false positive since the code isn't dead because we're relying
on the Drop behavior.

See rust-lang/rust#21775
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. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

8 participants