@@ -13,7 +13,7 @@ use syntax_pos::{SourceFile, Span, MultiSpan};
13
13
14
14
use crate :: {
15
15
Level , CodeSuggestion , Diagnostic , SubDiagnostic ,
16
- SuggestionStyle , SourceMapperDyn , DiagnosticId ,
16
+ SuggestionStyle , SourceMapper , SourceMapperDyn , DiagnosticId ,
17
17
} ;
18
18
use crate :: Level :: Error ;
19
19
use crate :: snippet:: { Annotation , AnnotationType , Line , MultilineAnnotation , StyledString , Style } ;
@@ -239,11 +239,11 @@ pub trait Emitter {
239
239
format ! (
240
240
"help: {}{}: `{}`" ,
241
241
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 ) {
247
247
" (notice the capitalization)"
248
248
} else {
249
249
""
@@ -2058,3 +2058,18 @@ impl<'a> Drop for WritableDst<'a> {
2058
2058
}
2059
2059
}
2060
2060
}
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