@@ -1562,6 +1562,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15621562 Some ( suggestion) if suggestion. candidate == kw:: Underscore => return false ,
15631563 Some ( suggestion) => suggestion,
15641564 } ;
1565+
1566+ let mut did_label_def_span = false ;
1567+
15651568 if let Some ( def_span) = suggestion. res . opt_def_id ( ) . map ( |def_id| self . def_span ( def_id) ) {
15661569 if span. overlaps ( def_span) {
15671570 // Don't suggest typo suggestion for itself like in the following:
@@ -1595,31 +1598,38 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15951598 errors:: DefinedHere :: SingleItem { span, candidate_descr, candidate }
15961599 }
15971600 } ;
1601+ did_label_def_span = true ;
15981602 err. subdiagnostic ( self . tcx . dcx ( ) , label) ;
15991603 }
16001604
1601- let ( span, sugg , post ) = if let SuggestionTarget :: SimilarlyNamed = suggestion. target
1605+ let ( span, msg , sugg ) = if let SuggestionTarget :: SimilarlyNamed = suggestion. target
16021606 && let Ok ( snippet) = self . tcx . sess . source_map ( ) . span_to_snippet ( span)
16031607 && let Some ( span) = suggestion. span
16041608 && let Some ( candidate) = suggestion. candidate . as_str ( ) . strip_prefix ( '_' )
16051609 && snippet == candidate
16061610 {
1611+ let candidate = suggestion. candidate ;
16071612 // When the suggested binding change would be from `x` to `_x`, suggest changing the
16081613 // original binding definition instead. (#60164)
1609- let post = format ! ( ", consider renaming `{}` into `{snippet}`" , suggestion. candidate) ;
1610- ( span, snippet, post)
1611- } else {
1612- ( span, suggestion. candidate . to_ident_string ( ) , String :: new ( ) )
1613- } ;
1614- let msg = match suggestion. target {
1615- SuggestionTarget :: SimilarlyNamed => format ! (
1616- "{} {} with a similar name exists{post}" ,
1617- suggestion. res. article( ) ,
1618- suggestion. res. descr( )
1619- ) ,
1620- SuggestionTarget :: SingleItem => {
1621- format ! ( "maybe you meant this {}" , suggestion. res. descr( ) )
1614+ let msg = format ! (
1615+ "the leading underscore in `{candidate}` marks it as unused, consider renaming it to `{snippet}`"
1616+ ) ;
1617+ if !did_label_def_span {
1618+ err. span_label ( span, format ! ( "`{candidate}` defined here" ) ) ;
16221619 }
1620+ ( span, msg, snippet)
1621+ } else {
1622+ let msg = match suggestion. target {
1623+ SuggestionTarget :: SimilarlyNamed => format ! (
1624+ "{} {} with a similar name exists" ,
1625+ suggestion. res. article( ) ,
1626+ suggestion. res. descr( )
1627+ ) ,
1628+ SuggestionTarget :: SingleItem => {
1629+ format ! ( "maybe you meant this {}" , suggestion. res. descr( ) )
1630+ }
1631+ } ;
1632+ ( span, msg, suggestion. candidate . to_ident_string ( ) )
16231633 } ;
16241634 err. span_suggestion ( span, msg, sugg, Applicability :: MaybeIncorrect ) ;
16251635 true
0 commit comments