@@ -80,7 +80,9 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
80
80
if let ItemKind :: Enum ( ref def, _) = item. kind {
81
81
let ty = cx. tcx . type_of ( item. def_id ) ;
82
82
let adt = ty. ty_adt_def ( ) . expect ( "already checked whether this is an enum" ) ;
83
-
83
+ if adt. variants . len ( ) <= 1 {
84
+ return ;
85
+ }
84
86
let mut variants_size: Vec < VariantInfo > = adt
85
87
. variants
86
88
. iter ( )
@@ -110,69 +112,67 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
110
112
} )
111
113
. collect ( ) ;
112
114
113
- if variants_size. len ( ) >= 2 {
114
- variants_size. sort_by ( |a, b| ( b. size . cmp ( & a. size ) ) ) ;
115
+ variants_size. sort_by ( |a, b| ( b. size . cmp ( & a. size ) ) ) ;
115
116
116
- let mut difference = variants_size[ 0 ] . size - variants_size[ 1 ] . size ;
117
- if difference > self . maximum_size_difference_allowed {
118
- let help_text = "consider boxing the large fields to reduce the total size of the enum" ;
119
- span_lint_and_then (
120
- cx,
121
- LARGE_ENUM_VARIANT ,
122
- def. variants [ variants_size[ 0 ] . ind ] . span ,
123
- "large size difference between variants" ,
124
- |diag| {
125
- diag. span_label (
126
- def. variants [ variants_size[ 0 ] . ind ] . span ,
127
- & format ! ( "this variant is {} bytes" , variants_size[ 0 ] . size) ,
128
- ) ;
129
- diag. span_note (
130
- def. variants [ variants_size[ 1 ] . ind ] . span ,
131
- & format ! ( "and the second-largest variant is {} bytes:" , variants_size[ 1 ] . size) ,
132
- ) ;
117
+ let mut difference = variants_size[ 0 ] . size - variants_size[ 1 ] . size ;
118
+ if difference > self . maximum_size_difference_allowed {
119
+ let help_text = "consider boxing the large fields to reduce the total size of the enum" ;
120
+ span_lint_and_then (
121
+ cx,
122
+ LARGE_ENUM_VARIANT ,
123
+ def. variants [ variants_size[ 0 ] . ind ] . span ,
124
+ "large size difference between variants" ,
125
+ |diag| {
126
+ diag. span_label (
127
+ def. variants [ variants_size[ 0 ] . ind ] . span ,
128
+ & format ! ( "this variant is {} bytes" , variants_size[ 0 ] . size) ,
129
+ ) ;
130
+ diag. span_note (
131
+ def. variants [ variants_size[ 1 ] . ind ] . span ,
132
+ & format ! ( "and the second-largest variant is {} bytes:" , variants_size[ 1 ] . size) ,
133
+ ) ;
133
134
134
- let fields = def. variants [ variants_size[ 0 ] . ind ] . data . fields ( ) ;
135
- variants_size[ 0 ] . fields_size . sort_by ( |a, b| ( a. size . cmp ( & b. size ) ) ) ;
136
- let mut applicability = Applicability :: MaybeIncorrect ;
137
- let sugg: Vec < ( Span , String ) > = variants_size[ 0 ]
138
- . fields_size
139
- . iter ( )
140
- . rev ( )
141
- . take_while ( |val| {
142
- let judge = difference > self . maximum_size_difference_allowed ;
143
- difference = if difference > val. size {
144
- difference - val. size
145
- } else {
146
- 0
147
- } ;
148
- judge
149
- } )
150
- . map ( |val| {
151
- (
152
- fields[ val. ind ] . ty . span ,
153
- format ! (
154
- "Box<{}>" ,
155
- snippet_with_applicability(
156
- cx,
157
- fields[ val. ind] . ty. span,
158
- ".." ,
159
- & mut applicability
160
- )
161
- . into_owned( )
162
- ) ,
163
- )
164
- } )
165
- . collect ( ) ;
135
+ let fields = def. variants [ variants_size[ 0 ] . ind ] . data . fields ( ) ;
136
+ variants_size[ 0 ] . fields_size . sort_by ( |a, b| ( a. size . cmp ( & b. size ) ) ) ;
137
+ let mut applicability = Applicability :: MaybeIncorrect ;
138
+ let sugg: Vec < ( Span , String ) > = variants_size[ 0 ]
139
+ . fields_size
140
+ . iter ( )
141
+ . rev ( )
142
+ . take_while ( |val| {
143
+ let judge = difference > self . maximum_size_difference_allowed ;
144
+ difference = if difference > val. size {
145
+ difference - val. size
146
+ } else {
147
+ 0
148
+ } ;
149
+ judge
150
+ } )
151
+ . map ( |val| {
152
+ (
153
+ fields[ val. ind ] . ty . span ,
154
+ format ! (
155
+ "Box<{}>" ,
156
+ snippet_with_applicability(
157
+ cx,
158
+ fields[ val. ind] . ty. span,
159
+ ".." ,
160
+ & mut applicability
161
+ )
162
+ . into_owned( )
163
+ ) ,
164
+ )
165
+ } )
166
+ . collect ( ) ;
166
167
167
- if !sugg. is_empty ( ) {
168
- diag. multipart_suggestion ( help_text, sugg, Applicability :: MaybeIncorrect ) ;
169
- return ;
170
- }
168
+ if !sugg. is_empty ( ) {
169
+ diag. multipart_suggestion ( help_text, sugg, Applicability :: MaybeIncorrect ) ;
170
+ return ;
171
+ }
171
172
172
- diag. span_help ( def. variants [ variants_size[ 0 ] . ind ] . span , help_text) ;
173
- } ,
174
- ) ;
175
- }
173
+ diag. span_help ( def. variants [ variants_size[ 0 ] . ind ] . span , help_text) ;
174
+ } ,
175
+ ) ;
176
176
}
177
177
}
178
178
}
0 commit comments