Skip to content

Bug: The compiler claims that a variable isn't read from when it is #56436

Closed
@JohnGinger

Description

@JohnGinger

The compiler claims that a variable isn't read from when it is

I tried this code:

fn main() {
    let mut read_compiler_error = 0;
    let mut i = 0;
    loop {
        i += 1;
        if i == 4 {
            read_compiler_error = i;
            break;
        }
    }
    let loops = 25 % read_compiler_error;

    println!("Loops are {}", loops)
}

I expected to see this happen:

The code to compile without warning

Instead, this happened:

warning: value assigned to `read_compiler_error` is never read
 --> src/main.rs:2:13
  |
2 |     let mut read_compiler_error = 0;
  |             ^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_assignments)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.50s
     Running `target/debug/bug`
Loops are 1

It has to have been read, otherwise it wouldn't give loops are 1.

Meta

rustc --version --verbose:

Johns-MacBook-Pro-2:bug johnginger$ rustc --version --verbose
rustc 1.30.1 (1433507eb 2018-11-07)
binary: rustc
commit-hash: 1433507eba7d1a114e4c6f27ae0e1a74f60f20de
commit-date: 2018-11-07
host: x86_64-apple-darwin
release: 1.30.1
LLVM version: 8.0

I might be doing something wrong, I'm fairly new to rust, but this seems like a bug to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions