diff options
| author | Jubilee <workingjubilee@gmail.com> | 2024-11-07 18:48:21 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-07 18:48:21 -0800 |
| commit | 6c0e8ef86aa6dd8ce6102d364633c3eae6a8dc36 (patch) | |
| tree | 3a2e82b1946780a44d2cf787a72650cd10c8b871 /compiler | |
| parent | 93e9ec05a9c2eeea41dbbc5f52954ff0d0b87de1 (diff) | |
| parent | cc0ec046b1f7a1b708454cf8d6e33fceee44420d (diff) | |
| download | rust-6c0e8ef86aa6dd8ce6102d364633c3eae6a8dc36.tar.gz rust-6c0e8ef86aa6dd8ce6102d364633c3eae6a8dc36.zip | |
Rollup merge of #132095 - gechelberger:fix-131977, r=wesleywiser
Fix #131977 parens mangled in shared mut static lint suggestion Resolves #131977 for static mut references after discussion with Esteban & Jieyou on [t-compiler/help](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/linting.20with.20parens.20in.20the.20HIR). This doesn't do anything to change the underlying issue if there are other expressions that generate lint suggestions which need to be applied within parentheses.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_lint/src/static_mut_refs.rs | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/compiler/rustc_lint/src/static_mut_refs.rs b/compiler/rustc_lint/src/static_mut_refs.rs index 5d78b41944f..fed5c29284b 100644 --- a/compiler/rustc_lint/src/static_mut_refs.rs +++ b/compiler/rustc_lint/src/static_mut_refs.rs @@ -3,8 +3,8 @@ use rustc_hir::{Expr, Stmt}; use rustc_middle::ty::{Mutability, TyKind}; use rustc_session::lint::FutureIncompatibilityReason; use rustc_session::{declare_lint, declare_lint_pass}; -use rustc_span::Span; use rustc_span::edition::Edition; +use rustc_span::{BytePos, Span}; use crate::lints::{MutRefSugg, RefOfMutStatic}; use crate::{LateContext, LateLintPass, LintContext}; @@ -71,13 +71,24 @@ impl<'tcx> LateLintPass<'tcx> for StaticMutRefs { if matches!(borrow_kind, hir::BorrowKind::Ref) && let Some(err_span) = path_is_static_mut(ex, err_span) => { - emit_static_mut_refs( - cx, - err_span, - err_span.with_hi(ex.span.lo()), - m, - !expr.span.from_expansion(), - ); + let source_map = cx.sess().source_map(); + let snippet = source_map.span_to_snippet(err_span); + + let sugg_span = if let Ok(snippet) = snippet { + // ( ( &IDENT ) ) + // ~~~~ exclude these from the suggestion span to avoid unmatching parens + let exclude_n_bytes: u32 = snippet + .chars() + .take_while(|ch| ch.is_whitespace() || *ch == '(') + .map(|ch| ch.len_utf8() as u32) + .sum(); + + err_span.with_lo(err_span.lo() + BytePos(exclude_n_bytes)).with_hi(ex.span.lo()) + } else { + err_span.with_hi(ex.span.lo()) + }; + + emit_static_mut_refs(cx, err_span, sugg_span, m, !expr.span.from_expansion()); } hir::ExprKind::MethodCall(_, e, _, _) if let Some(err_span) = path_is_static_mut(e, expr.span) |
