-
Notifications
You must be signed in to change notification settings - Fork 1.8k
add new lint: rest_when_destructuring_struct
#15000
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
Open
Erk-
wants to merge
10
commits into
rust-lang:master
Choose a base branch
from
Erk-:lint/rest_when_destructuring_struct
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+462
−4
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6888853
add new lint: `rest_when_destructuring_struct`
Erk- 5dddd0e
add suggestions to `rest_when_destructuring_struct`
Erk- a269af7
guard `rest_when_destructuring_struct` against panics
Erk- b67ec9e
impl WithSearchPat for patterns
Erk- e00cb9b
`rest_when_destructuring_struct`: check if inside of proc macro
Erk- 12ed41b
remove debug log
Erk- 04caaae
use let chains to get more in line with the general style
Erk- 4f53e81
Use the span of .. now that the compiler provides it
Erk- 45d2dfa
handle non local non-exhaustive structs as well as private fields
Erk- 8200ba7
handle feedback from review
Erk- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::is_from_proc_macro; | ||
use itertools::Itertools; | ||
use rustc_abi::VariantIdx; | ||
use rustc_lint::LateLintPass; | ||
use rustc_middle::ty::{self, Visibility}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Disallows the use of rest patterns when destructuring structs. | ||
/// | ||
/// ### Why is this bad? | ||
/// It might lead to unhandled fields when the struct changes. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// struct S { | ||
/// a: u8, | ||
/// b: u8, | ||
/// c: u8, | ||
/// } | ||
/// | ||
/// let s = S { a: 1, b: 2, c: 3 }; | ||
/// | ||
/// let S { a, b, .. } = s; | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// struct S { | ||
/// a: u8, | ||
/// b: u8, | ||
/// c: u8, | ||
/// } | ||
/// | ||
/// let s = S { a: 1, b: 2, c: 3 }; | ||
/// | ||
/// let S { a, b, c: _ } = s; | ||
/// ``` | ||
#[clippy::version = "1.89.0"] | ||
pub REST_WHEN_DESTRUCTURING_STRUCT, | ||
nursery, | ||
"rest (..) in destructuring expression" | ||
} | ||
declare_lint_pass!(RestWhenDestructuringStruct => [REST_WHEN_DESTRUCTURING_STRUCT]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for RestWhenDestructuringStruct { | ||
fn check_pat(&mut self, cx: &rustc_lint::LateContext<'tcx>, pat: &'tcx rustc_hir::Pat<'tcx>) { | ||
if let rustc_hir::PatKind::Struct(path, fields, Some(dotdot)) = pat.kind | ||
&& !pat.span.in_external_macro(cx.tcx.sess.source_map()) | ||
&& !is_from_proc_macro(cx, pat) | ||
&& let qty = cx.typeck_results().qpath_res(&path, pat.hir_id) | ||
&& let ty = cx.typeck_results().pat_ty(pat) | ||
&& let ty::Adt(a, _) = ty.kind() | ||
{ | ||
let vid = qty | ||
.opt_def_id() | ||
.map_or(VariantIdx::ZERO, |x| a.variant_index_with_id(x)); | ||
|
||
let leave_dotdot = a.variants()[vid].field_list_has_applicable_non_exhaustive(); | ||
|
||
let mut rest_fields = a.variants()[vid] | ||
.fields | ||
.iter() | ||
.filter(|f| { | ||
if a.did().is_local() { | ||
true | ||
} else { | ||
matches!(f.vis, Visibility::Public) | ||
} | ||
Erk- marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}) | ||
.map(|field| field.ident(cx.tcx)) | ||
.filter(|pf| !fields.iter().any(|x| x.ident == *pf)) | ||
Erk- marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
.map(|x| format!("{x}: _")); | ||
|
||
let mut fmt_fields = rest_fields.join(", "); | ||
|
||
if fmt_fields.is_empty() && leave_dotdot { | ||
// The struct is non_exhaustive, from a non-local crate and all public fields are explicitly named. | ||
return; | ||
} | ||
|
||
if leave_dotdot { | ||
fmt_fields.push_str(", .."); | ||
} | ||
|
||
span_lint_and_then( | ||
cx, | ||
REST_WHEN_DESTRUCTURING_STRUCT, | ||
pat.span, | ||
"struct destructuring with rest (..)", | ||
|diag| { | ||
diag.span_suggestion_verbose( | ||
dotdot, | ||
"consider explicitly ignoring remaining fields with wildcard patterns (x: _)", | ||
fmt_fields, | ||
rustc_errors::Applicability::MachineApplicable, | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#[non_exhaustive] | ||
#[derive(Default)] | ||
pub struct NonExhaustiveStruct { | ||
pub field1: i32, | ||
pub field2: i32, | ||
_private: i32, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//@aux-build:proc_macros.rs | ||
//@aux-build:non-exhaustive-struct.rs | ||
#![warn(clippy::rest_when_destructuring_struct)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
extern crate proc_macros; | ||
|
||
extern crate non_exhaustive_struct; | ||
|
||
use non_exhaustive_struct::NonExhaustiveStruct; | ||
|
||
struct S { | ||
a: u8, | ||
b: u8, | ||
c: u8, | ||
} | ||
|
||
enum E { | ||
A { a1: u8, a2: u8 }, | ||
B { b1: u8, b2: u8 }, | ||
C {}, | ||
} | ||
|
||
fn main() { | ||
let s = S { a: 1, b: 2, c: 3 }; | ||
|
||
let S { a, b, c: _ } = s; | ||
//~^ rest_when_destructuring_struct | ||
|
||
let S { a, b, c, } = s; | ||
//~^ rest_when_destructuring_struct | ||
|
||
let e = E::A { a1: 1, a2: 2 }; | ||
|
||
match e { | ||
E::A { a1, a2 } => (), | ||
E::B { b1: _, b2: _ } => (), | ||
//~^ rest_when_destructuring_struct | ||
E::C { } => (), | ||
//~^ rest_when_destructuring_struct | ||
} | ||
|
||
match e { | ||
E::A { a1: _, a2: _ } => (), | ||
E::B { b1: _, b2: _ } => (), | ||
//~^ rest_when_destructuring_struct | ||
E::C {} => (), | ||
} | ||
|
||
proc_macros::external! { | ||
let s1 = S { a: 1, b: 2, c: 3 }; | ||
let S { a, b, .. } = s1; | ||
} | ||
|
||
proc_macros::with_span! { | ||
span | ||
let s2 = S { a: 1, b: 2, c: 3 }; | ||
let S { a, b, .. } = s2; | ||
} | ||
|
||
let ne = NonExhaustiveStruct::default(); | ||
let NonExhaustiveStruct { field1: _, field2: _, .. } = ne; | ||
//~^ rest_when_destructuring_struct | ||
|
||
let ne = NonExhaustiveStruct::default(); | ||
let NonExhaustiveStruct { | ||
field1: _, field2: _, .. | ||
} = ne; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//@aux-build:proc_macros.rs | ||
//@aux-build:non-exhaustive-struct.rs | ||
#![warn(clippy::rest_when_destructuring_struct)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
extern crate proc_macros; | ||
|
||
extern crate non_exhaustive_struct; | ||
|
||
use non_exhaustive_struct::NonExhaustiveStruct; | ||
|
||
struct S { | ||
a: u8, | ||
b: u8, | ||
c: u8, | ||
} | ||
|
||
enum E { | ||
A { a1: u8, a2: u8 }, | ||
B { b1: u8, b2: u8 }, | ||
C {}, | ||
} | ||
|
||
fn main() { | ||
let s = S { a: 1, b: 2, c: 3 }; | ||
|
||
let S { a, b, .. } = s; | ||
//~^ rest_when_destructuring_struct | ||
|
||
let S { a, b, c, .. } = s; | ||
//~^ rest_when_destructuring_struct | ||
|
||
let e = E::A { a1: 1, a2: 2 }; | ||
|
||
match e { | ||
E::A { a1, a2 } => (), | ||
E::B { .. } => (), | ||
//~^ rest_when_destructuring_struct | ||
E::C { .. } => (), | ||
//~^ rest_when_destructuring_struct | ||
} | ||
|
||
match e { | ||
E::A { a1: _, a2: _ } => (), | ||
E::B { b1: _, .. } => (), | ||
//~^ rest_when_destructuring_struct | ||
E::C {} => (), | ||
} | ||
|
||
proc_macros::external! { | ||
let s1 = S { a: 1, b: 2, c: 3 }; | ||
let S { a, b, .. } = s1; | ||
} | ||
|
||
proc_macros::with_span! { | ||
span | ||
let s2 = S { a: 1, b: 2, c: 3 }; | ||
let S { a, b, .. } = s2; | ||
} | ||
|
||
let ne = NonExhaustiveStruct::default(); | ||
let NonExhaustiveStruct { field1: _, .. } = ne; | ||
//~^ rest_when_destructuring_struct | ||
|
||
let ne = NonExhaustiveStruct::default(); | ||
let NonExhaustiveStruct { | ||
field1: _, field2: _, .. | ||
} = ne; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.