Skip to content

Commit 43dc430

Browse files
committed
Test expect with forbid and fix doc errors (RFC-2383)
* Add test to expect and the forbid a lint (RFC 2383)
1 parent aa2a0a8 commit 43dc430

File tree

5 files changed

+124
-13
lines changed

5 files changed

+124
-13
lines changed

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,17 @@ pub enum Applicability {
5050
Unspecified,
5151
}
5252

53-
/// Each lint expectation has a `LintExpectationId` assigned by the
54-
/// [`LintLevelsBuilder`][`rustc_lint::levels::LintLevelsBuilder`]. Expected
55-
/// [`Diagnostic`][`rustc_errors::Diagnostic`]s get the lint level `Expect` which
56-
/// stores the `LintExpectationId` to match it with the actual expectation later on.
53+
/// Each lint expectation has a `LintExpectationId` assigned by the `LintLevelsBuilder`.
54+
/// Expected `Diagnostic`s get the lint level `Expect` which stores the `LintExpectationId`
55+
/// to match it with the actual expectation later on.
5756
///
5857
/// The `LintExpectationId` has to be has stable between compilations, as diagnostic
5958
/// instances might be loaded from cache. Lint messages can be emitted during an
6059
/// `EarlyLintPass` operating on the AST and during a `LateLintPass` traversing the
6160
/// HIR tree. The AST doesn't have enough information to create a stable id. The
6261
/// `LintExpectationId` will instead store the [`AttrId`] defining the expectation.
6362
/// These `LintExpectationId` will be updated to use the stable [`HirId`] once the
64-
/// AST has been lowered. The transformation is done by the
65-
/// [`LintLevelsBuilder`][`rustc_lint::levels::LintLevelsBuilder`]
63+
/// AST has been lowered. The transformation is done by the `LintLevelsBuilder`
6664
///
6765
/// Each lint inside the `expect` attribute is tracked individually, the `lint_index`
6866
/// identifies the lint inside the attribute and ensures that the IDs are unique.
@@ -135,7 +133,7 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation
135133

136134
/// Setting for how to handle a lint.
137135
///
138-
/// See: https://doc.rust-lang.org/rustc/lints/levels.html
136+
/// See: <https://doc.rust-lang.org/rustc/lints/levels.html>
139137
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
140138
pub enum Level {
141139
/// The `allow` level will not issue any message.

src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// check-pass
21
// ignore-tidy-linelength
32

43
#![feature(lint_reasons)]
@@ -37,7 +36,18 @@ mod oof {
3736
let mut v = 0;
3837
//~^ WARNING variable does not need to be mutable [unused_mut]
3938
//~| NOTE this overrides the previous `expect` lint level and warns about the `unused_mut` lint here
39+
//~| HELP remove this `mut`
4040
}
4141
}
4242

