Repository: rust
Issue: Most dead code warnings don't work inside `const _`
Labels: A-diagnostics,T-compiler

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.

If you cannot produce a minimal reproduction case (something that would work in
isolation), please provide the steps or even link to a repository that causes
the problematic output to occur.
-->

Given the following code: <!-- Please provide a link to play.rust-lang.org -->

```rust
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
```

<!-- The following is not always necessary. -->
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 _: ()`:

```rust
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:

```rust
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`.

<!--
If the problem is not self-explanatory, please provide a rationale for the
change.
-->

<!--
If dramatically different output is caused by small changes, consider also
adding them here.

If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions. The output might also be
different depending on the Edition.
-->