File tree 3 files changed +31
-3
lines changed
3 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,14 @@ declare_clippy_lint! {
21
21
/// # z: i32,
22
22
/// # }
23
23
/// # let zero_point = Point { x: 0, y: 0, z: 0 };
24
- ///
24
+ /// #
25
+ /// # #[non_exhaustive]
26
+ /// # struct Options {
27
+ /// # a: bool,
28
+ /// # b: i32,
29
+ /// # }
30
+ /// # let default_options = Options { a: false, b: 0 };
31
+ /// #
25
32
/// // Bad
26
33
/// Point {
27
34
/// x: 1,
@@ -36,6 +43,14 @@ declare_clippy_lint! {
36
43
/// y: 1,
37
44
/// ..zero_point
38
45
/// };
46
+ ///
47
+ /// // this lint is not applied to structs marked with [non_exhaustive](https://doc.rust-lang.org/reference/attributes/type_system.html)
48
+ /// // Ok
49
+ /// Options {
50
+ /// a: true,
51
+ /// b: 321,
52
+ /// ..default_options
53
+ /// };
39
54
/// ```
40
55
pub NEEDLESS_UPDATE ,
41
56
complexity,
@@ -49,7 +64,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessUpdate {
49
64
if let ExprKind :: Struct ( _, ref fields, Some ( ref base) ) = expr. kind {
50
65
let ty = cx. typeck_results ( ) . expr_ty ( expr) ;
51
66
if let ty:: Adt ( def, _) = ty. kind ( ) {
52
- if fields. len ( ) == def. non_enum_variant ( ) . fields . len ( ) {
67
+ if fields. len ( ) == def. non_enum_variant ( ) . fields . len ( )
68
+ && !def. variants [ 0_usize . into ( ) ] . is_field_list_non_exhaustive ( )
69
+ {
53
70
span_lint (
54
71
cx,
55
72
NEEDLESS_UPDATE ,
Original file line number Diff line number Diff line change @@ -6,9 +6,20 @@ struct S {
6
6
pub b : i32 ,
7
7
}
8
8
9
+ #[ non_exhaustive]
10
+ struct T {
11
+ pub x : i32 ,
12
+ pub y : i32 ,
13
+ }
14
+
9
15
fn main ( ) {
10
16
let base = S { a : 0 , b : 0 } ;
11
17
S { ..base } ; // no error
12
18
S { a : 1 , ..base } ; // no error
13
19
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
14
25
}
Original file line number Diff line number Diff line change 1
1
error: 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
3
3
|
4
4
LL | S { a: 1, b: 1, ..base };
5
5
| ^^^^
You can’t perform that action at this time.
0 commit comments