Skip to content

Reduce span highlighted code in unused_variables lint #50472

Closed
@killercup

Description

@killercup

Let's say I wrote this critical piece of code:

fn main() {
    let mut i = 42;
}

rustc gives me the following output:

warning: unused variable: `i`
 --> src/main.rs:2:9
  |
2 |     let mut i = 42;
  |         ^^^^^ help: consider using `_i` instead
  |
  = note: #[warn(unused_variables)] on by default

warning: variable does not need to be mutable
 --> src/main.rs:2:9
  |
2 |     let mut i = 42;
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: #[warn(unused_mut)] on by default

Which is correct -- except for one little detail. Which is crucial, if you are not just any developer but actually rustfix.

You see, the mut in mut i is highlighted, but the replacement is just for the binding name, not the mutability. (Why not the mutability? Because the mutability is covered by the next lint we trigger, unused_mut.) And rustfix will rightfully complain if we try to replace parts of the code that we have already replaced.

tl;dr We need to change the span in unused_variables to only cover the i

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions