Skip to content

Commit f633dd3

Browse files
committed
Implement crate level only lints checking.
1 parent 5949391 commit f633dd3

File tree

8 files changed

+148
-11
lines changed

8 files changed

+148
-11
lines changed

src/librustc_lint/early.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
5555
where
5656
F: FnOnce(&mut Self),
5757
{
58-
let push = self.context.builder.push(attrs, &self.context.lint_store);
58+
let is_crate_node = id == ast::CRATE_NODE_ID;
59+
let push = self.context.builder.push(attrs, &self.context.lint_store, is_crate_node);
5960
self.check_id(id);
6061
self.enter_attrs(attrs);
6162
f(self);

src/librustc_lint/levels.rs

+43-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> LintLevelMap {
2929
let mut builder = LintLevelMapBuilder { levels, tcx, store };
3030
let krate = tcx.hir().krate();
3131

32-
let push = builder.levels.push(&krate.item.attrs, &store);
32+
let push = builder.levels.push(&krate.item.attrs, &store, true);
3333
builder.levels.register_id(hir::CRATE_HIR_ID);
3434
for macro_def in krate.exported_macros {
3535
builder.levels.register_id(macro_def.hir_id);
@@ -109,7 +109,12 @@ impl<'s> LintLevelsBuilder<'s> {
109109
/// `#[allow]`
110110
///
111111
/// Don't forget to call `pop`!
112-
pub fn push(&mut self, attrs: &[ast::Attribute], store: &LintStore) -> BuilderPush {
112+
pub fn push(
113+
&mut self,
114+
attrs: &[ast::Attribute],
115+
store: &LintStore,
116+
is_crate_node: bool,
117+
) -> BuilderPush {
113118
let mut specs = FxHashMap::default();
114119
let sess = self.sess;
115120
let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
@@ -333,6 +338,40 @@ impl<'s> LintLevelsBuilder<'s> {
333338
}
334339
}
335340

341+
if !is_crate_node {
342+
for (id, &(level, ref src)) in specs.iter() {
343+
if !id.lint.crate_level_only {
344+
continue;
345+
}
346+
347+
let (lint_attr_name, lint_attr_span) = match *src {
348+
LintSource::Node(name, span, _) => (name, span),
349+
_ => continue,
350+
};
351+
352+
let lint = builtin::UNUSED_ATTRIBUTES;
353+
let (lint_level, lint_src) =
354+
self.sets.get_lint_level(lint, self.cur, Some(&specs), self.sess);
355+
struct_lint_level(
356+
self.sess,
357+
lint,
358+
lint_level,
359+
lint_src,
360+
Some(lint_attr_span.into()),
361+
|lint| {
362+
let mut db = lint.build(&format!(
363+
"{}({}) is ignored unless specified at crate level",
364+
level.as_str(),
365+
lint_attr_name
366+
));
367+
db.emit();
368+
},
369+
);
370+
// don't set a separate error for every lint in the group
371+
break;
372+
}
373+
}
374+
336375
for (id, &(level, ref src)) in specs.iter() {
337376
if level == Level::Forbid {
338377
continue;
@@ -449,7 +488,8 @@ impl LintLevelMapBuilder<'_, '_> {
449488
where
450489
F: FnOnce(&mut Self),
451490
{
452-
let push = self.levels.push(attrs, self.store);
491+
let is_crate_hir = id == hir::CRATE_HIR_ID;
492+
let push = self.levels.push(attrs, self.store, is_crate_hir);
453493
if push.changed {
454494
self.levels.register_id(id);
455495
}

src/librustc_lint/non_ascii_idents.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ use std::ops::Deref;
88
declare_lint! {
99
pub NON_ASCII_IDENTS,
1010
Allow,
11-
"detects non-ASCII identifiers"
11+
"detects non-ASCII identifiers",
12+
crate_level_only
1213
}
1314

1415
declare_lint! {
1516
pub UNCOMMON_CODEPOINTS,
1617
Warn,
17-
"detects uncommon Unicode codepoints in identifiers"
18+
"detects uncommon Unicode codepoints in identifiers",
19+
crate_level_only
1820
}
1921

2022
// FIXME: Change this to warn.
2123
declare_lint! {
2224
pub CONFUSABLE_IDENTS,
2325
Allow,
24-
"detects visually confusable pairs between identifiers"
26+
"detects visually confusable pairs between identifiers",
27+
crate_level_only
2528
}
2629

2730
declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS, UNCOMMON_CODEPOINTS, CONFUSABLE_IDENTS]);

src/librustc_session/lint.rs

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ pub struct Lint {
8888

8989
/// `Some` if this lint is feature gated, otherwise `None`.
9090
pub feature_gate: Option<Symbol>,
91+
92+
pub crate_level_only: bool,
9193
}
9294

9395
/// Extra information for a future incompatibility lint.
@@ -111,6 +113,7 @@ impl Lint {
111113
report_in_external_macro: false,
112114
future_incompatible: None,
113115
feature_gate: None,
116+
crate_level_only: false,
114117
}
115118
}
116119

@@ -336,6 +339,7 @@ macro_rules! declare_tool_lint {
336339
future_incompatible: None,
337340
is_plugin: true,
338341
feature_gate: None,
342+
crate_level_only: false,
339343
};
340344
);
341345
}

src/librustc_session/lint/builtin.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare_lint! {
1717
reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
1818
edition: None,
1919
};
20+
crate_level_only
2021
}
2122

2223
declare_lint! {
@@ -75,7 +76,8 @@ declare_lint! {
7576
declare_lint! {
7677
pub UNUSED_CRATE_DEPENDENCIES,
7778
Allow,
78-
"crate dependencies that are never used"
79+
"crate dependencies that are never used",
80+
crate_level_only
7981
}
8082

8183
declare_lint! {
@@ -166,7 +168,8 @@ declare_lint! {
166168
declare_lint! {
167169
pub UNKNOWN_CRATE_TYPES,
168170
Deny,
169-
"unknown crate type found in `#[crate_type]` directive"
171+
"unknown crate type found in `#[crate_type]` directive",
172+
crate_level_only
170173
}
171174

172175
declare_lint! {
@@ -339,7 +342,8 @@ declare_lint! {
339342
declare_lint! {
340343
pub ELIDED_LIFETIMES_IN_PATHS,
341344
Allow,
342-
"hidden lifetime parameters in types are deprecated"
345+
"hidden lifetime parameters in types are deprecated",
346+
crate_level_only
343347
}
344348

345349
declare_lint! {
@@ -459,6 +463,7 @@ declare_lint! {
459463
reference: "issue #52234 <https://github.com/rust-lang/rust/issues/52234>",
460464
edition: None,
461465
};
466+
crate_level_only
462467
}
463468

464469
declare_lint! {

src/test/ui/issues/issue-48508.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// ignore-asmjs wasm2js does not support source maps yet
1212

1313
#![feature(non_ascii_idents)]
14-
#[allow(uncommon_codepoints)]
14+
#![allow(uncommon_codepoints)]
1515

1616
#[path = "issue-48508-aux.rs"]
1717
mod other_file;
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![deny(uncommon_codepoints, unused_attributes)]
2+
3+
mod foo {
4+
#![allow(uncommon_codepoints)]
5+
//~^ ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
6+
//~| ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
7+
//~| ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
8+
9+
#[allow(uncommon_codepoints)]
10+
//~^ ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
11+
//~| ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
12+
//~| ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
13+
const BAR: f64 = 0.000001;
14+
15+
}
16+
17+
#[allow(uncommon_codepoints)]
18+
//~^ ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
19+
//~| ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
20+
//~| ERROR allow(uncommon_codepoints) is ignored unless specified at crate level [unused_attributes]
21+
fn main() {
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
2+
--> $DIR/crate_level_only_lint.rs:4:10
3+
|
4+
LL | #![allow(uncommon_codepoints)]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/crate_level_only_lint.rs:1:30
9+
|
10+
LL | #![deny(uncommon_codepoints, unused_attributes)]
11+
| ^^^^^^^^^^^^^^^^^
12+
13+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
14+
--> $DIR/crate_level_only_lint.rs:9:9
15+
|
16+
LL | #[allow(uncommon_codepoints)]
17+
| ^^^^^^^^^^^^^^^^^^^
18+
19+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
20+
--> $DIR/crate_level_only_lint.rs:17:9
21+
|
22+
LL | #[allow(uncommon_codepoints)]
23+
| ^^^^^^^^^^^^^^^^^^^
24+
25+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
26+
--> $DIR/crate_level_only_lint.rs:4:10
27+
|
28+
LL | #![allow(uncommon_codepoints)]
29+
| ^^^^^^^^^^^^^^^^^^^
30+
31+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
32+
--> $DIR/crate_level_only_lint.rs:9:9
33+
|
34+
LL | #[allow(uncommon_codepoints)]
35+
| ^^^^^^^^^^^^^^^^^^^
36+
37+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
38+
--> $DIR/crate_level_only_lint.rs:17:9
39+
|
40+
LL | #[allow(uncommon_codepoints)]
41+
| ^^^^^^^^^^^^^^^^^^^
42+
43+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
44+
--> $DIR/crate_level_only_lint.rs:4:10
45+
|
46+
LL | #![allow(uncommon_codepoints)]
47+
| ^^^^^^^^^^^^^^^^^^^
48+
49+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
50+
--> $DIR/crate_level_only_lint.rs:9:9
51+
|
52+
LL | #[allow(uncommon_codepoints)]
53+
| ^^^^^^^^^^^^^^^^^^^
54+
55+
error: allow(uncommon_codepoints) is ignored unless specified at crate level
56+
--> $DIR/crate_level_only_lint.rs:17:9
57+
|
58+
LL | #[allow(uncommon_codepoints)]
59+
| ^^^^^^^^^^^^^^^^^^^
60+
61+
error: aborting due to 9 previous errors
62+

0 commit comments

Comments
 (0)