@@ -5090,62 +5090,59 @@ impl<'a> Resolver<'a> {
5090
5090
}
5091
5091
5092
5092
// See https://github.com/rust-lang/rust/issues/32354
5093
- if old_binding. is_import ( ) || new_binding. is_import ( ) {
5094
- let binding = if new_binding. is_import ( ) && !new_binding. span . is_dummy ( ) {
5095
- new_binding
5093
+ let directive = match ( & new_binding. kind , & old_binding. kind ) {
5094
+ ( NameBindingKind :: Import { directive, .. } , _) if !new_binding. span . is_dummy ( ) =>
5095
+ Some ( ( directive, new_binding. span ) ) ,
5096
+ ( _, NameBindingKind :: Import { directive, .. } ) if !old_binding. span . is_dummy ( ) =>
5097
+ Some ( ( directive, old_binding. span ) ) ,
5098
+ _ => None ,
5099
+ } ;
5100
+ if let Some ( ( directive, binding_span) ) = directive {
5101
+ let suggested_name = if name. as_str ( ) . chars ( ) . next ( ) . unwrap ( ) . is_uppercase ( ) {
5102
+ format ! ( "Other{}" , name)
5096
5103
} else {
5097
- old_binding
5104
+ format ! ( "other_{}" , name )
5098
5105
} ;
5099
5106
5100
- let cm = self . session . source_map ( ) ;
5101
- let rename_msg = "you can use `as` to change the binding name of the import" ;
5102
-
5103
- if let (
5104
- Ok ( snippet) ,
5105
- NameBindingKind :: Import { directive, ..} ,
5106
- false ,
5107
- false ,
5108
- ) = (
5109
- cm. span_to_snippet ( binding. span ) ,
5110
- binding. kind . clone ( ) ,
5111
- binding. span . is_dummy ( ) ,
5112
- binding. span . ctxt ( ) . outer ( ) . expn_info ( ) . is_some ( ) ,
5113
- ) {
5114
- let suggested_name = if name. as_str ( ) . chars ( ) . next ( ) . unwrap ( ) . is_uppercase ( ) {
5115
- format ! ( "Other{}" , name)
5116
- } else {
5117
- format ! ( "other_{}" , name)
5118
- } ;
5107
+ let mut suggestion = None ;
5108
+ match directive. subclass {
5109
+ ImportDirectiveSubclass :: SingleImport { type_ns_only : true , .. } =>
5110
+ suggestion = Some ( format ! ( "self as {}" , suggested_name) ) ,
5111
+ ImportDirectiveSubclass :: SingleImport { source, .. } => {
5112
+ if let Some ( pos) = source. span . hi ( ) . 0 . checked_sub ( binding_span. lo ( ) . 0 )
5113
+ . map ( |pos| pos as usize ) {
5114
+ if let Ok ( snippet) = self . session . source_map ( )
5115
+ . span_to_snippet ( binding_span) {
5116
+ if pos <= snippet. len ( ) {
5117
+ suggestion = Some ( format ! (
5118
+ "{} as {}{}" ,
5119
+ & snippet[ ..pos] ,
5120
+ suggested_name,
5121
+ if snippet. ends_with( ";" ) { ";" } else { "" }
5122
+ ) )
5123
+ }
5124
+ }
5125
+ }
5126
+ }
5127
+ ImportDirectiveSubclass :: ExternCrate { source, target, .. } =>
5128
+ suggestion = Some ( format ! (
5129
+ "extern crate {} as {};" ,
5130
+ source. unwrap_or( target. name) ,
5131
+ suggested_name,
5132
+ ) ) ,
5133
+ _ => unreachable ! ( ) ,
5134
+ }
5119
5135
5136
+ let rename_msg = "you can use `as` to change the binding name of the import" ;
5137
+ if let Some ( suggestion) = suggestion {
5120
5138
err. span_suggestion_with_applicability (
5121
- binding. span ,
5122
- & rename_msg,
5123
- match directive. subclass {
5124
- ImportDirectiveSubclass :: SingleImport { type_ns_only : true , .. } =>
5125
- format ! ( "self as {}" , suggested_name) ,
5126
- ImportDirectiveSubclass :: SingleImport { source, .. } =>
5127
- format ! (
5128
- "{} as {}{}" ,
5129
- & snippet[ ..( ( source. span. hi( ) . 0 - binding. span. lo( ) . 0 ) as usize ) ] ,
5130
- suggested_name,
5131
- if snippet. ends_with( ";" ) {
5132
- ";"
5133
- } else {
5134
- ""
5135
- }
5136
- ) ,
5137
- ImportDirectiveSubclass :: ExternCrate { source, target, .. } =>
5138
- format ! (
5139
- "extern crate {} as {};" ,
5140
- source. unwrap_or( target. name) ,
5141
- suggested_name,
5142
- ) ,
5143
- _ => unreachable ! ( ) ,
5144
- } ,
5139
+ binding_span,
5140
+ rename_msg,
5141
+ suggestion,
5145
5142
Applicability :: MaybeIncorrect ,
5146
5143
) ;
5147
5144
} else {
5148
- err. span_label ( binding . span , rename_msg) ;
5145
+ err. span_label ( binding_span , rename_msg) ;
5149
5146
}
5150
5147
}
5151
5148
0 commit comments