How to get warning about unused type?

I have code like this:

mod foo {
    use serde::Deserialize;

    #[derive(Deserialize)]
    pub struct Foo {}
}

fn main() {}

So struct Foo is unused,
but because of #[derive(Deserialize)] there is code to construct it,
so I do not see any warnings, that it is not used.

Any way to force compiler warn about such structs?

1 Like

Change pub to pub(crate).

Still no warning:

mod foo {
    use serde::Deserialize;

    #[derive(Deserialize)]
    pub(crate) struct Foo {}
}

fn main() {
    println!("Results:")
}

If you add a field to Foo it will throw a dead_code warning, because the field is never read:

mod foo {
    use serde::Deserialize;

    #[derive(Deserialize)]
    pub struct Foo {
        member: u8,
    }
}

fn main() {}

Playground.

But this won't warn if your type also implements Serialize. See this answer:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.