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

Most dead code warnings don't work inside const _ #101532

Open
RReverser opened this issue Sep 7, 2022 · 1 comment
Open

Most dead code warnings don't work inside const _ #101532

RReverser opened this issue Sep 7, 2022 · 1 comment
Labels
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.

Comments

@RReverser
Copy link
Contributor

Given the following code:

const _: () = {
    let a: ();

    struct B {}

    enum C {}

    fn d() {}

    const E: () = {};
};

The current output is:

warning: unused variable: `a`
 --> <source>:2:9
  |
2 |     let a: ();
  |         ^ help: if this is intentional, prefix it with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

Ideally the output should look like:

warning: unused variable: `a`
 --> <source>:2:9
  |
2 |     let a: ();
  |         ^ help: if this is intentional, prefix it with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: struct `B` is never constructed
 --> <source>:4:12
  |
4 |     struct B {}
  |            ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: enum `C` is never used
 --> <source>:6:10
  |
6 |     enum C {}
  |          ^

warning: function `d` is never used
 --> <source>:8:8
  |
8 |     fn d() {}
  |        ^

warning: constant `E` is never used
  --> <source>:10:5
   |
10 |     const E: () = {};
   |     ^^^^^^^^^^^^^^^^^

warning: 5 warnings emitted

What's surprising is that adding pub changes the output to the expected one, even though pub doesn't make any meaning on const _: ():

pub const _: () = {
    let a: ();

    struct B {}

    enum C {}

    fn d() {}

    const E: () = {};
};

It's also interesting that the logic matches what happens with functions. For example, in:

fn foo() {
    let a: ();

    struct B {}

    enum C {}

    fn d() {}

    const E: () = {};
}

the function foo itself and also only the variable a are reported as unused, while the rest of the items don't get a warning:

warning: unused variable: `a`
 --> <source>:2:9
  |
2 |     let a: ();
  |         ^ help: if this is intentional, prefix it with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: function `foo` is never used
 --> <source>:1:4
  |
1 | fn foo() {
  |    ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 2 warnings emitted

However, once foo is made public, all the items get warnings:

warning: unused variable: `a`
 --> <source>:2:9
  |
2 |     let a: ();
  |         ^ help: if this is intentional, prefix it with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: struct `B` is never constructed
 --> <source>:4:12
  |
4 |     struct B {}
  |            ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: enum `C` is never used
 --> <source>:6:10
  |
6 |     enum C {}
  |          ^

warning: function `d` is never used
 --> <source>:8:8
  |
8 |     fn d() {}
  |        ^

warning: constant `E` is never used
  --> <source>:10:5
   |
10 |     const E: () = {};
   |     ^^^^^^^^^^^^^^^^^

warning: 5 warnings emitted

Either way, const _ should have different treatment due to being anonymous, but it's still a bit surprising that only let variables get inside both const _ and fn foo.

@RReverser RReverser 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 Sep 7, 2022
@Noratrieb
Copy link
Member

Noratrieb commented Sep 7, 2022

Trying a few more and see whether it warns for the unused S

fn main() {
    let x = { struct S; }; // WARN
    let _ = { struct S; }; // WARN
    const CX: () = { struct S; }; // NO WARN
    const _: () = { struct S; }; // NO WARN
    static SX: () = { struct S; }; // NO WARN
    static _SX: () = { struct S; }; // NO WARN
}

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

2 participants