Skip to content

Commit 864c501

Browse files
committed
Get "make check" to work with unused-attribute
There's a fair number of attributes that have to be whitelisted since they're either looked for by rustdoc, in trans, or as needed. These can be cleaned up in the future.
1 parent 24f98c6 commit 864c501

19 files changed

+102
-64
lines changed

src/doc/rust.md

+1
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ Attributes on the anonymous crate module define important metadata that influenc
661661
the behavior of the compiler.
662662

663663
~~~~ {.rust}
664+
# #![allow(unused_attribute)]
664665
// Crate ID
665666
#![crate_id = "projx#2.5"]
666667

src/doc/rustdoc.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Documenting Rust APIs is quite simple. To document a given item, we have "doc
1111
comments":
1212

1313
~~~
14+
# #![allow(unused_attribute)]
1415
// the "link" crate attribute is currently required for rustdoc, but normally
1516
// isn't needed.
1617
#![crate_id = "universe"]

src/doc/tutorial.md

+5
Original file line numberDiff line numberDiff line change
@@ -3166,6 +3166,7 @@ without conflict.
31663166
Therefore, if you plan to compile your crate as a library, you should annotate it with that information:
31673167

31683168
~~~~
3169+
# #![allow(unused_attribute)]
31693170
// `lib.rs`
31703171
31713172
# #![crate_type = "lib"]
@@ -3189,6 +3190,7 @@ Other crate settings and metadata include things like enabling/disabling certain
31893190
or setting the crate type (library or executable) explicitly:
31903191

31913192
~~~~
3193+
# #![allow(unused_attribute)]
31923194
// `lib.rs`
31933195
// ...
31943196
@@ -3208,6 +3210,7 @@ Now for something that you can actually compile yourself.
32083210
We define two crates, and use one of them as a library in the other.
32093211

32103212
~~~~
3213+
# #![allow(unused_attribute)]
32113214
// `world.rs`
32123215
#![crate_id = "world#0.42"]
32133216
@@ -3282,11 +3285,13 @@ fn main() {
32823285
Both auto-insertions can be disabled with an attribute if necessary:
32833286

32843287
~~~
3288+
# #![allow(unused_attribute)]
32853289
// In the crate root:
32863290
#![no_std]
32873291
~~~
32883292

32893293
~~~
3294+
# #![allow(unused_attribute)]
32903295
// In any module:
32913296
#![no_implicit_prelude]
32923297
~~~

src/librustc/driver/driver.rs

+40-38
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,45 @@ fn print_flowgraph<W:io::Writer>(analysis: CrateAnalysis,
716716

717717
pub fn collect_crate_types(session: &Session,
718718
attrs: &[ast::Attribute]) -> Vec<config::CrateType> {
719+
// Unconditionally collect crate types from attributes to make them used
720+
let attr_types: Vec<config::CrateType> = attrs.iter().filter_map(|a| {
721+
if a.check_name("crate_type") {
722+
match a.value_str() {
723+
Some(ref n) if n.equiv(&("rlib")) => {
724+
Some(config::CrateTypeRlib)
725+
}
726+
Some(ref n) if n.equiv(&("dylib")) => {
727+
Some(config::CrateTypeDylib)
728+
}
729+
Some(ref n) if n.equiv(&("lib")) => {
730+
Some(config::default_lib_output())
731+
}
732+
Some(ref n) if n.equiv(&("staticlib")) => {
733+
Some(config::CrateTypeStaticlib)
734+
}
735+
Some(ref n) if n.equiv(&("bin")) => Some(config::CrateTypeExecutable),
736+
Some(_) => {
737+
session.add_lint(lint::UnknownCrateType,
738+
ast::CRATE_NODE_ID,
739+
a.span,
740+
"invalid `crate_type` \
741+
value".to_strbuf());
742+
None
743+
}
744+
_ => {
745+
session.add_lint(lint::UnknownCrateType,
746+
ast::CRATE_NODE_ID,
747+
a.span,
748+
"`crate_type` requires a \
749+
value".to_strbuf());
750+
None
751+
}
752+
}
753+
} else {
754+
None
755+
}
756+
}).collect();
757+
719758
// If we're generating a test executable, then ignore all other output
720759
// styles at all other locations
721760
if session.opts.test {
@@ -729,44 +768,7 @@ pub fn collect_crate_types(session: &Session,
729768
if base.len() > 0 {
730769
return base
731770
} else {
732-
let iter = attrs.iter().filter_map(|a| {
733-
if a.name().equiv(&("crate_type")) {
734-
match a.value_str() {
735-
Some(ref n) if n.equiv(&("rlib")) => {
736-
Some(config::CrateTypeRlib)
737-
}
738-
Some(ref n) if n.equiv(&("dylib")) => {
739-
Some(config::CrateTypeDylib)
740-
}
741-
Some(ref n) if n.equiv(&("lib")) => {
742-
Some(config::default_lib_output())
743-
}
744-
Some(ref n) if n.equiv(&("staticlib")) => {
745-
Some(config::CrateTypeStaticlib)
746-
}
747-
Some(ref n) if n.equiv(&("bin")) => Some(config::CrateTypeExecutable),
748-
Some(_) => {
749-
session.add_lint(lint::UnknownCrateType,
750-
ast::CRATE_NODE_ID,
751-
a.span,
752-
"invalid `crate_type` \
753-
value".to_strbuf());
754-
None
755-
}
756-
_ => {
757-
session.add_lint(lint::UnknownCrateType,
758-
ast::CRATE_NODE_ID,
759-
a.span,
760-
"`crate_type` requires a \
761-
value".to_strbuf());
762-
None
763-
}
764-
}
765-
} else {
766-
None
767-
}
768-
});
769-
base.extend(iter);
771+
base.extend(attr_types.move_iter());
770772
if base.len() == 0 {
771773
base.push(config::CrateTypeExecutable);
772774
}

src/librustc/front/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ fn is_bench_fn(cx: &TestCtxt, i: @ast::Item) -> bool {
253253
fn is_ignored(cx: &TestCtxt, i: @ast::Item) -> bool {
254254
i.attrs.iter().any(|attr| {
255255
// check ignore(cfg(foo, bar))
256-
attr.name().equiv(&("ignore")) && match attr.meta_item_list() {
256+
attr.check_name("ignore") && match attr.meta_item_list() {
257257
Some(ref cfgs) => {
258258
attr::test_cfg(cx.config.as_slice(), cfgs.iter().map(|x| *x))
259259
}

src/librustc/metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ fn synthesize_crate_attrs(ecx: &EncodeContext,
14471447

14481448
let mut attrs = Vec::new();
14491449
for attr in krate.attrs.iter() {
1450-
if !attr.name().equiv(&("crate_id")) {
1450+
if !attr.check_name("crate_id") {
14511451
attrs.push(*attr);
14521452
}
14531453
}

src/librustc/middle/lint.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
293293
LintSpec {
294294
lint: UnusedAttribute,
295295
desc: "detects attributes that were not used by the compiler",
296-
default: Allow
296+
default: Warn
297297
}),
298298

299299
("unused_variable",
@@ -1148,8 +1148,38 @@ fn check_attrs_usage(cx: &Context, attrs: &[ast::Attribute]) {
11481148
fn check_unused_attribute(cx: &Context, attrs: &[ast::Attribute]) {
11491149
for attr in attrs.iter() {
11501150
// whitelist docs since rustdoc looks at them
1151+
attr.check_name("automatically_derived");
11511152
attr.check_name("doc");
11521153

1154+
// these are processed in trans, which happens after the lint pass
1155+
attr.check_name("address_insignificant");
1156+
attr.check_name("cold");
1157+
attr.check_name("inline");
1158+
attr.check_name("link");
1159+
attr.check_name("link_name");
1160+
attr.check_name("link_section");
1161+
attr.check_name("no_builtins");
1162+
attr.check_name("no_mangle");
1163+
attr.check_name("no_split_stack");
1164+
attr.check_name("packed");
1165+
attr.check_name("static_assert");
1166+
attr.check_name("thread_local");
1167+
1168+
// not used anywhere (!?) but apparently we want to keep them around
1169+
attr.check_name("comment");
1170+
attr.check_name("desc");
1171+
attr.check_name("license");
1172+
1173+
// these are only looked at on-demand so we can't guarantee they'll have
1174+
// already been checked
1175+
attr.check_name("deprecated");
1176+
attr.check_name("experimental");
1177+
attr.check_name("frozen");
1178+
attr.check_name("locked");
1179+
attr.check_name("must_use");
1180+
attr.check_name("stable");
1181+
attr.check_name("unstable");
1182+
11531183
if !attr::is_used(attr) {
11541184
cx.span_lint(UnusedAttribute, attr.span, "unused attribute");
11551185
}

src/librustdoc/clean.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,17 @@ impl Clean<Attribute> for ast::Attribute {
314314
}
315315

316316
// This is a rough approximation that gets us what we want.
317-
impl<'a> attr::AttrMetaMethods for &'a Attribute {
317+
impl attr::AttrMetaMethods for Attribute {
318318
fn name(&self) -> InternedString {
319-
match **self {
319+
match *self {
320320
Word(ref n) | List(ref n, _) | NameValue(ref n, _) => {
321321
token::intern_and_get_ident(n.as_slice())
322322
}
323323
}
324324
}
325325

326326
fn value_str(&self) -> Option<InternedString> {
327-
match **self {
327+
match *self {
328328
NameValue(_, ref v) => {
329329
Some(token::intern_and_get_ident(v.as_slice()))
330330
}

src/libsyntax/attr.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>>
350350

351351
// this would be much nicer as a chain of iterator adaptors, but
352352
// this doesn't work.
353-
let some_cfg_matches = metas.any(|mi| {
353+
let some_cfg_matches = metas.fold(false, |matches, mi| {
354354
debug!("testing name: {}", mi.name());
355-
if mi.check_name("cfg") { // it is a #[cfg()] attribute
355+
let this_matches = if mi.check_name("cfg") { // it is a #[cfg()] attribute
356356
debug!("is cfg");
357357
no_cfgs = false;
358358
// only #[cfg(...)] ones are understood.
@@ -380,7 +380,8 @@ pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>>
380380
}
381381
} else {
382382
false
383-
}
383+
};
384+
matches || this_matches
384385
});
385386
debug!("test_cfg (no_cfgs={}, some_cfg_matches={})", no_cfgs, some_cfg_matches);
386387
no_cfgs || some_cfg_matches

src/test/compile-fail/lint-misplaced-attr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
// injected intrinsics by the compiler.
1313

1414
#![deny(attribute_usage)]
15+
#![deny(unused_attribute)]
1516

1617
mod a {
1718
#![crate_type = "bin"] //~ ERROR: crate-level attribute
19+
//~^ ERROR: unused attribute
1820
}
1921

2022
#[crate_type = "bin"] fn main() {} //~ ERROR: crate-level attribute
23+
//~^ ERROR: unused attribute

src/test/compile-fail/lint-obsolete-attr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
// injected intrinsics by the compiler.
1313

1414
#![deny(attribute_usage)]
15+
#![deny(unused_attribute)]
1516
#![allow(dead_code)]
1617

1718
#[abi="stdcall"] extern {} //~ ERROR: obsolete attribute
19+
//~^ ERROR: unused attribute
1820

1921
#[fixed_stack_segment] fn f() {} //~ ERROR: obsolete attribute
22+
//~^ ERROR: unused attribute
2023

2124
fn main() {}

src/test/compile-fail/lint-unknown-attr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
// injected intrinsics by the compiler.
1313

1414
#![deny(attribute_usage)]
15+
#![deny(unused_attribute)]
1516

1617
#![mutable_doc] //~ ERROR: unknown crate attribute
18+
//~^ ERROR: unused attribute
1719

1820
#[dance] mod a {} //~ ERROR: unknown attribute
21+
//~^ ERROR: unused attribute
1922

2023
#[dance] fn main() {} //~ ERROR: unknown attribute
24+
//~^ ERROR: unused attribute

src/test/run-pass/attr-mix-new.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10+
#![allow(unused_attribute)]
1011

1112
#[foo(bar)]
1213
mod foo {

src/test/run-pass/backtrace.rs

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
// except according to those terms.
1010

1111
// ignore-win32 FIXME #13259
12-
#![no_uv]
13-
1412
extern crate native;
1513

1614
use std::os;

src/test/run-pass/class-attributes-1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// pp-exact - Make sure we actually print the attributes
12+
#![allow(unused_attribute)]
1213

1314
struct cat {
1415
name: StrBuf,

src/test/run-pass/class-attributes-2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10+
#![allow(unused_attribute)]
1011

1112
struct cat {
1213
name: StrBuf,

src/test/run-pass/issue-3250.rs

-15
This file was deleted.

src/test/run-pass/item-attributes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// These are attributes of the implicit crate. Really this just needs to parse
1212
// for completeness since .rs files linked from .rc files support this
1313
// notation to specify their module's attributes
14+
#![allow(unused_attribute)]
1415
#![attr1 = "val"]
1516
#![attr2 = "val"]
1617
#![attr3]

src/test/run-pass/method-attributes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// pp-exact - Make sure we print all the attributes
12+
#![allow(unused_attribute)]
1213

1314
#[frobable]
1415
trait frobable {

0 commit comments

Comments
 (0)