Skip to content

Commit abf92c0

Browse files
committed
Use more accurate span for addr_of! suggestion
Use a multipart suggestion instead of a single whole-span replacement: ``` error[E0796]: creating a shared reference to a mutable static --> $DIR/reference-to-mut-static-unsafe-fn.rs:10:18 | LL | let _y = &X; | ^^ shared reference to mutable static | = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior help: use `addr_of!` instead to create a raw pointer | LL | let _y = addr_of!(X); | ~~~~~~~~~ + ```
1 parent 5753b30 commit abf92c0

21 files changed

+74
-91
lines changed

compiler/rustc_hir_analysis/src/check/errs.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_hir as hir;
2-
use rustc_hir_pretty::qpath_to_string;
32
use rustc_lint_defs::builtin::STATIC_MUT_REFS;
43
use rustc_middle::ty::{Mutability, TyCtxt};
54
use rustc_span::Span;
@@ -12,9 +11,17 @@ pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) {
1211
let hir_id = expr.hir_id;
1312
if let hir::ExprKind::AddrOf(borrow_kind, m, expr) = expr.kind
1413
&& matches!(borrow_kind, hir::BorrowKind::Ref)
15-
&& let Some(var) = path_if_static_mut(tcx, expr)
14+
&& path_if_static_mut(expr)
1615
{
17-
handle_static_mut_ref(tcx, span, var, span.edition().at_least_rust_2024(), m, hir_id);
16+
handle_static_mut_ref(
17+
tcx,
18+
span,
19+
span.with_hi(expr.span.lo()),
20+
span.shrink_to_hi(),
21+
span.edition().at_least_rust_2024(),
22+
m,
23+
hir_id,
24+
);
1825
}
1926
}
2027

@@ -24,51 +31,53 @@ pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) {
2431
&& let hir::PatKind::Binding(ba, _, _, _) = loc.pat.kind
2532
&& let hir::ByRef::Yes(rmutbl) = ba.0
2633
&& let Some(init) = loc.init
27-
&& let Some(var) = path_if_static_mut(tcx, init)
34+
&& path_if_static_mut(init)
2835
{
2936
handle_static_mut_ref(
3037
tcx,
3138
init.span,
32-
var,
39+
init.span.shrink_to_lo(),
40+
init.span.shrink_to_hi(),
3341
loc.span.edition().at_least_rust_2024(),
3442
rmutbl,
3543
stmt.hir_id,
3644
);
3745
}
3846
}
3947

40-
fn path_if_static_mut(tcx: TyCtxt<'_>, expr: &hir::Expr<'_>) -> Option<String> {
48+
fn path_if_static_mut(expr: &hir::Expr<'_>) -> bool {
4149
if let hir::ExprKind::Path(qpath) = expr.kind
4250
&& let hir::QPath::Resolved(_, path) = qpath
4351
&& let hir::def::Res::Def(def_kind, _) = path.res
4452
&& let hir::def::DefKind::Static { safety: _, mutability: Mutability::Mut, nested: false } =
4553
def_kind
4654
{
47-
return Some(qpath_to_string(&tcx, &qpath));
55+
return true;
4856
}
49-
None
57+
false
5058
}
5159

5260
fn handle_static_mut_ref(
5361
tcx: TyCtxt<'_>,
5462
span: Span,
55-
var: String,
63+
lo: Span,
64+
hi: Span,
5665
e2024: bool,
5766
mutable: Mutability,
5867
hir_id: hir::HirId,
5968
) {
6069
if e2024 {
6170
let (sugg, shared) = if mutable == Mutability::Mut {
62-
(errors::StaticMutRefSugg::Mut { span, var }, "mutable")
71+
(errors::MutRefSugg::Mut { lo, hi }, "mutable")
6372
} else {
64-
(errors::StaticMutRefSugg::Shared { span, var }, "shared")
73+
(errors::MutRefSugg::Shared { lo, hi }, "shared")
6574
};
6675
tcx.dcx().emit_err(errors::StaticMutRef { span, sugg, shared });
6776
} else {
6877
let (sugg, shared) = if mutable == Mutability::Mut {
69-
(errors::RefOfMutStaticSugg::Mut { span, var }, "mutable")
78+
(errors::MutRefSugg::Mut { lo, hi }, "mutable")
7079
} else {
71-
(errors::RefOfMutStaticSugg::Shared { span, var }, "shared")
80+
(errors::MutRefSugg::Shared { lo, hi }, "shared")
7281
};
7382
tcx.emit_node_span_lint(
7483
STATIC_MUT_REFS,

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,33 +1500,33 @@ pub struct StaticMutRef<'a> {
15001500
#[label]
15011501
pub span: Span,
15021502
#[subdiagnostic]
1503-
pub sugg: StaticMutRefSugg,
1503+
pub sugg: MutRefSugg,
15041504
pub shared: &'a str,
15051505
}
15061506

15071507
#[derive(Subdiagnostic)]
1508-
pub enum StaticMutRefSugg {
1509-
#[suggestion(
1508+
pub enum MutRefSugg {
1509+
#[multipart_suggestion(
15101510
hir_analysis_suggestion,
15111511
style = "verbose",
1512-
code = "addr_of!({var})",
15131512
applicability = "maybe-incorrect"
15141513
)]
15151514
Shared {
1516-
#[primary_span]
1517-
span: Span,
1518-
var: String,
1515+
#[suggestion_part(code = "addr_of!(")]
1516+
lo: Span,
1517+
#[suggestion_part(code = ")")]
1518+
hi: Span,
15191519
},
1520-
#[suggestion(
1520+
#[multipart_suggestion(
15211521
hir_analysis_suggestion_mut,
15221522
style = "verbose",
1523-
code = "addr_of_mut!({var})",
15241523
applicability = "maybe-incorrect"
15251524
)]
15261525
Mut {
1527-
#[primary_span]
1528-
span: Span,
1529-
var: String,
1526+
#[suggestion_part(code = "addr_of_mut!(")]
1527+
lo: Span,
1528+
#[suggestion_part(code = ")")]
1529+
hi: Span,
15301530
},
15311531
}
15321532

@@ -1539,36 +1539,10 @@ pub struct RefOfMutStatic<'a> {
15391539
#[label]
15401540
pub span: Span,
15411541
#[subdiagnostic]
1542-
pub sugg: RefOfMutStaticSugg,
1542+
pub sugg: MutRefSugg,
15431543
pub shared: &'a str,
15441544
}
15451545

1546-
#[derive(Subdiagnostic)]
1547-
pub enum RefOfMutStaticSugg {
1548-
#[suggestion(
1549-
hir_analysis_suggestion,
1550-
style = "verbose",
1551-
code = "addr_of!({var})",
1552-
applicability = "maybe-incorrect"
1553-
)]
1554-
Shared {
1555-
#[primary_span]
1556-
span: Span,
1557-
var: String,
1558-
},
1559-
#[suggestion(
1560-
hir_analysis_suggestion_mut,
1561-
style = "verbose",
1562-
code = "addr_of_mut!({var})",
1563-
applicability = "maybe-incorrect"
1564-
)]
1565-
Mut {
1566-
#[primary_span]
1567-
span: Span,
1568-
var: String,
1569-
},
1570-
}
1571-
15721546
#[derive(Diagnostic)]
15731547
#[diag(hir_analysis_not_supported_delegation)]
15741548
pub struct NotSupportedDelegation<'a> {

tests/ui/abi/statics/static-mut-foreign.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | static_bound(&rust_dbg_static_mut);
1111
help: use `addr_of!` instead to create a raw pointer
1212
|
1313
LL | static_bound(addr_of!(rust_dbg_static_mut));
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~ +
1515

1616
warning: creating a mutable reference to mutable static is discouraged
1717
--> $DIR/static-mut-foreign.rs:33:22
@@ -25,7 +25,7 @@ LL | static_bound_set(&mut rust_dbg_static_mut);
2525
help: use `addr_of_mut!` instead to create a raw pointer
2626
|
2727
LL | static_bound_set(addr_of_mut!(rust_dbg_static_mut));
28-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
| ~~~~~~~~~~~~~ +
2929

3030
warning: 2 warnings emitted
3131

tests/ui/borrowck/borrowck-access-permissions.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | let _y2 = &mut static_x_mut;
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | let _y2 = addr_of_mut!(static_x_mut);
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
1717
--> $DIR/borrowck-access-permissions.rs:10:19

tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | let sfoo: *mut Foo = &mut SFOO;
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | let sfoo: *mut Foo = addr_of_mut!(SFOO);
14-
| ~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

tests/ui/borrowck/issue-20801.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | unsafe { &mut GLOBAL_MUT_T }
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | unsafe { addr_of_mut!(GLOBAL_MUT_T) }
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
error[E0507]: cannot move out of a mutable reference
1717
--> $DIR/issue-20801.rs:27:22

tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | c1(&mut Y);
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | c1(addr_of_mut!(Y));
14-
| ~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
warning: creating a mutable reference to mutable static is discouraged
1717
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:27:16
@@ -25,7 +25,7 @@ LL | c1(&mut Z);
2525
help: use `addr_of_mut!` instead to create a raw pointer
2626
|
2727
LL | c1(addr_of_mut!(Z));
28-
| ~~~~~~~~~~~~~~~
28+
| ~~~~~~~~~~~~~ +
2929

3030
warning: creating a mutable reference to mutable static is discouraged
3131
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:64:37
@@ -39,7 +39,7 @@ LL | borrowck_closures_unique::e(&mut X);
3939
help: use `addr_of_mut!` instead to create a raw pointer
4040
|
4141
LL | borrowck_closures_unique::e(addr_of_mut!(X));
42-
| ~~~~~~~~~~~~~~~
42+
| ~~~~~~~~~~~~~ +
4343

4444
error[E0594]: cannot assign to `x`, as it is not declared as mutable
4545
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:9:46

tests/ui/consts/const_let_assign2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | let ptr = unsafe { &mut BB };
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | let ptr = unsafe { addr_of_mut!(BB) };
14-
| ~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

tests/ui/drop/issue-23338-ensure-param-drop-order.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | (mem::size_of_val(&trails) * 8) as u32
1111
help: use `addr_of!` instead to create a raw pointer
1212
|
1313
LL | (mem::size_of_val(addr_of!(trails)) * 8) as u32
14-
| ~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

tests/ui/drop/issue-23611-enum-swap-in-drop.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | (mem::size_of_val(&trails) * 8) as u32
1111
help: use `addr_of!` instead to create a raw pointer
1212
|
1313
LL | (mem::size_of_val(addr_of!(trails)) * 8) as u32
14-
| ~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

0 commit comments

Comments
 (0)