-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Value assigned is never read #49171
Comments
Hi, I think what it means is that the result of the assignment of note that fn main() {
let foo;
foo = "Hi!".to_string();
println!("{}", foo);
} doesn't trigger a warning. |
Hi, the warning underlines the the part
|
Yeah, I agree that's a bit confusing. |
Tagging with |
This is #33536 which was incorrectly closed. |
Hello,
The tuple in the indirect link to the other ticket would not need a pretty print to know how to address the problem with the above advice with both lhs and rhs warnings. Like: |
2 | let (mut a, b) = tup;
| ^^^ ^^^
| Thank you. Good day. |
(I am working on this) |
Note that upon removing the unused initializer fn main() {
let mut foo;
foo = "Hi!".to_string();
println!("{}", foo);
} one does indeed get another warning that warning: variable does not need to be mutable
--> src/main.rs:2:6
|
2 | let mut foo;
| ---^^^^
| |
| help: remove this `mut`
|
= note: #[warn(unused_mut)] on by default I assume in the general case getting both at once could be hard or false-positive-y, so the two-step is probably fine for this. |
Thanks to Yemi Bedu for suggesting that we put a span on both sides of the assignment. (Some might argue that we should just put the single span on the right-hand side, but as the change to liveness-dead.rs highlights, it's also nice to have a span on the left when the left-hand side has multiple patterns being assigned.) Resolves rust-lang#49171.
Hello, What @zackmdavis has presented in the merge looks clean and useful. Only one nitpick:
|
Current output:
That help should have a span pointing to the reassignment of |
Seems wrong. Unused warning should not show up here?
Output:
|
No, the 6 written on that line is never read (it is overwritten on the next line, by setting to 7). I agree with @estebank that it'd be good to point out where the write occurs, though. |
|
|
I'd also like to chime in on this, I hit it on this (obviously cut down) snippet of code:
The issue here is that the innermost loop is a 60 FPS rendering loop. The outer loop is a window resize loop. So, at the B: conditional, if the issue is resize, we can possibly adjust the window and recover and go back to rendering, but sometimes we can't. So, while I do overwrite the value, I need the binding to live long enough to be able to do the right thing. Otherwise, I have to start creating multiples values at different scopes just to abort my loops correctly with appropriate cleanup. I'm not sure I consider this a bug as I can work around it if I really wanted to. I can obviously create separate variables and scope everything properly to avoid the warning with the attendant extra code and grief. However, most of the time I just consider this a false positive and move on with life since that code may disappear at some point if I do a refactor for some reason. Thanks. |
At least one of those initializations can be trivially removed: let mut ff_loop_abort;
loop { // Resize loop
ff_loop_abort = false;
... lots of code here ...
}
if ff_loop_abort {
println!("loop aborted abnormally");
}
|
I have the following simple code:
The compiler gives me the following warning:
This warning seems to be wrong to me. The assignment
foo
has been used in the println! macro. So I should not get this error. When I remove the mutable assignment as follows, I get no warning as expected:I am using rustc 1.24.1 (d3ae9a9 2018-02-27) on Windows 7.
The text was updated successfully, but these errors were encountered: