Skip to content

Commit 9f6ca74

Browse files
committed
Shorten Span of unused macro lints
The span has been recuded to the actual ident, instead of linting the *whole* macro.
1 parent 9dbbbb1 commit 9f6ca74

File tree

7 files changed

+41
-62
lines changed

7 files changed

+41
-62
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1194,15 +1194,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11941194
// Mark the given macro as unused unless its name starts with `_`.
11951195
// Macro uses will remove items from this set, and the remaining
11961196
// items will be reported as `unused_macros`.
1197-
fn insert_unused_macro(
1198-
&mut self,
1199-
ident: Ident,
1200-
def_id: LocalDefId,
1201-
node_id: NodeId,
1202-
span: Span,
1203-
) {
1197+
fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) {
12041198
if !ident.as_str().starts_with('_') {
1205-
self.r.unused_macros.insert(def_id, (node_id, span));
1199+
self.r.unused_macros.insert(def_id, (node_id, ident));
12061200
}
12071201
}
12081202

@@ -1246,7 +1240,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
12461240
self.r.define(module, ident, MacroNS, (res, vis, span, expansion, IsMacroExport));
12471241
} else {
12481242
self.r.check_reserved_macro_name(ident, res);
1249-
self.insert_unused_macro(ident, def_id, item.id, span);
1243+
self.insert_unused_macro(ident, def_id, item.id);
12501244
}
12511245
self.r.visibilities.insert(def_id, vis);
12521246
self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
@@ -1267,7 +1261,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
12671261
_ => self.resolve_visibility(&item.vis),
12681262
};
12691263
if vis != ty::Visibility::Public {
1270-
self.insert_unused_macro(ident, def_id, item.id, span);
1264+
self.insert_unused_macro(ident, def_id, item.id);
12711265
}
12721266
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
12731267
self.r.visibilities.insert(def_id, vis);

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ pub struct Resolver<'a> {
988988
non_macro_attr: Lrc<SyntaxExtension>,
989989
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'a>>,
990990
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'a>>,
991-
unused_macros: FxHashMap<LocalDefId, (NodeId, Span)>,
991+
unused_macros: FxHashMap<LocalDefId, (NodeId, Ident)>,
992992
proc_macro_stubs: FxHashSet<LocalDefId>,
993993
/// Traces collected during macro resolution and validated when it's complete.
994994
single_segment_macro_resolutions:

compiler/rustc_resolve/src/macros.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,13 @@ impl<'a> ResolverExpand for Resolver<'a> {
315315
}
316316

317317
fn check_unused_macros(&mut self) {
318-
for (_, &(node_id, span)) in self.unused_macros.iter() {
319-
self.lint_buffer.buffer_lint(UNUSED_MACROS, node_id, span, "unused macro definition");
318+
for (_, &(node_id, ident)) in self.unused_macros.iter() {
319+
self.lint_buffer.buffer_lint(
320+
UNUSED_MACROS,
321+
node_id,
322+
ident.span,
323+
&format!("unused macro definition: `{}`", ident.as_str()),
324+
);
320325
}
321326
}
322327

src/test/ui/lint/unused/issue-70041.stderr

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
warning: unused macro definition
2-
--> $DIR/issue-70041.rs:4:1
1+
warning: unused macro definition: `regex`
2+
--> $DIR/issue-70041.rs:4:14
33
|
4-
LL | / macro_rules! regex {
5-
LL | |
6-
LL | | () => {};
7-
LL | | }
8-
| |_^
4+
LL | macro_rules! regex {
5+
| ^^^^^
96
|
107
= note: `#[warn(unused_macros)]` on by default
118

src/test/ui/lint/unused/unused-macro-rules.stderr

+12-23
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
1-
error: unused macro definition
2-
--> $DIR/unused-macro-rules.rs:4:1
1+
error: unused macro definition: `unused`
2+
--> $DIR/unused-macro-rules.rs:4:14
33
|
4-
LL | / macro_rules! unused {
5-
LL | | () => {};
6-
LL | | }
7-
| |_^
4+
LL | macro_rules! unused {
5+
| ^^^^^^
86
|
97
note: the lint level is defined here
108
--> $DIR/unused-macro-rules.rs:1:9
119
|
1210
LL | #![deny(unused_macros)]
1311
| ^^^^^^^^^^^^^
1412

15-
error: unused macro definition
16-
--> $DIR/unused-macro-rules.rs:11:9
13+
error: unused macro definition: `m`
14+
--> $DIR/unused-macro-rules.rs:11:22
1715
|
18-
LL | / macro_rules! m {
19-
LL | | () => {};
20-
LL | | }
21-
| |_________^
22-
...
23-
LL | create_macro!();
24-
| --------------- in this macro invocation
25-
|
26-
= note: this error originates in the macro `create_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
LL | macro_rules! m {
17+
| ^
2718

28-
error: unused macro definition
29-
--> $DIR/unused-macro-rules.rs:24:5
19+
error: unused macro definition: `unused`
20+
--> $DIR/unused-macro-rules.rs:24:18
3021
|
31-
LL | / macro_rules! unused {
32-
LL | | () => {};
33-
LL | | }
34-
| |_____^
22+
LL | macro_rules! unused {
23+
| ^^^^^^
3524
|
3625
note: the lint level is defined here
3726
--> $DIR/unused-macro-rules.rs:23:12
+12-18
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
1-
error: unused macro definition
2-
--> $DIR/unused-macro.rs:5:1
1+
error: unused macro definition: `unused`
2+
--> $DIR/unused-macro.rs:5:7
33
|
4-
LL | / macro unused {
5-
LL | | () => {}
6-
LL | | }
7-
| |_^
4+
LL | macro unused {
5+
| ^^^^^^
86
|
97
note: the lint level is defined here
108
--> $DIR/unused-macro.rs:2:9
119
|
1210
LL | #![deny(unused_macros)]
1311
| ^^^^^^^^^^^^^
1412

15-
error: unused macro definition
16-
--> $DIR/unused-macro.rs:15:5
13+
error: unused macro definition: `unused`
14+
--> $DIR/unused-macro.rs:15:11
1715
|
18-
LL | / macro unused {
19-
LL | | () => {}
20-
LL | | }
21-
| |_____^
16+
LL | macro unused {
17+
| ^^^^^^
2218
|
2319
note: the lint level is defined here
2420
--> $DIR/unused-macro.rs:14:12
2521
|
2622
LL | #[deny(unused_macros)]
2723
| ^^^^^^^^^^^^^
2824

29-
error: unused macro definition
30-
--> $DIR/unused-macro.rs:21:5
25+
error: unused macro definition: `unused`
26+
--> $DIR/unused-macro.rs:21:22
3127
|
32-
LL | / pub(crate) macro unused {
33-
LL | | () => {}
34-
LL | | }
35-
| |_____^
28+
LL | pub(crate) macro unused {
29+
| ^^^^^^
3630

3731
error: aborting due to 3 previous errors
3832

src/test/ui/proc-macro/issue-39889.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-pass
22

3-
#![allow(dead_code)]
3+
#![allow(dead_code, unused_macros)]
44
// aux-build:issue-39889.rs
55

66
extern crate issue_39889;

0 commit comments

Comments
 (0)