@@ -250,25 +250,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
250
250
let code = source. error_code ( res. is_some ( ) ) ;
251
251
let mut err =
252
252
self . r . session . struct_span_err_with_code ( base_error. span , & base_error. msg , code) ;
253
- if let Some ( ( trait_ref, self_ty) ) =
254
- self . diagnostic_metadata . currently_processing_impl_trait . clone ( )
255
- && let TyKind :: Path ( _, self_ty_path) = & self_ty. kind
256
- && let PathResult :: Module ( ModuleOrUniformRoot :: Module ( module) ) = self . resolve_path ( & Segment :: from_path ( self_ty_path) , Some ( TypeNS ) , None )
257
- && let ModuleKind :: Def ( DefKind :: Trait , ..) = module. kind
258
- && trait_ref. path . span == span
259
- && let PathSource :: Trait ( _) = source
260
- && let Some ( Res :: Def ( DefKind :: Struct , _) ) = res
261
- && let Ok ( self_ty_str) =
262
- self . r . session . source_map ( ) . span_to_snippet ( self_ty. span )
263
- && let Ok ( trait_ref_str) =
264
- self . r . session . source_map ( ) . span_to_snippet ( trait_ref. path . span )
265
- {
266
- err. multipart_suggestion (
267
- "consider swapping the struct and the trait" ,
268
- vec ! [ ( trait_ref. path. span, self_ty_str) , ( self_ty. span, trait_ref_str) ] ,
269
- Applicability :: MaybeIncorrect ,
270
- ) ;
271
- }
253
+
254
+ self . suggest_swapping_misplaced_self_ty_and_trait ( & mut err, source, res, base_error. span ) ;
272
255
273
256
if let Some ( sugg) = base_error. suggestion {
274
257
err. span_suggestion_verbose ( sugg. 0 , sugg. 1 , sugg. 2 , Applicability :: MaybeIncorrect ) ;
@@ -704,6 +687,35 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
704
687
}
705
688
}
706
689
690
+ fn suggest_swapping_misplaced_self_ty_and_trait (
691
+ & mut self ,
692
+ err : & mut Diagnostic ,
693
+ source : PathSource < ' _ > ,
694
+ res : Option < Res > ,
695
+ span : Span ,
696
+ ) {
697
+ if let Some ( ( trait_ref, self_ty) ) =
698
+ self . diagnostic_metadata . currently_processing_impl_trait . clone ( )
699
+ && let TyKind :: Path ( _, self_ty_path) = & self_ty. kind
700
+ && let PathResult :: Module ( ModuleOrUniformRoot :: Module ( module) ) =
701
+ self . resolve_path ( & Segment :: from_path ( self_ty_path) , Some ( TypeNS ) , None )
702
+ && let ModuleKind :: Def ( DefKind :: Trait , ..) = module. kind
703
+ && trait_ref. path . span == span
704
+ && let PathSource :: Trait ( _) = source
705
+ && let Some ( Res :: Def ( DefKind :: Struct | DefKind :: Enum | DefKind :: Union , _) ) = res
706
+ && let Ok ( self_ty_str) =
707
+ self . r . session . source_map ( ) . span_to_snippet ( self_ty. span )
708
+ && let Ok ( trait_ref_str) =
709
+ self . r . session . source_map ( ) . span_to_snippet ( trait_ref. path . span )
710
+ {
711
+ err. multipart_suggestion (
712
+ "`impl` items mention the trait being implemented first and the type it is being implemented for second" ,
713
+ vec ! [ ( trait_ref. path. span, self_ty_str) , ( self_ty. span, trait_ref_str) ] ,
714
+ Applicability :: MaybeIncorrect ,
715
+ ) ;
716
+ }
717
+ }
718
+
707
719
fn get_single_associated_item (
708
720
& mut self ,
709
721
path : & [ Segment ] ,
0 commit comments