@@ -13,7 +13,7 @@ use syntax_pos::{SourceFile, Span, MultiSpan};
1313
1414use crate :: {
1515 Level , CodeSuggestion , Diagnostic , SubDiagnostic ,
16- SuggestionStyle , SourceMapperDyn , DiagnosticId ,
16+ SuggestionStyle , SourceMapper , SourceMapperDyn , DiagnosticId ,
1717} ;
1818use crate :: Level :: Error ;
1919use crate :: snippet:: { Annotation , AnnotationType , Line , MultilineAnnotation , StyledString , Style } ;
@@ -239,11 +239,11 @@ pub trait Emitter {
239239 format ! (
240240 "help: {}{}: `{}`" ,
241241 sugg. msg,
242- if self . source_map( ) . as_ref ( ) . map( |sm| substitution . to_lowercase ( ) == sm
243- . span_to_snippet ( sugg . substitutions [ 0 ] . parts [ 0 ] . span )
244- . unwrap ( )
245- . to_lowercase ( ) ) . unwrap_or ( false )
246- {
242+ if self . source_map( ) . map( |sm| is_case_difference (
243+ & * * sm ,
244+ substitution ,
245+ sugg . substitutions [ 0 ] . parts [ 0 ] . span ,
246+ ) ) . unwrap_or ( false ) {
247247 " (notice the capitalization)"
248248 } else {
249249 ""
@@ -2058,3 +2058,18 @@ impl<'a> Drop for WritableDst<'a> {
20582058 }
20592059 }
20602060}
2061+
2062+ /// Whether the original and suggested code are visually similar enough to warrant extra wording.
2063+ pub fn is_case_difference ( sm : & dyn SourceMapper , suggested : & str , sp : Span ) -> bool {
2064+ // FIXME: this should probably be extended to also account for `FO0` → `FOO` and unicode.
2065+ let found = sm. span_to_snippet ( sp) . unwrap ( ) ;
2066+ let ascii_confusables = & [ 'c' , 'f' , 'i' , 'k' , 'o' , 's' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ] ;
2067+ // There are ASCII chars that are confusable (above) and differ in capitalization:
2068+ let confusable = found. chars ( ) . zip ( suggested. chars ( ) ) . any ( |( f, s) | {
2069+ ( ascii_confusables. contains ( & f) || ascii_confusables. contains ( & s) ) && f != s
2070+ } ) ;
2071+ confusable && found. to_lowercase ( ) == suggested. to_lowercase ( )
2072+ // FIXME: We sometimes suggest the same thing we already have, which is a
2073+ // bug, but be defensive against that here.
2074+ && found != suggested
2075+ }
0 commit comments