Consider the following code snippet:
struct Foo {
inner: u32,
}
impl From<u32> for Foo {
fn from(inner: u32) -> Self {
Self {
inner
}
}
}
This will trigger a 'struct is never constructed' warning, even though the struct is clearly constructed in the implementation of From<u32>.
The problem seems to be caused by using Self as the constructor. If I replace Self with the name of the struct, as in the following code snippet, the warning goes away:
struct Foo {
inner: u32,
}
impl From<u32> for Foo {
fn from(inner: u32) -> Self {
Foo {
inner
}
}
}
You seem to have posted the same snippet twice
You seem to have posted the same snippet twice
My bad! I've updated my comment.
I think this is because Foo is not public, and so rustc can see that that trait implementation is never used. If you make it pub, the warning goes away. (With a new warning about inner never being used, which is also true, and if you make that pub, you get no warnings)
I _think_ this is because
Foois not public, and so rustc can see that that trait implementation is never used. If you make itpub, the warning goes away. (With a new warning about inner never being used, which is also true, and if you make _that_pub, you get no warnings)
That seems reasonable, but in that case, I'd expect to get a warning if struct Foo is private, regardless of whether I instantiate it with Foo or Self in the implementation of From. As it stands, I only get a warning if I instantiate it with Self, which seems inconsistent?
This also happens with enum variants and "variant is never constructed" warning, see playground link.
This also applies in connection with 47133:
pub struct SlackStatus;
impl SlackStatus {
pub const STARTING: &'static str = "starting";
pub const SUCCESS: &'static str = "success";
pub const ERROR: &'static str = "error";
}
I get warnings about struct is never constructed and associated const is never used although they are clearly used multiple times in my code.
But maybe I'm doing something wrong here or getting it wrong as a concept in my head; if so, I'm glad for some pointers and hints :-)
Most helpful comment
I think this is because
Foois not public, and so rustc can see that that trait implementation is never used. If you make itpub, the warning goes away. (With a new warning about inner never being used, which is also true, and if you make thatpub, you get no warnings)