Skip to content

Commit 0c6af84

Browse files
committed
WIP lint reasons with unused-attributes for empty lint attrs
I can't figure out why the first empty lint attr. warning is getting emitted twice; I could hack around it with one-time-diagnostics, but I'd rather not duct-tape over my lack of understanding INFO 2018-10-16T05:40:37Z: rustc::lint::levels: ZMD C empty-attr being called on Deny Node(unused_attributes, Span { lo: BytePos(35), hi: BytePos(52), ctxt: #0 }, None) Some(MultiSpan { primary_spans: [Span { lo: BytePos(56), hi: BytePos(66), ctxt: #0 }], span_labels: [] }) INFO 2018-10-16T05:40:37Z: rustc::lint::levels: ZMD C empty-attr being called on Deny Node(unused_attributes, Span { lo: BytePos(35), hi: BytePos(52), ctxt: #0 }, None) Some(MultiSpan { primary_spans: [Span { lo: BytePos(121), hi: BytePos(167), ctxt: #0 }], span_labels: [] }) INFO 2018-10-16T05:40:37Z: rustc::lint::levels: ZMD C empty-attr being called on Deny Node(unused_attributes, Span { lo: BytePos(35), hi: BytePos(52), ctxt: #0 }, None) Some(MultiSpan { primary_spans: [Span { lo: BytePos(222), hi: BytePos(231), ctxt: #0 }], span_labels: [] }) INFO 2018-10-16T05:40:37Z: rustc::lint::levels: ZMD C empty-attr being called on Deny Node(unused_attributes, Span { lo: BytePos(35), hi: BytePos(52), ctxt: #0 }, None) Some(MultiSpan { primary_spans: [Span { lo: BytePos(56), hi: BytePos(66), ctxt: #0 }], span_labels: [] }) INFO 2018-10-16T05:40:37Z: rustc::lint::levels: ZMD C empty-attr being called on Deny Node(unused_attributes, Span { lo: BytePos(35), hi: BytePos(52), ctxt: #0 }, None) Some(MultiSpan { primary_spans: [Span { lo: BytePos(121), hi: BytePos(167), ctxt: #0 }], span_labels: [] }) INFO 2018-10-16T05:40:37Z: rustc::lint::levels: ZMD C empty-attr being called on Deny Node(unused_attributes, Span { lo: BytePos(35), hi: BytePos(52), ctxt: #0 }, None) Some(MultiSpan { primary_spans: [Span { lo: BytePos(222), hi: BytePos(231), ctxt: #0 }], span_labels: [] })
1 parent 0ddf461 commit 0c6af84

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

src/librustc/lint/builtin.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ declare_lint! {
6868
"detect assignments that will never be read"
6969
}
7070

71+
declare_lint! {
72+
pub UNUSED_ATTRIBUTES,
73+
Warn,
74+
"detects attributes that were not used by the compiler"
75+
}
76+
7177
declare_lint! {
7278
pub DEAD_CODE,
7379
Warn,

src/librustc/lint/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ impl<'a> LintContext<'a> for EarlyContext<'a> {
707707
f: F)
708708
where F: FnOnce(&mut Self)
709709
{
710+
info!("ZMD A with_lint_attrs {:?} ", attrs.iter().map(|a| (a.id, a.path.clone())).collect::<Vec<_>>());
710711
let push = self.builder.push(attrs);
711712
self.check_id(id);
712713
self.enter_attrs(attrs);

src/librustc/lint/levels.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use syntax::attr;
2424
use syntax::feature_gate;
2525
use syntax::source_map::MultiSpan;
2626
use syntax::symbol::Symbol;
27+
use syntax_pos::Span;
2728
use util::nodemap::FxHashMap;
2829

2930
pub struct LintLevelSets {
@@ -196,12 +197,22 @@ impl<'a> LintLevelsBuilder<'a> {
196197
///
197198
/// Don't forget to call `pop`!
198199
pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush {
200+
info!("ZMD B push called with {:?}", attrs);
199201
let mut specs = FxHashMap();
200202
let store = self.sess.lint_store.borrow();
201203
let sess = self.sess;
202204
let bad_attr = |span| {
203205
struct_span_err!(sess, span, E0452, "malformed lint attribute")
204206
};
207+
let empty_attr = |span: Span, specs| {
208+
let lint = builtin::UNUSED_ATTRIBUTES;
209+
let (level, src) = self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
210+
let sp = Some(span.into());
211+
let msg = "empty lint attribute is unused";
212+
info!("ZMD C empty-attr being called on {:?} {:?} {:?}", level, src, sp);
213+
lint::struct_lint_level(sess, lint, level, src, sp, msg)
214+
};
215+
205216
for attr in attrs {
206217
let level = match Level::from_str(&attr.name().as_str()) {
207218
None => continue,
@@ -216,9 +227,15 @@ impl<'a> LintLevelsBuilder<'a> {
216227
} else {
217228
let mut err = bad_attr(meta.span);
218229
err.emit();
219-
continue
230+
continue;
220231
};
221232

233+
if metas.is_empty() {
234+
let mut err = empty_attr(attr.span, specs.clone());
235+
err.emit();
236+
continue;
237+
}
238+
222239
// Before processing the lint names, look for a reason (RFC 2383)
223240
// at the end.
224241
let mut reason = None;
@@ -231,6 +248,13 @@ impl<'a> LintLevelsBuilder<'a> {
231248
if item.ident == "reason" {
232249
// found reason, reslice meta list to exclude it
233250
metas = &metas[0..metas.len()-1];
251+
// ... but also notice if we thereby don't have any lint names
252+
// left (attribute was `#[level(reason = "foo")]`)
253+
if metas.is_empty() {
254+
let mut err = empty_attr(attr.span, specs.clone());
255+
err.emit();
256+
continue;
257+
}
234258
if let ast::LitKind::Str(rationale, _) = name_value.node {
235259
if gate_reasons {
236260
feature_gate::emit_feature_err(

src/librustc/lint/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ impl Level {
463463
}
464464

465465
/// How a lint level was set.
466-
#[derive(Clone, Copy, PartialEq, Eq)]
466+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
467467
pub enum LintSource {
468468
/// Lint is at the default level as declared
469469
/// in rustc or a plugin.

src/librustc_lint/unused.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010

1111
use rustc::hir::def::Def;
1212
use rustc::hir::def_id::DefId;
13+
1314
use rustc::ty;
1415
use rustc::ty::adjustment;
15-
use lint::{LateContext, EarlyContext, LintContext, LintArray};
16-
use lint::{LintPass, EarlyLintPass, LateLintPass};
16+
use lint::{
17+
LateContext, EarlyContext, LintContext, LintArray, LintPass, EarlyLintPass, LateLintPass,
18+
builtin::UNUSED_ATTRIBUTES
19+
};
1720

1821
use syntax::ast;
1922
use syntax::attr;
@@ -195,12 +198,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathStatements {
195198
}
196199
}
197200

198-
declare_lint! {
199-
pub UNUSED_ATTRIBUTES,
200-
Warn,
201-
"detects attributes that were not used by the compiler"
202-
}
203-
204201
#[derive(Copy, Clone)]
205202
pub struct UnusedAttributes;
206203

0 commit comments

Comments
 (0)