Skip to content

Commit

Permalink
WIP lint reasons with unused-attributes for empty lint attrs
Browse files Browse the repository at this point in the history
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: [] })
  • Loading branch information
zackmdavis committed Oct 16, 2018
1 parent 0ddf461 commit 0c6af84
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ declare_lint! {
"detect assignments that will never be read"
}

declare_lint! {
pub UNUSED_ATTRIBUTES,
Warn,
"detects attributes that were not used by the compiler"
}

declare_lint! {
pub DEAD_CODE,
Warn,
Expand Down
1 change: 1 addition & 0 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ impl<'a> LintContext<'a> for EarlyContext<'a> {
f: F)
where F: FnOnce(&mut Self)
{
info!("ZMD A with_lint_attrs {:?} ", attrs.iter().map(|a| (a.id, a.path.clone())).collect::<Vec<_>>());
let push = self.builder.push(attrs);
self.check_id(id);
self.enter_attrs(attrs);
Expand Down
26 changes: 25 additions & 1 deletion src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use syntax::attr;
use syntax::feature_gate;
use syntax::source_map::MultiSpan;
use syntax::symbol::Symbol;
use syntax_pos::Span;
use util::nodemap::FxHashMap;

pub struct LintLevelSets {
Expand Down Expand Up @@ -196,12 +197,22 @@ impl<'a> LintLevelsBuilder<'a> {
///
/// Don't forget to call `pop`!
pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush {
info!("ZMD B push called with {:?}", attrs);
let mut specs = FxHashMap();
let store = self.sess.lint_store.borrow();
let sess = self.sess;
let bad_attr = |span| {
struct_span_err!(sess, span, E0452, "malformed lint attribute")
};
let empty_attr = |span: Span, specs| {
let lint = builtin::UNUSED_ATTRIBUTES;
let (level, src) = self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
let sp = Some(span.into());
let msg = "empty lint attribute is unused";
info!("ZMD C empty-attr being called on {:?} {:?} {:?}", level, src, sp);
lint::struct_lint_level(sess, lint, level, src, sp, msg)
};

for attr in attrs {
let level = match Level::from_str(&attr.name().as_str()) {
None => continue,
Expand All @@ -216,9 +227,15 @@ impl<'a> LintLevelsBuilder<'a> {
} else {
let mut err = bad_attr(meta.span);
err.emit();
continue
continue;
};

if metas.is_empty() {
let mut err = empty_attr(attr.span, specs.clone());
err.emit();
continue;
}

// Before processing the lint names, look for a reason (RFC 2383)
// at the end.
let mut reason = None;
Expand All @@ -231,6 +248,13 @@ impl<'a> LintLevelsBuilder<'a> {
if item.ident == "reason" {
// found reason, reslice meta list to exclude it
metas = &metas[0..metas.len()-1];
// ... but also notice if we thereby don't have any lint names
// left (attribute was `#[level(reason = "foo")]`)
if metas.is_empty() {
let mut err = empty_attr(attr.span, specs.clone());
err.emit();
continue;
}
if let ast::LitKind::Str(rationale, _) = name_value.node {
if gate_reasons {
feature_gate::emit_feature_err(
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ impl Level {
}

/// How a lint level was set.
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum LintSource {
/// Lint is at the default level as declared
/// in rustc or a plugin.
Expand Down
13 changes: 5 additions & 8 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

use rustc::hir::def::Def;
use rustc::hir::def_id::DefId;

use rustc::ty;
use rustc::ty::adjustment;
use lint::{LateContext, EarlyContext, LintContext, LintArray};
use lint::{LintPass, EarlyLintPass, LateLintPass};
use lint::{
LateContext, EarlyContext, LintContext, LintArray, LintPass, EarlyLintPass, LateLintPass,
builtin::UNUSED_ATTRIBUTES
};

use syntax::ast;
use syntax::attr;
Expand Down Expand Up @@ -195,12 +198,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathStatements {
}
}

declare_lint! {
pub UNUSED_ATTRIBUTES,
Warn,
"detects attributes that were not used by the compiler"
}

#[derive(Copy, Clone)]
pub struct UnusedAttributes;

Expand Down

0 comments on commit 0c6af84

Please sign in to comment.