@@ -1580,7 +1580,6 @@ impl<'tcx> Liveness<'_, 'tcx> {
15801580 opt_body: Option<&hir::Body<'_>>,
15811581 ) {
15821582 let first_hir_id = hir_ids_and_spans[0].0;
1583-
15841583 if let Some(name) = self.should_warn(var).filter(|name| name != "self") {
15851584 // annoying: for parameters in funcs like `fn(x: i32)
15861585 // {ret}`, there is only one node, so asking about
@@ -1652,11 +1651,29 @@ impl<'tcx> Liveness<'_, 'tcx> {
16521651 },
16531652 );
16541653 } else {
1654+ // #117284, when `pat_span` and `ident_span` have different contexts
1655+ // we can't provide a good suggestion, instead we pointed out the spans from macro
1656+ let from_macro = non_shorthands
1657+ .iter()
1658+ .find(|(_, pat_span, ident_span)| {
1659+ pat_span.ctxt() != ident_span.ctxt() && pat_span.from_expansion()
1660+ })
1661+ .map(|(_, pat_span, _)| *pat_span);
16551662 let non_shorthands = non_shorthands
16561663 .into_iter()
16571664 .map(|(_, _, ident_span)| ident_span)
16581665 .collect::<Vec<_>>();
1666+
16591667 let suggestions = self.string_interp_suggestions(&name, opt_body);
1668+ let sugg = if let Some(span) = from_macro {
1669+ errors::UnusedVariableSugg::NoSugg { span, name: name.clone() }
1670+ } else {
1671+ errors::UnusedVariableSugg::TryPrefixSugg {
1672+ spans: non_shorthands,
1673+ name: name.clone(),
1674+ }
1675+ };
1676+
16601677 self.ir.tcx.emit_spanned_lint(
16611678 lint::builtin::UNUSED_VARIABLES,
16621679 first_hir_id,
@@ -1666,10 +1683,8 @@ impl<'tcx> Liveness<'_, 'tcx> {
16661683 .collect::<Vec<_>>(),
16671684 errors::UnusedVariableTryPrefix {
16681685 label: if !suggestions.is_empty() { Some(pat.span) } else { None },
1669- sugg: errors::UnusedVariableTryPrefixSugg {
1670- spans: non_shorthands,
1671- name,
1672- },
1686+ name,
1687+ sugg,
16731688 string_interp: suggestions,
16741689 },
16751690 );
0 commit comments