From ddafe234018f75f479b359a958fd09942739f954 Mon Sep 17 00:00:00 2001 From: Ryo Yoshida Date: Thu, 18 May 2023 20:33:07 +0900 Subject: [PATCH] Consider lint check attributes on match arms in late lints Additionally add analogous test for early lints. --- compiler/rustc_lint/src/late.rs | 6 +- tests/ui/lint/lint-attr-everywhere-early.rs | 8 +++ .../ui/lint/lint-attr-everywhere-early.stderr | 48 ++++++++++------ tests/ui/lint/lint-attr-everywhere-late.rs | 5 ++ .../ui/lint/lint-attr-everywhere-late.stderr | 56 +++++++++++-------- 5 files changed, 82 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index c9781a72704..8a4a451f8a8 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -240,8 +240,10 @@ fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) { } fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) { - lint_callback!(self, check_arm, a); - hir_visit::walk_arm(self, a); + self.with_lint_attrs(a.hir_id, |cx| { + lint_callback!(cx, check_arm, a); + hir_visit::walk_arm(cx, a); + }) } fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) { diff --git a/tests/ui/lint/lint-attr-everywhere-early.rs b/tests/ui/lint/lint-attr-everywhere-early.rs index fd0c4b43e05..0c820ef9a29 100644 --- a/tests/ui/lint/lint-attr-everywhere-early.rs +++ b/tests/ui/lint/lint-attr-everywhere-early.rs @@ -134,6 +134,14 @@ struct Match{f1: i32} } } + match f { + #[deny(ellipsis_inclusive_range_patterns)] + Match{f1: 0...100} => {} + //~^ ERROR range patterns are deprecated + //~| WARNING this is accepted in the current edition + _ => {} + } + // Statement Block { #![deny(unsafe_code)] diff --git a/tests/ui/lint/lint-attr-everywhere-early.stderr b/tests/ui/lint/lint-attr-everywhere-early.stderr index d6c6d5faef2..fac0eb4faff 100644 --- a/tests/ui/lint/lint-attr-everywhere-early.stderr +++ b/tests/ui/lint/lint-attr-everywhere-early.stderr @@ -384,92 +384,106 @@ note: the lint level is defined here LL | #[deny(while_true)] | ^^^^^^^^^^ +error: `...` range patterns are deprecated + --> $DIR/lint-attr-everywhere-early.rs:139:20 + | +LL | Match{f1: 0...100} => {} + | ^^^ help: use `..=` for an inclusive range + | + = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + = note: for more information, see +note: the lint level is defined here + --> $DIR/lint-attr-everywhere-early.rs:138:16 + | +LL | #[deny(ellipsis_inclusive_range_patterns)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: usage of an `unsafe` block - --> $DIR/lint-attr-everywhere-early.rs:140:9 + --> $DIR/lint-attr-everywhere-early.rs:148:9 | LL | unsafe {} | ^^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-attr-everywhere-early.rs:139:17 + --> $DIR/lint-attr-everywhere-early.rs:147:17 | LL | #![deny(unsafe_code)] | ^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-attr-everywhere-early.rs:144:9 + --> $DIR/lint-attr-everywhere-early.rs:152:9 | LL | unsafe {} | ^^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-attr-everywhere-early.rs:143:16 + --> $DIR/lint-attr-everywhere-early.rs:151:16 | LL | #[deny(unsafe_code)] | ^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-attr-everywhere-early.rs:149:5 + --> $DIR/lint-attr-everywhere-early.rs:157:5 | LL | unsafe {}; | ^^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-attr-everywhere-early.rs:148:12 + --> $DIR/lint-attr-everywhere-early.rs:156:12 | LL | #[deny(unsafe_code)] | ^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-attr-everywhere-early.rs:151:27 + --> $DIR/lint-attr-everywhere-early.rs:159:27 | LL | [#[deny(unsafe_code)] unsafe {123}]; | ^^^^^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-attr-everywhere-early.rs:151:13 + --> $DIR/lint-attr-everywhere-early.rs:159:13 | LL | [#[deny(unsafe_code)] unsafe {123}]; | ^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-attr-everywhere-early.rs:152:27 + --> $DIR/lint-attr-everywhere-early.rs:160:27 | LL | (#[deny(unsafe_code)] unsafe {123},); | ^^^^^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-attr-everywhere-early.rs:152:13 + --> $DIR/lint-attr-everywhere-early.rs:160:13 | LL | (#[deny(unsafe_code)] unsafe {123},); | ^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-attr-everywhere-early.rs:154:31 + --> $DIR/lint-attr-everywhere-early.rs:162:31 | LL | call(#[deny(unsafe_code)] unsafe {123}); | ^^^^^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-attr-everywhere-early.rs:154:17 + --> $DIR/lint-attr-everywhere-early.rs:162:17 | LL | call(#[deny(unsafe_code)] unsafe {123}); | ^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-attr-everywhere-early.rs:156:38 + --> $DIR/lint-attr-everywhere-early.rs:164:38 | LL | TupleStruct(#[deny(unsafe_code)] unsafe {123}); | ^^^^^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-attr-everywhere-early.rs:156:24 + --> $DIR/lint-attr-everywhere-early.rs:164:24 | LL | TupleStruct(#[deny(unsafe_code)] unsafe {123}); | ^^^^^^^^^^^ error: `...` range patterns are deprecated - --> $DIR/lint-attr-everywhere-early.rs:167:18 + --> $DIR/lint-attr-everywhere-early.rs:175:18 | LL | f1: 0...100, | ^^^ help: use `..=` for an inclusive range @@ -477,10 +491,10 @@ LL | f1: 0...100, = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see note: the lint level is defined here - --> $DIR/lint-attr-everywhere-early.rs:166:20 + --> $DIR/lint-attr-everywhere-early.rs:174:20 | LL | #[deny(ellipsis_inclusive_range_patterns)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 36 previous errors +error: aborting due to 37 previous errors diff --git a/tests/ui/lint/lint-attr-everywhere-late.rs b/tests/ui/lint/lint-attr-everywhere-late.rs index 1055157d602..a24355babb6 100644 --- a/tests/ui/lint/lint-attr-everywhere-late.rs +++ b/tests/ui/lint/lint-attr-everywhere-late.rs @@ -162,6 +162,11 @@ struct Match{f1: i32} } } + match 123 { + #[deny(non_snake_case)] + ARM_VAR => {} //~ ERROR variable `ARM_VAR` should have a snake case name + } + // Statement Block { #![deny(enum_intrinsics_non_enums)] diff --git a/tests/ui/lint/lint-attr-everywhere-late.stderr b/tests/ui/lint/lint-attr-everywhere-late.stderr index a69c2e0ef2b..9587556b0c1 100644 --- a/tests/ui/lint/lint-attr-everywhere-late.stderr +++ b/tests/ui/lint/lint-attr-everywhere-late.stderr @@ -305,124 +305,136 @@ note: the lint level is defined here LL | #[deny(enum_intrinsics_non_enums)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ +error: variable `ARM_VAR` should have a snake case name + --> $DIR/lint-attr-everywhere-late.rs:167:9 + | +LL | ARM_VAR => {} + | ^^^^^^^ help: convert the identifier to snake case: `arm_var` + | +note: the lint level is defined here + --> $DIR/lint-attr-everywhere-late.rs:166:16 + | +LL | #[deny(non_snake_case)] + | ^^^^^^^^^^^^^^ + error: the return value of `mem::discriminant` is unspecified when called with a non-enum type - --> $DIR/lint-attr-everywhere-late.rs:168:9 + --> $DIR/lint-attr-everywhere-late.rs:173:9 | LL | discriminant::(&123); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `i32`, which is not an enum. - --> $DIR/lint-attr-everywhere-late.rs:168:29 + --> $DIR/lint-attr-everywhere-late.rs:173:29 | LL | discriminant::(&123); | ^^^^ note: the lint level is defined here - --> $DIR/lint-attr-everywhere-late.rs:167:17 + --> $DIR/lint-attr-everywhere-late.rs:172:17 | LL | #![deny(enum_intrinsics_non_enums)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type - --> $DIR/lint-attr-everywhere-late.rs:172:9 + --> $DIR/lint-attr-everywhere-late.rs:177:9 | LL | discriminant::(&123); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `i32`, which is not an enum. - --> $DIR/lint-attr-everywhere-late.rs:172:29 + --> $DIR/lint-attr-everywhere-late.rs:177:29 | LL | discriminant::(&123); | ^^^^ note: the lint level is defined here - --> $DIR/lint-attr-everywhere-late.rs:171:16 + --> $DIR/lint-attr-everywhere-late.rs:176:16 | LL | #[deny(enum_intrinsics_non_enums)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type - --> $DIR/lint-attr-everywhere-late.rs:177:5 + --> $DIR/lint-attr-everywhere-late.rs:182:5 | LL | discriminant::(&123); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `i32`, which is not an enum. - --> $DIR/lint-attr-everywhere-late.rs:177:25 + --> $DIR/lint-attr-everywhere-late.rs:182:25 | LL | discriminant::(&123); | ^^^^ note: the lint level is defined here - --> $DIR/lint-attr-everywhere-late.rs:176:12 + --> $DIR/lint-attr-everywhere-late.rs:181:12 | LL | #[deny(enum_intrinsics_non_enums)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type - --> $DIR/lint-attr-everywhere-late.rs:179:41 + --> $DIR/lint-attr-everywhere-late.rs:184:41 | LL | [#[deny(enum_intrinsics_non_enums)] discriminant::(&123)]; | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `i32`, which is not an enum. - --> $DIR/lint-attr-everywhere-late.rs:179:61 + --> $DIR/lint-attr-everywhere-late.rs:184:61 | LL | [#[deny(enum_intrinsics_non_enums)] discriminant::(&123)]; | ^^^^ note: the lint level is defined here - --> $DIR/lint-attr-everywhere-late.rs:179:13 + --> $DIR/lint-attr-everywhere-late.rs:184:13 | LL | [#[deny(enum_intrinsics_non_enums)] discriminant::(&123)]; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type - --> $DIR/lint-attr-everywhere-late.rs:180:41 + --> $DIR/lint-attr-everywhere-late.rs:185:41 | LL | (#[deny(enum_intrinsics_non_enums)] discriminant::(&123),); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `i32`, which is not an enum. - --> $DIR/lint-attr-everywhere-late.rs:180:61 + --> $DIR/lint-attr-everywhere-late.rs:185:61 | LL | (#[deny(enum_intrinsics_non_enums)] discriminant::(&123),); | ^^^^ note: the lint level is defined here - --> $DIR/lint-attr-everywhere-late.rs:180:13 + --> $DIR/lint-attr-everywhere-late.rs:185:13 | LL | (#[deny(enum_intrinsics_non_enums)] discriminant::(&123),); | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type - --> $DIR/lint-attr-everywhere-late.rs:182:45 + --> $DIR/lint-attr-everywhere-late.rs:187:45 | LL | call(#[deny(enum_intrinsics_non_enums)] discriminant::(&123)); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `i32`, which is not an enum. - --> $DIR/lint-attr-everywhere-late.rs:182:65 + --> $DIR/lint-attr-everywhere-late.rs:187:65 | LL | call(#[deny(enum_intrinsics_non_enums)] discriminant::(&123)); | ^^^^ note: the lint level is defined here - --> $DIR/lint-attr-everywhere-late.rs:182:17 + --> $DIR/lint-attr-everywhere-late.rs:187:17 | LL | call(#[deny(enum_intrinsics_non_enums)] discriminant::(&123)); | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: the return value of `mem::discriminant` is unspecified when called with a non-enum type - --> $DIR/lint-attr-everywhere-late.rs:184:52 + --> $DIR/lint-attr-everywhere-late.rs:189:52 | LL | TupleStruct(#[deny(enum_intrinsics_non_enums)] discriminant::(&123)); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `i32`, which is not an enum. - --> $DIR/lint-attr-everywhere-late.rs:184:72 + --> $DIR/lint-attr-everywhere-late.rs:189:72 | LL | TupleStruct(#[deny(enum_intrinsics_non_enums)] discriminant::(&123)); | ^^^^ note: the lint level is defined here - --> $DIR/lint-attr-everywhere-late.rs:184:24 + --> $DIR/lint-attr-everywhere-late.rs:189:24 | LL | TupleStruct(#[deny(enum_intrinsics_non_enums)] discriminant::(&123)); | ^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 31 previous errors +error: aborting due to 32 previous errors