Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lint levels not getting overridden by attrs on Stmt nodes #130293

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,9 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> {
intravisit::walk_foreign_item(self, it);
}

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

fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
Expand Down
10 changes: 9 additions & 1 deletion src/tools/clippy/tests/ui/expect_tool_lint_rfc_2383.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ error: this lint expectation is unfulfilled
LL | #[expect(invalid_nan_comparisons)]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: this lint expectation is unfulfilled
--> tests/ui/expect_tool_lint_rfc_2383.rs:36:18
|
LL | #[expect(invalid_nan_comparisons)]
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: this lint expectation is unfulfilled
--> tests/ui/expect_tool_lint_rfc_2383.rs:107:14
|
Expand All @@ -37,5 +45,5 @@ error: this lint expectation is unfulfilled
LL | #[expect(clippy::overly_complex_bool_expr)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

19 changes: 19 additions & 0 deletions tests/ui/lint/lints-on-stmt-not-overridden-130142.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Regression test for issue #130142

// Checks that we emit no warnings when a lint's level
// is overridden by an expect or allow attr on a Stmt node

//@ check-pass

#[must_use]
pub fn must_use_result() -> i32 {
42
}

fn main() {
#[expect(unused_must_use)]
must_use_result();

#[allow(unused_must_use)]
must_use_result();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ mod rustc_warn {

#[expect(invalid_nan_comparisons)]
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| NOTE duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
let _b = x == 5;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ warning: this lint expectation is unfulfilled
LL | #[expect(invalid_nan_comparisons)]
| ^^^^^^^^^^^^^^^^^^^^^^^

warning: 2 warnings emitted
warning: this lint expectation is unfulfilled
--> $DIR/expect_tool_lint_rfc_2383.rs:38:18
|
LL | #[expect(invalid_nan_comparisons)]
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: 3 warnings emitted

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,32 @@ LL | #[expect(unused_mut, reason = "this expectation will create a diagnosti
= note: this expectation will create a diagnostic with the default lint level

warning: this lint expectation is unfulfilled
--> $DIR/expect_unfulfilled_expectation.rs:24:22
--> $DIR/expect_unfulfilled_expectation.rs:17:14
|
LL | #[expect(unused_mut, reason = "this expectation will create a diagnostic with the default lint level")]
| ^^^^^^^^^^
|
= note: this expectation will create a diagnostic with the default lint level
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: this lint expectation is unfulfilled
--> $DIR/expect_unfulfilled_expectation.rs:27:22
|
LL | #[expect(unused, unfulfilled_lint_expectations, reason = "the expectation for `unused` should be fulfilled")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the expectation for `unused` should be fulfilled
= note: the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message

warning: this lint expectation is unfulfilled
--> $DIR/expect_unfulfilled_expectation.rs:27:22
|
LL | #[expect(unused, unfulfilled_lint_expectations, reason = "the expectation for `unused` should be fulfilled")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the expectation for `unused` should be fulfilled
= note: the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: 4 warnings emitted
warning: 6 warnings emitted

Loading