Skip to content

Capturing a mut variable and then modifying the variable emits a dead-assignment warning #7966

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

Closed
alco opened this issue Jul 22, 2013 · 3 comments
Labels
A-destructors Area: Destructors (`Drop`, …) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@alco
Copy link
Contributor

alco commented Jul 22, 2013

fn main() {
    let mut a = 5;
    let adder = |x: int| x + a;
    a = 6;
    println(fmt!("%d", adder(1)));
}

---

λ rust run 07-mut-local-closure.rs
07-mut-local-closure.rs:4:4: 4:5 warning: value assigned to `a` is never read [-W dead-assignment (default)]
07-mut-local-closure.rs:4     a = 6;
                              ^
warning: no debug symbols in executable (-arch x86_64)
7

Expected behavior: no warning because the variable is read from in the subsequent adder(1) call.

@bblum
Copy link
Contributor

bblum commented Aug 23, 2013

Hmm, interesting. This is probably a result of the compiler assuming a function won't do anything interesting between constructing a closure and calling it (or passing it to another function). The fix will be nontrivial.

@pnkfelix
Copy link
Member

This is still a problem.

@alexcrichton
Copy link
Member

Closing, all captures now count as borrows, disallowing further assignment.

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 23, 2021
Run rustfmt on batches of multiple files

changelog: none

This gives `cargo dev fmt` a nice speed boost, down from 90s (because old) on my laptop and 120s (because windows) on my desktop to ~5s on both

250 at a time was to give windows a good amount of headroom (failed at ~800, rust-lang#40384)

Also adds rustfmt to the toolchain file and has the clippy_dev workflow test using the pinned version as a follow up to rust-lang#7963
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-destructors Area: Destructors (`Drop`, …) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

4 participants