Skip to content
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

field is never read warning doesn't consider Drop implementation #112290

Open
lvella opened this issue Jun 4, 2023 · 4 comments
Open

field is never read warning doesn't consider Drop implementation #112290

lvella opened this issue Jun 4, 2023 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lvella
Copy link

lvella commented Jun 4, 2023

Code

struct PrintOnDrop(&'static str);
impl Drop for PrintOnDrop {
    fn drop(&mut self) {
        println!("{}", self.0);
    }
}

pub struct A {
    act_on_drop: PrintOnDrop,
}

impl A {
    pub fn new(msg: &'static str) -> A {
        A {
            act_on_drop: PrintOnDrop(msg),
        }
    }
}

Current output

warning: field `act_on_drop` is never read
 --> <source>:9:5
  |
8 | pub struct A {
  |            - field in this struct
9 |     act_on_drop: PrintOnDrop,
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

Compiler returned: 0

Desired output

Nothing.

Rationale and extra context

Since the struct A is public and constructible, the compiler should assume that the drop for all its Drop fields will be called, which in this case makes the issued warning wrong.

Other cases

No response

Anything else?

Tested on 1.70.0 and on today's nightly (398fa21 2023-06-03).

@lvella lvella added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 4, 2023
@SkiFire13
Copy link
Contributor

SkiFire13 commented Jun 4, 2023

There's a thin line here between what's technically correct and what's useful.

Would you also expect this to not throw a warning?

pub struct Foo {
    pub bar: String
}

After all String does has drop glue that ultimately call a Drop implementation, it would be more useful to detect cases like this as unused. A way to do this is pretend that values are never used only for their destructor side-effects, even though this is sometimes the case like in your example.

@lvella
Copy link
Author

lvella commented Jun 4, 2023

There's a thin line here between what's technically correct and what's useful.

Would you also expect this to not throw a warning?

pub struct Foo {
    pub bar: String
}

After all String does has drop glue that ultimately call a Drop implementation, it would be more useful to detect cases like this as unused. A way to do this is pretend that values are never used only for their destructor side-effects, even though this is sometimes the case like in your example.

Well, not a good example, because the field is pub so there isn't a warning. But point taken: having a field just for its drop is an exceptional case.

@Noratrieb
Copy link
Member

I think we should err on the side of false positives here. For your case, you can just #[allow] (and soon, #[expect]) the lint on the field. This also makes the code clearer by telling the reader that the field is indeed just here for its drop.

@Noratrieb Noratrieb added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jun 5, 2023
@asquared31415
Copy link
Contributor

the compiler should assume that the drop for all its Drop fields will be called

This is not strictly true because mem::forget, Box::leak, Rc cycles, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-feature-request Category: A feature request, i.e: not implemented / a PR. 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

5 participants
@lvella @SkiFire13 @asquared31415 @Noratrieb and others