Skip to content

Warning that variable does not need to be mutable, while in fact, it does #52614

Closed
@PieterPenninckx

Description

@PieterPenninckx

I tried this code:

struct S<'a> { x: &'a mut u8 }

impl<'a> S<'a> {
	fn f(&self, y: &'a mut u8) -> &'a mut u8 { y }
}

fn main() {
	let mut b = 5;
	let c;
	{
		let mut a = 4;
		let s = S{x: &mut a};
		c = s.f(&mut b);
	}
}

The compiler warns that the variable a does not need to be mutable:

warning: variable does not need to be mutable
  --> test.rs:11:7
   |
11 |         let mut a = 4;
   |             ----^
   |             |
   |             help: remove this `mut`
   |
   = note: #[warn(unused_mut)] on by default

I did not expect this warning because a reference as mutable to a is taken later on.
(The other errors and warnings are expected.)

Meta

rustc --version --verbose:

rustc 1.29.0-nightly (12ed235ad 2018-07-18)
binary: rustc
commit-hash: 12ed235adc62e63b16bb4f715b143c37a5efa00d
commit-date: 2018-07-18
host: x86_64-unknown-linux-gnu
release: 1.29.0-nightly
LLVM version: 7.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsfixed-by-NLLBugs fixed, but only when NLL is enabled.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions