# Should dead code count toward var liveness?

**URL:** <https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996>\
**Category:** compiler\
**Created:** [July 11, 2022, 9:10pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996 "2022-07-11T21:10:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![camsteffen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/camsteffen/32/3246_2.png) [@camsteffen](https://internals.rust-lang.org/u/camsteffen)\
**Post date:** [July 11, 2022, 9:10pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/1 "2022-07-11T21:10:37Z")

</div>

Currently, the following code triggers `unused_variables` and `unreachable_code`. I feel that the `unused_variables` lint is just noise, and would prefer that variables usages in dead code still "count".

```rust
let x = 1;
return;
dbg!(x);

```

I'm working through refactoring the liveness analysis. If this change is acceptable, it would reduce complexity.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [July 11, 2022, 9:22pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/2 "2022-07-11T21:22:22Z")

</div>

If the code isn't reachable, how is it used?

---

<div class="post-metadata">

**Author:** ![camsteffen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/camsteffen/32/3246_2.png) [@camsteffen](https://internals.rust-lang.org/u/camsteffen)\
**Post date:** [July 11, 2022, 9:26pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/3 "2022-07-11T21:26:16Z")

</div>

I guess I am differentiating between "used in code" and "used at runtime". In practice, if I have code like the above, it's most likely because I am just trying something out randomly ("what happens if the rest of this function just doesn't run?"). The `unreachable_code` lint is sufficient to help me remember to fix it.

---

<div class="post-metadata">

**Author:** ![quaternic](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quaternic/32/10440_2.png) [@quaternic](https://internals.rust-lang.org/u/quaternic)\
**Post date:** [July 11, 2022, 9:57pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/4 "2022-07-11T21:57:40Z")

</div>

`x` is used in the sense that the code wouldn't compile without it:

```rust
return;
// error[E0425]: cannot find value `x` in this scope
dbg!(x);

```

---

<div class="post-metadata">

**Author:** ![camsteffen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/camsteffen/32/3246_2.png) [@camsteffen](https://internals.rust-lang.org/u/camsteffen)\
**Post date:** [July 11, 2022, 10:16pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/5 "2022-07-11T22:16:01Z")

</div>

Yeah @quaternic I think what you're alluding to is that, if the user does the logical thing and deletes the variable, surprise!

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [July 12, 2022, 12:35am UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/6 "2022-07-12T00:35:12Z")

</div>

Hmm, with NLL we've somewhat decided that dead uses aren't uses. If the dead use was a "use" (as it was in 1.9), then this still wouldn't compile: [https://rust.godbolt.org/z/e3bh5jhaG](https://rust.godbolt.org/z/e3bh5jhaG)

So arguably saying it's "unused" is correct.

But also it's a lint, so it's more important that it be useful than pedantic.

What's the new definition you were thinking of using for it? Some sort of HIR-level thing based on "name resolution resolved to it at least once" instead of a MIR-level thing?

---

<div class="post-metadata">

**Author:** ![camsteffen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/camsteffen/32/3246_2.png) [@camsteffen](https://internals.rust-lang.org/u/camsteffen)\
**Post date:** [July 12, 2022, 12:23pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/7 "2022-07-12T12:23:28Z")

</div>

> [@scottmcm](#):
>
> What's the new definition you were thinking of using for it? Some sort of HIR-level thing based on "name resolution resolved to it at least once" instead of a MIR-level thing?

It is already implemented in HIR and has special handling of `return` etc. I'm just proposing to not do that. My definition would be "fires if the variable is never referenced in code" (I think that's effectively the same as what you said).

In other words, "code is reachable" is orthogonal to "code references variable x". Or it least it could be thought of that way.

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [July 12, 2022, 12:44pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/8 "2022-07-12T12:44:18Z")

</div>

Is a variable only used within a `cfg!` or `#[cfg]` that is `false` "used"?

---

<div class="post-metadata">

**Author:** ![camsteffen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/camsteffen/32/3246_2.png) [@camsteffen](https://internals.rust-lang.org/u/camsteffen)\
**Post date:** [July 12, 2022, 1:03pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/9 "2022-07-12T13:03:49Z")

</div>

Interesting. No, I would say `cfg` is different, it's like "I really intend for this code to be (conditionally) unreachable". And "unused for the current config" should be linted. But control-flow-unreachable code is usually supposed to be reachable, but regardless of whether it is, there's `unreachable_code` to tell the user something's wrong.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [July 12, 2022, 5:29pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/10 "2022-07-12T17:29:26Z")

</div>

> [@camsteffen](#):
>
> It is already implemented in HIR and has special handling of `return` etc.

Ah, that's interesting.

I think it should definitely be either

- Control-flow unaware and on HIR (like the `unsafe` checking will soon be), or
- Control-flow aware and on MIR (like NLL and `mut` checking)

If it's currently control-flow aware yet on HIR, that definitely sounds like it should change.

I don't think I have a confident answer which way is the right solution.

Maybe for now just remove the flow-awareness from the HIR lint, and we can always add a "value never read on any possible path" kind of MIR lint later?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [July 12, 2022, 7:54pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/11 "2022-07-12T19:54:56Z")

</div>

`cfg!` gated use is definitionally the same as `if false { use; }`. (I.e. control-flow dependent use.)

`#[cfg]` gated use is definitionally the same as `{}`. (I.e. no use.)

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [July 12, 2022, 8:17pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/12 "2022-07-12T20:17:06Z")

</div>

As an illustrative example:

```rust
if false {
    println!(); // not considered unreachable_code
}
return;
println!(); // considered unreachable_code

```

Another illustrative example:

```rust
pub fn demo1() -> String {
    let mut s = "1".to_string();
    let b = &mut s; // unused_variables
    s = "2".to_string();
    return s;
    *b = "3".to_string(); // unreachable_code
}

pub fn demo2() -> String {
    let mut s = "1".to_string();
    let b = &mut s;
    s = "2".to_string();
    if false {
        *b = "3".to_string(); // cannot assign to `s` because it is borrowed
    }
    return s;
}

```

So

> [@scottmcm](#):
>
> with NLL we've somewhat decided that dead uses aren't uses.

isn't quite fully accurate; you also have to define that how "dead uses" is defined, and that's roughly "exists after a `-> !` expression." Or restated again, MIR control-flow aware lints/checks/etc are done after pruning unreachable-due-to-divergence basic blocks, but before unreachable-due-to-control-flow basic blocks are pruned.

I'm fairly in agreement that unused\_code should be on MIR / divergence-aware, and that unused\_variables should be on HIR / divergence-unaware.

> [@scottmcm](#):
>
> Maybe for now just remove the flow-awareness from the HIR lint, and we can always add a "value never read on any possible path" kind of MIR lint later?

It does seem potentially reasonable to have a separate unused\_except\_unreachable lint over MIR (so long as it doesn't duplicate HIR unused lints). Choosing whether it's available/on is a question of choosing between

- emitted warnings should be independently fixable
- fixing warnings should not introduce new warnings (that are trivial to predict)

but one could make a reasonable argument that the OP unused\_variables lint is D-incorrect, because the implied (though not outright suggested) fix to remove the variable results in an error.

The unused\_variables lint could be extended to suggest a machine-applicable fix of removing the full variable if it checks the expression to be clippy::no\_effect.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 10, 2022, 8:17pm UTC](https://internals.rust-lang.org/t/should-dead-code-count-toward-var-liveness/16996/13 "2022-10-10T20:17:37Z")

</div>

Closed on October 10, 2022, 8:17pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
