Skip to content

Commit fd3ee92

Browse files
committed
Fix lint levels not getting overridden by attrs on Stmt nodes
1 parent 507c05b commit fd3ee92

7 files changed

+70
-9
lines changed

Diff for: compiler/rustc_lint/src/levels.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,9 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> {
255255
intravisit::walk_foreign_item(self, it);
256256
}
257257

258-
fn visit_stmt(&mut self, e: &'tcx hir::Stmt<'tcx>) {
259-
// We will call `add_id` when we walk
260-
// the `StmtKind`. The outer statement itself doesn't
261-
// define the lint levels.
262-
intravisit::walk_stmt(self, e);
258+
fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
259+
self.add_id(s.hir_id);
260+
intravisit::walk_stmt(self, s);
263261
}
264262

265263
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {

Diff for: src/tools/clippy/tests/ui/expect_tool_lint_rfc_2383.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ error: this lint expectation is unfulfilled
1313
LL | #[expect(invalid_nan_comparisons)]
1414
| ^^^^^^^^^^^^^^^^^^^^^^^
1515

16+
error: this lint expectation is unfulfilled
17+
--> tests/ui/expect_tool_lint_rfc_2383.rs:36:18
18+
|
19+
LL | #[expect(invalid_nan_comparisons)]
20+
| ^^^^^^^^^^^^^^^^^^^^^^^
21+
|
22+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
23+
1624
error: this lint expectation is unfulfilled
1725
--> tests/ui/expect_tool_lint_rfc_2383.rs:107:14
1826
|
@@ -37,5 +45,5 @@ error: this lint expectation is unfulfilled
3745
LL | #[expect(clippy::overly_complex_bool_expr)]
3846
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3947

40-
error: aborting due to 6 previous errors
48+
error: aborting due to 7 previous errors
4149

Diff for: tests/ui/lint/lints-on-stmt-not-overridden-130142.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Regression test for issue #130142
2+
3+
// Checks that we emit no warnings when a lint's level
4+
// is overridden by an expect or allow attr on a Stmt node
5+
6+
//@ check-pass
7+
8+
#[must_use]
9+
pub fn must_use_result() -> i32 {
10+
42
11+
}
12+
13+
fn main() {
14+
#[expect(unused_must_use)]
15+
must_use_result();
16+
17+
#[allow(unused_must_use)]
18+
must_use_result();
19+
}

Diff for: tests/ui/lint/rfc-2383-lint-reason/expect_tool_lint_rfc_2383.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ mod rustc_warn {
3737

3838
#[expect(invalid_nan_comparisons)]
3939
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
40+
//~| WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
41+
//~| NOTE duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
4042
let _b = x == 5;
4143
}
4244
}

Diff for: tests/ui/lint/rfc-2383-lint-reason/expect_tool_lint_rfc_2383.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@ warning: this lint expectation is unfulfilled
1212
LL | #[expect(invalid_nan_comparisons)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
warning: 2 warnings emitted
15+
warning: this lint expectation is unfulfilled
16+
--> $DIR/expect_tool_lint_rfc_2383.rs:38:18
17+
|
18+
LL | #[expect(invalid_nan_comparisons)]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
22+
23+
warning: 3 warnings emitted
1624

Diff for: tests/ui/lint/rfc-2383-lint-reason/expect_unfulfilled_expectation.rs

+7
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@
1616
pub fn normal_test_fn() {
1717
#[expect(unused_mut, reason = "this expectation will create a diagnostic with the default lint level")]
1818
//~^ WARNING this lint expectation is unfulfilled
19+
//~| WARNING this lint expectation is unfulfilled
1920
//~| NOTE this expectation will create a diagnostic with the default lint level
21+
//~| NOTE this expectation will create a diagnostic with the default lint level
22+
//~| NOTE duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2023
let mut v = vec![1, 1, 2, 3, 5];
2124
v.sort();
2225

2326
// Check that lint lists including `unfulfilled_lint_expectations` are also handled correctly
2427
#[expect(unused, unfulfilled_lint_expectations, reason = "the expectation for `unused` should be fulfilled")]
2528
//~^ WARNING this lint expectation is unfulfilled
29+
//~| WARNING this lint expectation is unfulfilled
30+
//~| NOTE the expectation for `unused` should be fulfilled
2631
//~| NOTE the expectation for `unused` should be fulfilled
2732
//~| NOTE the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
33+
//~| NOTE the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
34+
//~| NOTE duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2835
let value = "I'm unused";
2936
}
3037

Diff for: tests/ui/lint/rfc-2383-lint-reason/expect_unfulfilled_expectation.stderr

+21-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,32 @@ LL | #[expect(unused_mut, reason = "this expectation will create a diagnosti
2626
= note: this expectation will create a diagnostic with the default lint level
2727

2828
warning: this lint expectation is unfulfilled
29-
--> $DIR/expect_unfulfilled_expectation.rs:24:22
29+
--> $DIR/expect_unfulfilled_expectation.rs:17:14
30+
|
31+
LL | #[expect(unused_mut, reason = "this expectation will create a diagnostic with the default lint level")]
32+
| ^^^^^^^^^^
33+
|
34+
= note: this expectation will create a diagnostic with the default lint level
35+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
36+
37+
warning: this lint expectation is unfulfilled
38+
--> $DIR/expect_unfulfilled_expectation.rs:27:22
39+
|
40+
LL | #[expect(unused, unfulfilled_lint_expectations, reason = "the expectation for `unused` should be fulfilled")]
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
|
43+
= note: the expectation for `unused` should be fulfilled
44+
= note: the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
45+
46+
warning: this lint expectation is unfulfilled
47+
--> $DIR/expect_unfulfilled_expectation.rs:27:22
3048
|
3149
LL | #[expect(unused, unfulfilled_lint_expectations, reason = "the expectation for `unused` should be fulfilled")]
3250
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3351
|
3452
= note: the expectation for `unused` should be fulfilled
3553
= note: the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
54+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3655

37-
warning: 4 warnings emitted
56+
warning: 6 warnings emitted
3857

0 commit comments

Comments
 (0)