Skip to content

Commit 862cd65

Browse files
committed
Add back hint for crate level attributes
1 parent 42a18bd commit 862cd65

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/librustc/middle/lint.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,20 @@ fn check_unused_attribute(cx: &Context, attr: &ast::Attribute) {
11241124
"unstable",
11251125
];
11261126

1127+
static CRATE_ATTRS: &'static [&'static str] = &'static [
1128+
"crate_type",
1129+
"feature",
1130+
"no_start",
1131+
"no_main",
1132+
"no_std",
1133+
"crate_id",
1134+
"desc",
1135+
"comment",
1136+
"license",
1137+
"copyright",
1138+
"no_builtins",
1139+
];
1140+
11271141
for &name in ATTRIBUTE_WHITELIST.iter() {
11281142
if attr.check_name(name) {
11291143
break;
@@ -1132,6 +1146,15 @@ fn check_unused_attribute(cx: &Context, attr: &ast::Attribute) {
11321146

11331147
if !attr::is_used(attr) {
11341148
cx.span_lint(UnusedAttribute, attr.span, "unused attribute");
1149+
if CRATE_ATTRS.contains(&attr.name().get()) {
1150+
let msg = match attr.node.style {
1151+
ast::AttrOuter => "crate-level attribute should be an inner \
1152+
attribute: add an exclamation mark: #![foo]",
1153+
ast::AttrInner => "crate-level attribute should be in the \
1154+
root module",
1155+
};
1156+
cx.span_lint(UnusedAttribute, attr.span, msg);
1157+
}
11351158
}
11361159
}
11371160

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
mod a {
1717
#![crate_type = "bin"] //~ ERROR unused attribute
18+
//~^ ERROR should be in the root module
1819
}
1920

2021
#[crate_type = "bin"] fn main() {} //~ ERROR unused attribute
22+
//~^ ERROR should be an inner

0 commit comments

Comments
 (0)