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

Incorrect 'struct is never constructed' warning #59003

Closed
ejpbruel2 opened this issue Mar 7, 2019 · 7 comments · Fixed by #110209
Closed

Incorrect 'struct is never constructed' warning #59003

ejpbruel2 opened this issue Mar 7, 2019 · 7 comments · Fixed by #110209
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ejpbruel2
Copy link

ejpbruel2 commented Mar 7, 2019

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
        }
    }
}
@jonas-schievink
Copy link
Contributor

You seem to have posted the same snippet twice

@jonas-schievink jonas-schievink added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Mar 7, 2019
@ejpbruel2
Copy link
Author

You seem to have posted the same snippet twice

My bad! I've updated my comment.

@steveklabnik
Copy link
Member

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)

@ejpbruel2
Copy link
Author

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)

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?

@drrlvn
Copy link
Contributor

drrlvn commented Aug 23, 2019

This also happens with enum variants and "variant is never constructed" warning, see playground link.

@akluth
Copy link

akluth commented Feb 12, 2020

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 :-)

@kadiwa4
Copy link
Contributor

kadiwa4 commented Jan 23, 2023

None of the incorrect warnings are emitted anymore (nightly 2023-01-22).
@rustbot label: +E-needs-test

@rustbot rustbot added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jan 23, 2023
JohnTitor added a commit to JohnTitor/rust that referenced this issue Apr 11, 2023
Signed-off-by: Yuki Okushi <jtitor@2k36.org>
compiler-errors added a commit to compiler-errors/rust that referenced this issue Apr 12, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 12, 2023
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#109959 (Fix transmute intrinsic mir validation ICE)
 - rust-lang#110176 (Renumbering cleanups)
 - rust-lang#110182 (Use `itertools::Either` instead of own impl)
 - rust-lang#110188 (Remove orphaned remove_dir_all implementation from rust-installer)
 - rust-lang#110190 (Custom MIR: Support `BinOp::Offset`)
 - rust-lang#110209 (Add regression test for rust-lang#59003)
 - rust-lang#110210 (`DescriptionCtx` cleanups)
 - rust-lang#110217 (doc: loongarch: Fix typos)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 4976926 Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants