Skip to content

Span for unused_macros lint is too large, which makes writing macros in IDEs annoying #90745

Closed
@PatchMixolydic

Description

@PatchMixolydic

Given the following code (playground):

macro_rules! foo {
    () => {};
}

fn bar() {
    // do stuff
}

The current output is:

warning: unused macro definition
 --> src/lib.rs:1:1
  |
1 | / macro_rules! foo {
2 | |     () => {};
3 | | }
  | |_^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: function is never used: `bar`
 --> src/lib.rs:5:4
  |
5 | fn bar() {
  |    ^^^
  |
  = note: `#[warn(dead_code)]` on by default

Note how the span for the unused_macros lint covers the entire macro definition. This is fine for terminal diagnostic output, but when using an IDE or a Rust language server, this causes the entire macro definition to be underlined with yellow:

A screenshot of VSCode with rust-analyzer demonstrating the problem.

In my experience, this tends to make writing macros obnoxious without allowing unused_macros (either globally or locally).

Ideally, the span for this lint should be reduced to the macro's identifier, similar to how the dead_code lint's span only highlights bar's identifier:


warning: unused macro definition
 --> src/lib.rs:1:1
  |
1 | macro_rules! foo {
  |              ^^^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: function is never used: `bar`
 --> src/lib.rs:5:4
  |
5 | fn bar() {
  |    ^^^
  |
  = note: `#[warn(dead_code)]` on by default

@rustbot modify labels: +A-macros +C-enhancement

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions