Fix #131977 parens mangled in shared mut static lint suggestion
This commit is contained in:
parent
b8bb2968ce
commit
cc0ec046b1
@ -3,8 +3,8 @@
|
|||||||
use rustc_middle::ty::{Mutability, TyKind};
|
use rustc_middle::ty::{Mutability, TyKind};
|
||||||
use rustc_session::lint::FutureIncompatibilityReason;
|
use rustc_session::lint::FutureIncompatibilityReason;
|
||||||
use rustc_session::{declare_lint, declare_lint_pass};
|
use rustc_session::{declare_lint, declare_lint_pass};
|
||||||
use rustc_span::Span;
|
|
||||||
use rustc_span::edition::Edition;
|
use rustc_span::edition::Edition;
|
||||||
|
use rustc_span::{BytePos, Span};
|
||||||
|
|
||||||
use crate::lints::{MutRefSugg, RefOfMutStatic};
|
use crate::lints::{MutRefSugg, RefOfMutStatic};
|
||||||
use crate::{LateContext, LateLintPass, LintContext};
|
use crate::{LateContext, LateLintPass, LintContext};
|
||||||
@ -71,13 +71,24 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
|
|||||||
if matches!(borrow_kind, hir::BorrowKind::Ref)
|
if matches!(borrow_kind, hir::BorrowKind::Ref)
|
||||||
&& let Some(err_span) = path_is_static_mut(ex, err_span) =>
|
&& let Some(err_span) = path_is_static_mut(ex, err_span) =>
|
||||||
{
|
{
|
||||||
emit_static_mut_refs(
|
let source_map = cx.sess().source_map();
|
||||||
cx,
|
let snippet = source_map.span_to_snippet(err_span);
|
||||||
err_span,
|
|
||||||
err_span.with_hi(ex.span.lo()),
|
let sugg_span = if let Ok(snippet) = snippet {
|
||||||
m,
|
// ( ( &IDENT ) )
|
||||||
!expr.span.from_expansion(),
|
// ~~~~ 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, _, _)
|
hir::ExprKind::MethodCall(_, e, _, _)
|
||||||
if let Some(err_span) = path_is_static_mut(e, expr.span)
|
if let Some(err_span) = path_is_static_mut(e, expr.span)
|
||||||
|
13
tests/ui/statics/static-mut-shared-parens.rs
Normal file
13
tests/ui/statics/static-mut-shared-parens.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
//Missing paren in diagnostic msg: https://github.com/rust-lang/rust/issues/131977
|
||||||
|
//@check-pass
|
||||||
|
|
||||||
|
|
||||||
|
static mut TEST: usize = 0;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _ = unsafe { (&TEST) as *const usize };
|
||||||
|
//~^WARN creating a shared reference to mutable static is discouraged
|
||||||
|
|
||||||
|
let _ = unsafe { ((&mut TEST)) as *const usize };
|
||||||
|
//~^WARN creating a mutable reference to mutable static is discouraged
|
||||||
|
}
|
29
tests/ui/statics/static-mut-shared-parens.stderr
Normal file
29
tests/ui/statics/static-mut-shared-parens.stderr
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
warning: creating a shared reference to mutable static is discouraged
|
||||||
|
--> $DIR/static-mut-shared-parens.rs:8:22
|
||||||
|
|
|
||||||
|
LL | let _ = unsafe { (&TEST) as *const usize };
|
||||||
|
| ^^^^^^^ shared reference to mutable static
|
||||||
|
|
|
||||||
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
|
||||||
|
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives
|
||||||
|
= note: `#[warn(static_mut_refs)]` on by default
|
||||||
|
help: use `&raw const` instead to create a raw pointer
|
||||||
|
|
|
||||||
|
LL | let _ = unsafe { (&raw const TEST) as *const usize };
|
||||||
|
| ~~~~~~~~~~
|
||||||
|
|
||||||
|
warning: creating a mutable reference to mutable static is discouraged
|
||||||
|
--> $DIR/static-mut-shared-parens.rs:11:22
|
||||||
|
|
|
||||||
|
LL | let _ = unsafe { ((&mut TEST)) as *const usize };
|
||||||
|
| ^^^^^^^^^^^^^ mutable reference to mutable static
|
||||||
|
|
|
||||||
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
|
||||||
|
= note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives
|
||||||
|
help: use `&raw mut` instead to create a raw pointer
|
||||||
|
|
|
||||||
|
LL | let _ = unsafe { ((&raw mut TEST)) as *const usize };
|
||||||
|
| ~~~~~~~~
|
||||||
|
|
||||||
|
warning: 2 warnings emitted
|
||||||
|
|
Loading…
Reference in New Issue
Block a user