43+
#[expect(unused_variables)]
44+
//~^ WARNING this lint expectation is unfulfilled
45+
#[forbid(unused_variables)]
46+
//~^ NOTE the lint level is defined here
47+
fn check_expect_then_forbid() {
48+
let this_is_my_function = 3;
49+
//~^ ERROR unused variable: `this_is_my_function` [unused_variables]
50+
//~| HELP if this is intentional, prefix it with an underscore
51+
}
52+
4353
fn main() {}
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
error: unused variable: `this_is_my_function`
2+
--> $DIR/expect_nested_lint_levels.rs:48:9
3+
|
4+
LL | let this_is_my_function = 3;
5+
| ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_this_is_my_function`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/expect_nested_lint_levels.rs:45:10
9+
|
10+
LL | #[forbid(unused_variables)]
11+
| ^^^^^^^^^^^^^^^^
12+
113
warning: variable does not need to be mutable
2-
--> $DIR/expect_nested_lint_levels.rs:37:13
14+
--> $DIR/expect_nested_lint_levels.rs:36:13
315
|
416
LL | let mut v = 0;
517
| ----^
@@ -8,13 +20,13 @@ LL | let mut v = 0;
820
|
921
= note: this overrides the previous `expect` lint level and warns about the `unused_mut` lint here
1022
note: the lint level is defined here
11-
--> $DIR/expect_nested_lint_levels.rs:32:9
23+
--> $DIR/expect_nested_lint_levels.rs:31:9
1224
|
1325
LL | unused_mut,
1426
| ^^^^^^^^^^
1527

1628
warning: this lint expectation is unfulfilled
17-
--> $DIR/expect_nested_lint_levels.rs:24:5
29+
--> $DIR/expect_nested_lint_levels.rs:23:5
1830
|
1931
LL | unused_mut,
2032
| ^^^^^^^^^^
@@ -23,12 +35,18 @@ LL | unused_mut,
2335
= note: this `expect` is overridden by a `warn` attribute before the `unused_mut` lint is triggered
2436

2537
warning: this lint expectation is unfulfilled
26-
--> $DIR/expect_nested_lint_levels.rs:8:5
38+
--> $DIR/expect_nested_lint_levels.rs:7:5
2739
|
2840
LL | unused_mut,
2941
| ^^^^^^^^^^
3042
|
3143
= note: this `expect` is overridden by a `allow` attribute before the `unused_mut` lint is triggered
3244

33-
warning: 3 warnings emitted
45+
warning: this lint expectation is unfulfilled
46+
--> $DIR/expect_nested_lint_levels.rs:43:10
47+
|
48+
LL | #[expect(unused_variables)]
49+
| ^^^^^^^^^^^^^^^^
50+
51+
error: aborting due to previous error; 4 warnings emitted
3452

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![feature(lint_reasons)]
2+
3+
#[forbid(unused_variables)]
4+
//~^ NOTE `forbid` level set here
5+
//~| NOTE `forbid` level set here
6+
#[expect(unused_variables)]
7+
//~^ ERROR incompatible with previous forbid [E0453]
8+
//~| NOTE overruled by previous forbid
9+
//~| ERROR incompatible with previous forbid [E0453]
10+
//~| NOTE overruled by previous forbid
11+
fn expect_forbidden_lint_1() {}
12+
13+
#[forbid(while_true)]
14+
//~^ NOTE `forbid` level set here
15+
//~| NOTE `forbid` level set here
16+
//~| NOTE the lint level is defined here
17+
#[expect(while_true)]
18+
//~^ ERROR incompatible with previous forbid [E0453]
19+
//~| NOTE overruled by previous forbid
20+
//~| ERROR incompatible with previous forbid [E0453]
21+
//~| NOTE overruled by previous forbid
22+
fn expect_forbidden_lint_2() {
23+
// This while loop will produce a `while_true` lint as the lint level
24+
// at this node is still `forbid` and the `while_true` check happens
25+
// before the compilation terminates due to `E0453`
26+
while true {}
27+
//~^ ERROR denote infinite loops with `loop { ... }`
28+
//~| HELP use `loop`
29+
}
30+
31+
fn main() {
32+
expect_forbidden_lint_1();
33+
expect_forbidden_lint_2();
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error[E0453]: expect(unused_variables) incompatible with previous forbid
2+
--> $DIR/expect_with_forbid.rs:6:10
3+
|
4+
LL | #[forbid(unused_variables)]
5+
| ---------------- `forbid` level set here
6+
...
7+
LL | #[expect(unused_variables)]
8+
| ^^^^^^^^^^^^^^^^ overruled by previous forbid
9+
10+
error[E0453]: expect(while_true) incompatible with previous forbid
11+
--> $DIR/expect_with_forbid.rs:17:10
12+
|
13+
LL | #[forbid(while_true)]
14+
| ---------- `forbid` level set here
15+
...
16+
LL | #[expect(while_true)]
17+
| ^^^^^^^^^^ overruled by previous forbid
18+
19+
error[E0453]: expect(unused_variables) incompatible with previous forbid
20+
--> $DIR/expect_with_forbid.rs:6:10
21+
|
22+
LL | #[forbid(unused_variables)]
23+
| ---------------- `forbid` level set here
24+
...
25+
LL | #[expect(unused_variables)]
26+
| ^^^^^^^^^^^^^^^^ overruled by previous forbid
27+
28+
error[E0453]: expect(while_true) incompatible with previous forbid
29+
--> $DIR/expect_with_forbid.rs:17:10
30+
|
31+
LL | #[forbid(while_true)]
32+
| ---------- `forbid` level set here
33+
...
34+
LL | #[expect(while_true)]
35+
| ^^^^^^^^^^ overruled by previous forbid
36+
37+
error: denote infinite loops with `loop { ... }`
38+
--> $DIR/expect_with_forbid.rs:26:5
39+
|
40+
LL | while true {}
41+
| ^^^^^^^^^^ help: use `loop`
42+
|
43+
note: the lint level is defined here
44+
--> $DIR/expect_with_forbid.rs:13:10
45+
|
46+
LL | #[forbid(while_true)]
47+
| ^^^^^^^^^^
48+
49+
error: aborting due to 5 previous errors
50+
51+
For more information about this error, try `rustc --explain E0453`.

0 commit comments

Comments
 (0)