@@ -90,11 +90,12 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, format_args: &FormatArgsStorag
90
90
91
91
let mut has_in_format_capture = false ;
92
92
suggestions. extend ( visitor. spans . into_iter ( ) . filter_map ( |span| match span {
93
- MaybeInFormatCapture :: Yes => {
93
+ VariableUsage :: FormatCapture => {
94
94
has_in_format_capture = true ;
95
95
None
96
96
} ,
97
- MaybeInFormatCapture :: No ( span) => Some ( ( span, "()" . to_string ( ) ) ) ,
97
+ VariableUsage :: Normal ( span) => Some ( ( span, "()" . to_string ( ) ) ) ,
98
+ VariableUsage :: FieldShorthand ( span) => Some ( ( span. shrink_to_hi ( ) , ": ()" . to_string ( ) ) ) ,
98
99
} ) ) ;
99
100
100
101
if has_in_format_capture {
@@ -141,19 +142,30 @@ struct UnitVariableCollector<'a, 'tcx> {
141
142
cx : & ' a LateContext < ' tcx > ,
142
143
format_args : & ' a FormatArgsStorage ,
143
144
id : HirId ,
144
- spans : Vec < MaybeInFormatCapture > ,
145
+ spans : Vec < VariableUsage > ,
145
146
macro_call : Option < & ' a FormatArgs > ,
146
147
}
147
148
148
- /// Whether the unit variable is captured in a `format!`:
149
- ///
150
- /// ```ignore
151
- /// let unit = ();
152
- /// eprintln!("{unit}");
153
- /// ```
154
- enum MaybeInFormatCapture {
155
- Yes ,
156
- No ( Span ) ,
149
+ /// How the unit variable is used
150
+ enum VariableUsage {
151
+ Normal ( Span ) ,
152
+ /// Captured in a `format!`:
153
+ ///
154
+ /// ```ignore
155
+ /// let unit = ();
156
+ /// eprintln!("{unit}");
157
+ /// ```
158
+ FormatCapture ,
159
+ /// In a field shorthand init:
160
+ ///
161
+ /// ```ignore
162
+ /// struct Foo {
163
+ /// unit: (),
164
+ /// }
165
+ /// let unit = ();
166
+ /// Foo { unit };
167
+ /// ```
168
+ FieldShorthand ( Span ) ,
157
169
}
158
170
159
171
impl < ' a , ' tcx > UnitVariableCollector < ' a , ' tcx > {
@@ -193,9 +205,17 @@ impl<'tcx> Visitor<'tcx> for UnitVariableCollector<'_, 'tcx> {
193
205
matches ! ( arg. kind, FormatArgumentKind :: Captured ( _) ) && find_format_arg_expr ( ex, arg) . is_some ( )
194
206
} )
195
207
{
196
- self . spans . push ( MaybeInFormatCapture :: Yes ) ;
208
+ self . spans . push ( VariableUsage :: FormatCapture ) ;
197
209
} else {
198
- self . spans . push ( MaybeInFormatCapture :: No ( path. span ) ) ;
210
+ let parent = self . cx . tcx . parent_hir_node ( ex. hir_id ) ;
211
+ match parent {
212
+ Node :: ExprField ( expr_field) if expr_field. is_shorthand => {
213
+ self . spans . push ( VariableUsage :: FieldShorthand ( ex. span ) ) ;
214
+ } ,
215
+ _ => {
216
+ self . spans . push ( VariableUsage :: Normal ( path. span ) ) ;
217
+ } ,
218
+ }
199
219
}
200
220
}
201
221
0 commit comments