Skip to content

Commit a8dc246

Browse files
committed
Auto merge of #42334 - est31:master, r=jseyfried
Extend the unused macro lint to macros 2.0 Extends the unused macro lint (added in PR #41907) to macros 2.0 (added in PR #40847). r? @jseyfried
2 parents 4225019 + 03876ec commit a8dc246

File tree

8 files changed

+64
-19
lines changed

8 files changed

+64
-19
lines changed

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ pub struct Resolver<'a> {
12041204
pub found_unresolved_macro: bool,
12051205

12061206
// List of crate local macros that we need to warn about as being unused.
1207-
// Right now this only includes macro_rules! macros.
1207+
// Right now this only includes macro_rules! macros, and macros 2.0.
12081208
unused_macros: FxHashSet<DefId>,
12091209

12101210
// Maps the `Mark` of an expansion to its containing module or block.

src/librustc_resolve/macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ impl<'a> base::Resolver for Resolver<'a> {
316316
for did in self.unused_macros.iter() {
317317
let id_span = match *self.macro_map[did] {
318318
SyntaxExtension::NormalTT(_, isp, _) => isp,
319+
SyntaxExtension::DeclMacro(.., osp) => osp,
319320
_ => None,
320321
};
321322
if let Some((id, span)) = id_span {
@@ -735,6 +736,9 @@ impl<'a> Resolver<'a> {
735736
let module = self.current_module;
736737
let def = Def::Macro(def_id, MacroKind::Bang);
737738
let vis = self.resolve_visibility(&item.vis);
739+
if vis != ty::Visibility::Public {
740+
self.unused_macros.insert(def_id);
741+
}
738742
self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
739743
}
740744
}

src/libsyntax/ext/base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,9 @@ pub enum SyntaxExtension {
552552
BuiltinDerive(BuiltinDeriveFn),
553553

554554
/// A declarative macro, e.g. `macro m() {}`.
555-
DeclMacro(Box<TTMacroExpander>, Option<Span> /* definition site span */),
555+
///
556+
/// The second element is the definition site span.
557+
DeclMacro(Box<TTMacroExpander>, Option<(ast::NodeId, Span)>),
556558
}
557559

558560
impl SyntaxExtension {

src/libsyntax/ext/expand.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
472472

473473
let marked_tts = noop_fold_tts(mac.node.stream(), &mut Marker(mark));
474474
let opt_expanded = match *ext {
475-
SyntaxExtension::DeclMacro(ref expand, def_site_span) => {
476-
if let Err(msg) = validate_and_set_expn_info(def_site_span, false) {
475+
SyntaxExtension::DeclMacro(ref expand, def_span) => {
476+
if let Err(msg) = validate_and_set_expn_info(def_span.map(|(_, s)| s),
477+
false) {
477478
self.cx.span_err(path.span, &msg);
478479
return kind.dummy(span);
479480
}

src/libsyntax/ext/tt/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ pub fn compile(sess: &ParseSess, features: &RefCell<Features>, def: &ast::Item)
266266
let allow_internal_unstable = attr::contains_name(&def.attrs, "allow_internal_unstable");
267267
NormalTT(exp, Some((def.id, def.span)), allow_internal_unstable)
268268
} else {
269-
SyntaxExtension::DeclMacro(exp, Some(def.span))
269+
SyntaxExtension::DeclMacro(exp, Some((def.id, def.span)))
270270
}
271271
}
272272

src/test/compile-fail/feature-gate-decl_macro.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![allow(unused_macros)]
12+
1113
macro m() {} //~ ERROR `macro` is experimental (see issue #39412)
1214
//~| HELP add #![feature(decl_macro)] to the crate attributes to enable
1315

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(unused_macros)]
12+
13+
// Most simple case
14+
macro_rules! unused { //~ ERROR: unused macro definition
15+
() => {};
16+
}
17+
18+
// Test macros created by macros
19+
macro_rules! create_macro {
20+
() => {
21+
macro_rules! m { //~ ERROR: unused macro definition
22+
() => {};
23+
}
24+
};
25+
}
26+
create_macro!();
27+
28+
#[allow(unused_macros)]
29+
mod bar {
30+
// Test that putting the #[deny] close to the macro's definition
31+
// works.
32+
33+
#[deny(unused_macros)]
34+
macro_rules! unused { //~ ERROR: unused macro definition
35+
() => {};
36+
}
37+
}
38+
39+
fn main() {}

src/test/compile-fail/unused-macro.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,28 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(decl_macro)]
1112
#![deny(unused_macros)]
1213

1314
// Most simple case
14-
macro_rules! unused { //~ ERROR: unused macro definition
15-
() => {};
15+
macro unused { //~ ERROR: unused macro definition
16+
() => {}
1617
}
1718

18-
// Test macros created by macros
19-
macro_rules! create_macro {
20-
() => {
21-
macro_rules! m { //~ ERROR: unused macro definition
22-
() => {};
23-
}
24-
};
25-
}
26-
create_macro!();
27-
2819
#[allow(unused_macros)]
2920
mod bar {
3021
// Test that putting the #[deny] close to the macro's definition
3122
// works.
3223

3324
#[deny(unused_macros)]
34-
macro_rules! unused { //~ ERROR: unused macro definition
35-
() => {};
25+
macro unused { //~ ERROR: unused macro definition
26+
() => {}
27+
}
28+
}
29+
30+
mod boo {
31+
pub(crate) macro unused { //~ ERROR: unused macro definition
32+
() => {}
3633
}
3734
}
3835

0 commit comments

Comments
 (0)