Skip to content

unused_assignments should provide more information for newcomers #154434

Description

@estebank

For

fn main(){
    let mut d = String::from("hello");
    d = String::from("ahoy");
        
    println!("{d}, world!");
}

the current output is

warning: value assigned to `d` is never read
 --> ownership.rs:2:17
  |
2 |     let mut d = String::from("hello");
  |                 ^^^^^^^^^^^^^^^^^^^^^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default

Ideally we should point at the reassignment(s) that make the default value be unused.

warning: value assigned to `d` is never read
 --> ownership.rs:2:17
  |
2 |     let mut d = String::from("hello");
  |                 ^^^^^^^^^^^^^^^^^^^^^ this value is reassigned later and never used
3 |     d = String::from("ahoy");
  |         -------------------- `d` is overwritten here before the previous value is read
  |
  = note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default

From https://users.rust-lang.org/t/ownership-page-89/139187

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.D-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.L-unused_assignmentsLint: unused_assignmentsP-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions