Skip to content
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

[NLL] variable does not need to be mutable #51830

Closed
kpp opened this issue Jun 26, 2018 · 4 comments · Fixed by #51964
Closed

[NLL] variable does not need to be mutable #51830

kpp opened this issue Jun 26, 2018 · 4 comments · Fixed by #51964
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-NLL Area: Non-lexical lifetimes (NLL)

Comments

@kpp
Copy link
Contributor

kpp commented Jun 26, 2018

UPD:
minimal repro from @lqd :

#![feature(nll)]
#![allow(unused_variables)]
fn main() {
    let _ = Some(0).as_ref().map(|ref a| true);
}

I got this warning:

warning: variable does not need to be mutable
   --> src/toxcore/dht/server/mod.rs:363:32
    |
363 |         ping_map.retain(|&_pk, ref client|
    |                                ----^^^^^^
    |                                |
    |                                help: remove this `mut`

While I don't see any mut in the given code. The given patch fixed the warning but still I don't know why:

-        ping_map.retain(|&_pk, ref client|
+        ping_map.retain(|_pk, client|

You may try on: tox-rs/tox@3d0ddf8 (you will have to enable NLL on your own).

Also seems like you don't highlight the correct site of the error in macros:

warning: variable does not need to be mutable
   --> src/toxcore/state_format/old.rs:173:9
    |
173 | /         do_gen!(buf,
174 | |             gen_le_u16!(0x0002) >>
175 | |             gen_slice!(SECTION_MAGIC) >>
176 | |             gen_le_u32!(DHT_MAGICAL as u32) >>
...   |
180 | |             gen_many_ref!(&self.0, |buf, node| PackedNode::to_bytes(node, buf))
181 | |         )
    | |_________^ help: remove this `mut`
    |
    = note: #[warn(unused_mut)] on by default
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: variable does not need to be mutable
 --> <gen_many_ref macros>:3:32
  |
3 | Ok ( ( $ i , $ idx ) ) , | r , ref v | {
  |                                ----^
  |                                |
  |                                help: remove this `mut`

I would like you to highlight gen_many_ref!.

@leonardo-m
Copy link

Dupe of #50897 ?

@kpp
Copy link
Contributor Author

kpp commented Jun 27, 2018

I tried to search for dups before I posted my issue. The difference with #50897 is that there is mut keyword in the code while my code does not have it.

@csmoe csmoe added A-diagnostics Area: Messages for errors, warnings, and lints A-NLL Area: Non-lexical lifetimes (NLL) labels Jun 27, 2018
@lqd
Copy link
Member

lqd commented Jun 28, 2018

I've had this one while trying to bootstrap rustc with NLL turned on, and had this minimal repro

@nikomatsakis
Copy link
Contributor

Tagging as NLL-deferred because #51918 has precedence

bors added a commit that referenced this issue Jul 5, 2018
…omatsakis

[NLL] Fix various unused mut errors

Closes #51801
Closes #50897
Closes #51830
Closes #51904
cc #51918 - keeping this one open in case there are any more issues

This PR contains multiple changes. List of changes with examples of what they fix:

* Change mir generation so that the parameter variable doesn't get a name when a `ref` pattern is used as an argument
```rust
fn f(ref y: i32) {} // doesn't trigger lint
```
* Change mir generation so that by-move closure captures don't get first moved into a temporary.
```rust
let mut x = 0; // doesn't trigger lint
move || {
    x = 1;
};
```
* Treat generator upvars the same as closure upvars
```rust
let mut x = 0; // This mut is now necessary and is not linted against.
move || {
    x = 1;
    yield;
};
```

r? @nikomatsakis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-NLL Area: Non-lexical lifetimes (NLL)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants