File tree Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,9 @@ declare_clippy_lint! {
88 /// **What it does:** Checks for needlessly including a base struct on update
99 /// when all fields are changed anyway.
1010 ///
11+ /// This lint is not applied to structs marked with
12+ /// [non_exhaustive](https://doc.rust-lang.org/reference/attributes/type_system.html).
13+ ///
1114 /// **Why is this bad?** This will cost resources (because the base has to be
1215 /// somewhere), and make the code less readable.
1316 ///
@@ -49,7 +52,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessUpdate {
4952 if let ExprKind :: Struct ( _, ref fields, Some ( ref base) ) = expr. kind {
5053 let ty = cx. typeck_results ( ) . expr_ty ( expr) ;
5154 if let ty:: Adt ( def, _) = ty. kind ( ) {
52- if fields. len ( ) == def. non_enum_variant ( ) . fields . len ( ) {
55+ if fields. len ( ) == def. non_enum_variant ( ) . fields . len ( )
56+ && !def. variants [ 0_usize . into ( ) ] . is_field_list_non_exhaustive ( )
57+ {
5358 span_lint (
5459 cx,
5560 NEEDLESS_UPDATE ,
Original file line number Diff line number Diff line change @@ -6,9 +6,20 @@ struct S {
66 pub b : i32 ,
77}
88
9+ #[ non_exhaustive]
10+ struct T {
11+ pub x : i32 ,
12+ pub y : i32 ,
13+ }
14+
915fn main ( ) {
1016 let base = S { a : 0 , b : 0 } ;
1117 S { ..base } ; // no error
1218 S { a : 1 , ..base } ; // no error
1319 S { a : 1 , b : 1 , ..base } ;
20+
21+ let base = T { x : 0 , y : 0 } ;
22+ T { ..base } ; // no error
23+ T { x : 1 , ..base } ; // no error
24+ T { x : 1 , y : 1 , ..base } ; // no error
1425}
Original file line number Diff line number Diff line change 11error: struct update has no effect, all the fields in the struct have already been specified
2- --> $DIR/needless_update.rs:13 :23
2+ --> $DIR/needless_update.rs:19 :23
33 |
44LL | S { a: 1, b: 1, ..base };
55 | ^^^^
You can’t perform that action at this time.
0 commit comments