Skip to content

"error[E0435]: attempt to use a non-constant value in a constant" shows wrong fix with mut variables #91560

Closed
@yedayak

Description

@yedayak

I tried this code:

fn main() {
    let mut length: usize = 2;
    let arr = [0; length];
}

I expected to see this happen: Get an error about using non constant values in a constant, with a help pointer showing where to change "let" to "const", like in this example without the mut keyword:

2 |     let length: usize = 2;
  |     ---------- help: consider using `const` instead of `let`: `const length`

Instead, this happened: I get the correct error, but it points to a wrong place to fix, off by one character:

2 |     let mut length: usize = 2;
  |      ------------- help: consider using `const` instead of `let`: `const length`

This means auto correcting it using the rust-analyzer vscode plugin produces invalid code:

lconst length: usize = 2;

Meta

rustc --version --verbose:

rustc 1.59.0-nightly (efec54529 2021-12-04)
binary: rustc
commit-hash: efec545293b9263be9edfb283a7aa66350b3acbf
commit-date: 2021-12-04
host: x86_64-pc-windows-msvc
release: 1.59.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions