Skip to content

Commit 4814577

Browse files
authored
Partly disable unneeded_wildcard_pattern when rest_pattern_accessible_field is enabled (#17416)
Since #16733 (related issue: #16638) `unneeded_wildcard_pattern` have supported struct patterns, but this is directly contradictory to the lint `rest_pattern_accessible_field` introduced in #15000. This change disable the `unneeded_wildcard_pattern` check for struct patterns if the `rest_pattern_accessible_field` lint is enabled. Example: ```rust #![allow(unused)] #![warn(clippy::rest_pattern_accessible_field)] #![warn(clippy::unneeded_wildcard_pattern)] fn main() { struct Struct4 { a: u32, b: u32, c: u32, d: u32, } let fourval = Struct4 { a: 5, b: 10, c: 15, d: 20, }; let Struct4 { a, b, c: _, .. } = fourval; } ``` Before: ``` warning: this pattern is unneeded as the `..` pattern can match that element --> example.rs:20:25 | 20 | let Struct4 { a, b, c: _, .. } = fourval; | ^^^^^^ help: remove it | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unneeded_wildcard_pattern note: the lint level is defined here --> example.rs:3:9 | 3 | #![warn(clippy::unneeded_wildcard_pattern)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: struct destructuring with rest (`..`) --> example.rs:20:9 | 20 | let Struct4 { a, b, c: _, .. } = fourval; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#rest_pattern_accessible_field note: the lint level is defined here --> example.rs:2:9 | 2 | #![warn(clippy::rest_pattern_accessible_field)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider explicitly ignoring remaining fields with wildcard patterns (`x: _`) | 20 - let Struct4 { a, b, c: _, .. } = fourval; 20 + let Struct4 { a, b, c: _, d: _ } = fourval; | ``` After: ``` warning: struct destructuring with rest (`..`) --> example.rs:20:9 | 20 | let Struct4 { a, b, c: _, .. } = fourval; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#rest_pattern_accessible_field note: the lint level is defined here --> example.rs:2:9 | 2 | #![warn(clippy::rest_pattern_accessible_field)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider explicitly ignoring remaining fields with wildcard patterns (`x: _`) | 20 - let Struct4 { a, b, c: _, .. } = fourval; 20 + let Struct4 { a, b, c: _, d: _ } = fourval; | warning: 1 warning emitted ``` changelog: [`unneeded_wildcard_pattern`]: Partly disable when [`rest_pattern_accessible_field`] is enabled.
2 parents a1d390a + 93c0223 commit 4814577

3 files changed

Lines changed: 5 additions & 3 deletions

File tree

clippy_lints/src/misc_early/unneeded_wildcard_pattern.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use rustc_ast::ast::{Pat, PatFieldsRest, PatKind};
33
use rustc_errors::Applicability;
4-
use rustc_lint::EarlyContext;
4+
use rustc_lint::{EarlyContext, LintContext as _};
55
use rustc_span::Span;
66

77
use super::UNNEEDED_WILDCARD_PATTERN;
@@ -40,6 +40,8 @@ pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
4040
.take_while(|patfield| matches!(patfield.pat.kind, PatKind::Wild))
4141
.enumerate()
4242
.last()
43+
// Only run if `rest_pattern_accessible_field` is Allow, as they otherwise will contradict each other.
44+
&& cx.get_lint_level_spec(crate::rest_when_destructuring_struct::REST_PATTERN_ACCESSIBLE_FIELD).is_allow()
4345
{
4446
// Unlike the tuples above, structs have patfields rather than patterns, and separate out the
4547
// `..` into a separate parameter. Also, the `..` can only be at the end of the pattern.

tests/ui/rest_pattern_accessible_field.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@aux-build:proc_macros.rs
22
//@aux-build:non-exhaustive-struct.rs
33
#![warn(clippy::rest_pattern_accessible_field)]
4-
#![allow(clippy::unneeded_wildcard_pattern)]
4+
#![warn(clippy::unneeded_wildcard_pattern)]
55

66
use non_exhaustive_struct::{NonExhaustiveStruct, NonExhaustiveStructNoPrivateFields};
77

tests/ui/rest_pattern_accessible_field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@aux-build:proc_macros.rs
22
//@aux-build:non-exhaustive-struct.rs
33
#![warn(clippy::rest_pattern_accessible_field)]
4-
#![allow(clippy::unneeded_wildcard_pattern)]
4+
#![warn(clippy::unneeded_wildcard_pattern)]
55

66
use non_exhaustive_struct::{NonExhaustiveStruct, NonExhaustiveStructNoPrivateFields};
77

0 commit comments

Comments
 (0)