@@ -6,6 +6,7 @@ use clippy_utils::{get_parent_expr, is_mutable};
66use rustc_hir:: { Expr , ExprField , ExprKind , Path , QPath , StructTailExpr , UnOp } ;
77use rustc_lint:: { LateContext , LateLintPass } ;
88use rustc_session:: declare_lint_pass;
9+ use rustc_span:: { DesugaringKind , Ident } ;
910
1011declare_clippy_lint ! {
1112 /// ### What it does
@@ -52,7 +53,8 @@ impl LateLintPass<'_> for UnnecessaryStruct {
5253 return ;
5354 } ;
5455
55- if expr. span . from_expansion ( ) {
56+ let expr_span = expr. range_span ( ) . unwrap_or ( expr. span ) ;
57+ if expr_span. from_expansion ( ) {
5658 // Prevent lint from hitting inside macro code
5759 return ;
5860 }
@@ -80,7 +82,7 @@ impl LateLintPass<'_> for UnnecessaryStruct {
8082 span_lint_and_sugg (
8183 cx,
8284 UNNECESSARY_STRUCT_INITIALIZATION ,
83- expr . span ,
85+ expr_span ,
8486 "unnecessary struct building" ,
8587 "replace with" ,
8688 snippet ( cx, sugg, ".." ) . into_owned ( ) ,
@@ -130,7 +132,7 @@ fn same_path_in_all_fields<'tcx>(
130132 // expression type matches
131133 && ty == cx. typeck_results ( ) . expr_ty ( src_expr)
132134 // field name matches
133- && f. ident == ident
135+ && ident_without_range_desugaring ( f. ident ) == ident
134136 // assigned from a path expression
135137 && let ExprKind :: Path ( QPath :: Resolved ( None , src_path) ) = src_expr. kind
136138 {
@@ -197,3 +199,14 @@ fn path_matches_base(path: &Path<'_>, base: &Expr<'_>) -> bool {
197199 } ;
198200 path. res == base_path. res
199201}
202+
203+ fn ident_without_range_desugaring ( ident : Ident ) -> Ident {
204+ if ident. span . desugaring_kind ( ) == Some ( DesugaringKind :: RangeExpr ) {
205+ Ident {
206+ span : ident. span . parent_callsite ( ) . unwrap ( ) ,
207+ ..ident
208+ }
209+ } else {
210+ ident
211+ }
212+ }
0 commit comments