Skip to content

Commit d14d194

Browse files
committedMay 13, 2017
Support #[allow] etc logic on a per macro level
This commit extends the current unused macro linter to support directives like #[allow(unused_macros)] or #[deny(unused_macros)] directly next to the macro definition, or in one of the modules the macro is inside. Before, we only supported such directives at a per crate level, due to the crate's NodeId being passed to session.add_lint. We also had to implement handling of the macro's NodeId in the lint visitor.
1 parent db82c57 commit d14d194

File tree

6 files changed

+22
-12
lines changed

6 files changed

+22
-12
lines changed
 

‎src/librustc/lint/context.rs

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use hir;
4949
use hir::def_id::LOCAL_CRATE;
5050
use hir::intravisit as hir_visit;
5151
use syntax::visit as ast_visit;
52+
use syntax::tokenstream::ThinTokenStream;
5253

5354
/// Information about the registered lints.
5455
///
@@ -1125,6 +1126,13 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> {
11251126
fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
11261127
run_lints!(self, check_attribute, early_passes, attr);
11271128
}
1129+
1130+
fn visit_mac_def(&mut self, _mac: &'a ThinTokenStream, id: ast::NodeId) {
1131+
let lints = self.sess.lints.borrow_mut().take(id);
1132+
for early_lint in lints {
1133+
self.early_lint(&early_lint);
1134+
}
1135+
}
11281136
}
11291137

11301138
enum CheckLintNameResult {

‎src/librustc_plugin/registry.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ impl<'a> Registry<'a> {
103103
}
104104
self.syntax_exts.push((name, match extension {
105105
NormalTT(ext, _, allow_internal_unstable) => {
106-
NormalTT(ext, Some(self.krate_span), allow_internal_unstable)
106+
let nid = ast::CRATE_NODE_ID;
107+
NormalTT(ext, Some((nid, self.krate_span)), allow_internal_unstable)
107108
}
108109
IdentTT(ext, _, allow_internal_unstable) => {
109110
IdentTT(ext, Some(self.krate_span), allow_internal_unstable)

‎src/librustc_resolve/macros.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,14 @@ impl<'a> base::Resolver for Resolver<'a> {
305305

306306
fn check_unused_macros(&self) {
307307
for (did, _) in self.unused_macros.iter().filter(|&(_, b)| *b) {
308-
let span = match *self.macro_map[did] {
309-
SyntaxExtension::NormalTT(_, sp, _) => sp,
310-
SyntaxExtension::IdentTT(_, sp, _) => sp,
308+
let id_span = match *self.macro_map[did] {
309+
SyntaxExtension::NormalTT(_, isp, _) => isp,
311310
_ => None
312311
};
313-
if let Some(span) = span {
312+
if let Some((id, span)) = id_span {
314313
let lint = lint::builtin::UNUSED_MACROS;
315-
let msg = "unused macro".to_string();
316-
// We are using CRATE_NODE_ID here even though its inaccurate, as we
317-
// sadly don't have the NodeId of the macro definition.
318-
self.session.add_lint(lint, ast::CRATE_NODE_ID, span, msg);
314+
let msg = "unused macro definition".to_string();
315+
self.session.add_lint(lint, id, span, msg);
319316
} else {
320317
bug!("attempted to create unused macro error, but span not available");
321318
}

‎src/libsyntax/ext/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ pub enum SyntaxExtension {
535535
///
536536
/// The `bool` dictates whether the contents of the macro can
537537
/// directly use `#[unstable]` things (true == yes).
538-
NormalTT(Box<TTMacroExpander>, Option<Span>, bool),
538+
NormalTT(Box<TTMacroExpander>, Option<(ast::NodeId, Span)>, bool),
539539

540540
/// A function-like syntax extension that has an extra ident before
541541
/// the block.

‎src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
469469
call_site: span,
470470
callee: NameAndSpan {
471471
format: MacroBang(Symbol::intern(&format!("{}", path))),
472-
span: exp_span,
472+
span: exp_span.map(|(_, s)| s),
473473
allow_internal_unstable: allow_internal_unstable,
474474
},
475475
});

‎src/libsyntax/ext/tt/macro_rules.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ pub fn compile(sess: &ParseSess, features: &RefCell<Features>, def: &ast::Item)
252252
valid: valid,
253253
});
254254

255-
NormalTT(exp, Some(def.span), attr::contains_name(&def.attrs, "allow_internal_unstable"))
255+
NormalTT(
256+
exp,
257+
Some((def.id, def.span)),
258+
attr::contains_name(&def.attrs, "allow_internal_unstable")
259+
)
256260
}
257261

258262
fn check_lhs_nt_follows(sess: &ParseSess,

0 commit comments

Comments
 (0)
Please sign in to comment.