Auto merge of #6464 - ahouts:make-needless_update-ignore-non_exhaustive-structs, r=phansch
make needless_update ignore non_exhaustive structs changelog: make `needless_update` lint ignore `non_exhaustive` structs fixes #6323
This commit is contained in:
commit
0718eeb648
3 changed files with 18 additions and 2 deletions
|
@ -8,6 +8,9 @@ declare_clippy_lint! {
|
||||||
/// **What it does:** Checks for needlessly including a base struct on update
|
/// **What it does:** Checks for needlessly including a base struct on update
|
||||||
/// when all fields are changed anyway.
|
/// when all fields are changed anyway.
|
||||||
///
|
///
|
||||||
|
/// This lint is not applied to structs marked with
|
||||||
|
/// [non_exhaustive](https://doc.rust-lang.org/reference/attributes/type_system.html).
|
||||||
|
///
|
||||||
/// **Why is this bad?** This will cost resources (because the base has to be
|
/// **Why is this bad?** This will cost resources (because the base has to be
|
||||||
/// somewhere), and make the code less readable.
|
/// somewhere), and make the code less readable.
|
||||||
///
|
///
|
||||||
|
@ -49,7 +52,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessUpdate {
|
||||||
if let ExprKind::Struct(_, ref fields, Some(ref base)) = expr.kind {
|
if let ExprKind::Struct(_, ref fields, Some(ref base)) = expr.kind {
|
||||||
let ty = cx.typeck_results().expr_ty(expr);
|
let ty = cx.typeck_results().expr_ty(expr);
|
||||||
if let ty::Adt(def, _) = ty.kind() {
|
if let ty::Adt(def, _) = ty.kind() {
|
||||||
if fields.len() == def.non_enum_variant().fields.len() {
|
if fields.len() == def.non_enum_variant().fields.len()
|
||||||
|
&& !def.variants[0_usize.into()].is_field_list_non_exhaustive()
|
||||||
|
{
|
||||||
span_lint(
|
span_lint(
|
||||||
cx,
|
cx,
|
||||||
NEEDLESS_UPDATE,
|
NEEDLESS_UPDATE,
|
||||||
|
|
|
@ -6,9 +6,20 @@ struct S {
|
||||||
pub b: i32,
|
pub b: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[non_exhaustive]
|
||||||
|
struct T {
|
||||||
|
pub x: i32,
|
||||||
|
pub y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let base = S { a: 0, b: 0 };
|
let base = S { a: 0, b: 0 };
|
||||||
S { ..base }; // no error
|
S { ..base }; // no error
|
||||||
S { a: 1, ..base }; // no error
|
S { a: 1, ..base }; // no error
|
||||||
S { a: 1, b: 1, ..base };
|
S { a: 1, b: 1, ..base };
|
||||||
|
|
||||||
|
let base = T { x: 0, y: 0 };
|
||||||
|
T { ..base }; // no error
|
||||||
|
T { x: 1, ..base }; // no error
|
||||||
|
T { x: 1, y: 1, ..base }; // no error
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: struct update has no effect, all the fields in the struct have already been specified
|
error: struct update has no effect, all the fields in the struct have already been specified
|
||||||
--> $DIR/needless_update.rs:13:23
|
--> $DIR/needless_update.rs:19:23
|
||||||
|
|
|
|
||||||
LL | S { a: 1, b: 1, ..base };
|
LL | S { a: 1, b: 1, ..base };
|
||||||
| ^^^^
|
| ^^^^
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue