Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a reason="foo" in lint attributes #9025

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub enum lint {
while_true,
path_statement,
unrecognized_lint,
no_lint_reason,
non_camel_case_types,
non_uppercase_statics,
type_limits,
Expand Down Expand Up @@ -123,7 +124,7 @@ pub enum level {
pub struct LintSpec {
lint: lint,
desc: &'static str,
default: level
default: level,
}

impl Ord for LintSpec {
Expand Down Expand Up @@ -195,6 +196,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
default: warn
}),

("no_lint_reason",
LintSpec {
lint: no_lint_reason,
desc: "lint setting without a reason",
default: allow,
}),

("non_camel_case_types",
LintSpec {
lint: non_camel_case_types,
Expand Down Expand Up @@ -479,7 +487,7 @@ impl Context {
// of what we changed so we can roll everything back after invoking the
// specified closure
let mut pushed = 0u;
do each_lint(self.tcx.sess, attrs) |meta, level, lintname| {
do each_lint(self.tcx.sess, attrs, self) |meta, level, lintname| {
match self.dict.find_equiv(&lintname) {
None => {
self.span_lint(
Expand All @@ -489,6 +497,8 @@ impl Context {
level_to_str(level), lintname));
}
Some(lint) => {
error!(meta);
error!(attrs);
let lint = lint.lint;
let now = self.get_level(lint);
if now == forbid && level != forbid {
Expand Down Expand Up @@ -605,16 +615,18 @@ impl Context {

pub fn each_lint(sess: session::Session,
attrs: &[ast::Attribute],
cx: @mut Context,
f: &fn(@ast::MetaItem, level, @str) -> bool) -> bool {
let xs = [allow, warn, deny, forbid];
for &level in xs.iter() {
let level_name = level_to_str(level);
for attr in attrs.iter().filter(|m| level_name == m.name()) {
let mut found_reason = false;
let meta = attr.node.value;
let metas = match meta.node {
ast::MetaList(_, ref metas) => metas,
_ => {
sess.span_err(meta.span, "malformed lint attribute");
sess.span_err(meta.span, "malformed lint attribute: not a list");
loop;
}
};
Expand All @@ -624,14 +636,26 @@ pub fn each_lint(sess: session::Session,
if !f(*meta, level, lintname) {
return false;
}
}
},
ast::MetaNameValue(n, _v) => {
if "reason" != n {
sess.span_err(meta.span,
"malformed lint attribute: unrecognized name/value pair");
} else {
found_reason = true;
}
},
_ => {
sess.span_err(meta.span, "malformed lint attribute");
sess.span_err(meta.span, "malformed lint attribute: not a word");
}
}
}
if !found_reason {
cx.span_lint(no_lint_reason, meta.span, "lint attribute without reason");
}
}
}

true
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/lint-reason.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[deny(no_lint_reason, reason="why not?")];
#[deny(heap_memory)]; //~ ERROR lint attribute without reason

fn main() { }
8 changes: 8 additions & 0 deletions src/test/run-pass/lint-reason.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[deny(no_lint_reason, reason="")];

#[allow(no_lint_reason, reason="")]
mod foo {
#[warn(unused_unsafe)];
}

fn main() { }