Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Let statement with wildcard lead to warnings for unused variables #57

Closed
roxelo opened this issue Aug 15, 2021 · 8 comments · Fixed by rust-lang/rust#88271
Closed

Let statement with wildcard lead to warnings for unused variables #57

roxelo opened this issue Aug 15, 2021 · 8 comments · Fixed by rust-lang/rust#88271
Assignees

Comments

@roxelo
Copy link
Member

roxelo commented Aug 15, 2021

There seem to be inconsistencies with lint warnings when using let statement with wildcards inside closures.

struct Props {
    field_1: u32, //~ WARNING: field is never read: `field_1`
    field_2: u32, //~ WARNING: field is never read: `field_2`
}

fn inside_closures() {
    let props_2 = Props { //~ WARNING: unused variable: `props_2`
        field_1: 1,
        field_2: 1,
    };

    let _ = || {
        let _: Props = props_2; // Warning lint does not consider `props_2` as being used here.
    };
}

fn ourside_closures() {
    let props_2 = Props {
        field_1: 1,
        field_2: 1,
    };

    let _: Props = props_2; // Warning lint does not consider `props_2` as being used here.
}

Note this same inconsistency takes place without type anotation.

@m-ou-se
Copy link
Member

m-ou-se commented Aug 23, 2021

What's the state of this? Does this need to be fixed before we release Rust 2021?

@jyn514
Copy link
Member

jyn514 commented Aug 23, 2021

@roxelo how is this related to disjoint captures? Wouldn't the same warning occur either way?

@m-ou-se
Copy link
Member

m-ou-se commented Aug 23, 2021

warning: unused variable: `props_2` only shows up with #![feature(capture_disjoint_fields)] enabled.

@nikomatsakis
Copy link
Contributor

So I looked into this a bit. Basically the problem is that we should consider all local variables that get mentioned by the closure to be at least... read? That seems plausible. They may also be mutated/moved. Is the problem @roxelo that we are not taking "fake reads" into account?

@roxelo
Copy link
Member Author

roxelo commented Aug 23, 2021

Yes, that's what Aman and I were thinking the issue was: https://rust-lang.zulipchat.com/#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/Lint.20warning.20inconsistencies/near/249786245. I'll try having a fix later tonight

@arora-aman
Copy link
Member

I remember this now 😅

We had purposely decided to do it at one point I think.

Here is the relevant code https://github.com/rust-lang/rust/blob/master/compiler/rustc_passes/src/liveness.rs#L1243-L1249

@arora-aman
Copy link
Member

@rustbot claim

@rustbot
Copy link

rustbot commented Aug 23, 2021

Error: This repository is not enabled to use triagebot.
Add a triagebot.toml in the root of the master branch to enable it.

Please let @rust-lang/release know if you're having trouble with this bot.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants