Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3dbbf23

Browse files
committed
Rename cast_ref_to_mut lint to invalid_reference_casting
1 parent a161ab0 commit 3dbbf23

File tree

9 files changed

+28
-28
lines changed

9 files changed

+28
-28
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ lint_builtin_unused_doc_comment = unused doc comment
155155
lint_builtin_while_true = denote infinite loops with `loop {"{"} ... {"}"}`
156156
.suggestion = use `loop`
157157
158-
lint_cast_ref_to_mut = casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
159-
160158
lint_check_name_deprecated = lint name `{$lint_name}` is deprecated and does not have an effect anymore. Use: {$new_name}
161159
162160
lint_check_name_unknown = unknown lint: `{$lint_name}`
@@ -322,6 +320,8 @@ lint_invalid_nan_comparisons_eq_ne = incorrect NaN comparison, NaN cannot be dir
322320
323321
lint_invalid_nan_comparisons_lt_le_gt_ge = incorrect NaN comparison, NaN is not orderable
324322
323+
lint_invalid_reference_casting = casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
324+
325325
lint_lintpass_by_hand = implementing `LintPass` by hand
326326
.help = try using `declare_lint_pass!` or `impl_lint_pass!` instead
327327

compiler/rustc_lint/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ extern crate tracing;
5050

5151
mod array_into_iter;
5252
pub mod builtin;
53-
mod cast_ref_to_mut;
5453
mod context;
5554
mod deref_into_dyn_supertrait;
5655
mod drop_forget_useless;
@@ -78,6 +77,7 @@ mod opaque_hidden_inferred_bound;
7877
mod pass_by_value;
7978
mod passes;
8079
mod redundant_semicolon;
80+
mod reference_casting;
8181
mod traits;
8282
mod types;
8383
mod unused;
@@ -99,7 +99,6 @@ use rustc_span::Span;
9999

100100
use array_into_iter::ArrayIntoIter;
101101
use builtin::*;
102-
use cast_ref_to_mut::*;
103102
use deref_into_dyn_supertrait::*;
104103
use drop_forget_useless::*;
105104
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
@@ -119,6 +118,7 @@ use noop_method_call::*;
119118
use opaque_hidden_inferred_bound::*;
120119
use pass_by_value::*;
121120
use redundant_semicolon::*;
121+
use reference_casting::*;
122122
use traits::*;
123123
use types::*;
124124
use unused::*;
@@ -218,7 +218,7 @@ late_lint_methods!(
218218
BoxPointers: BoxPointers,
219219
PathStatements: PathStatements,
220220
LetUnderscore: LetUnderscore,
221-
CastRefToMut: CastRefToMut,
221+
InvalidReferenceCasting: InvalidReferenceCasting,
222222
// Depends on referenced function signatures in expressions
223223
UnusedResults: UnusedResults,
224224
NonUpperCaseGlobals: NonUpperCaseGlobals,

compiler/rustc_lint/src/lints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,10 @@ pub enum InvalidFromUtf8Diag {
743743
},
744744
}
745745

746-
// cast_ref_to_mut.rs
746+
// reference_casting.rs
747747
#[derive(LintDiagnostic)]
748-
#[diag(lint_cast_ref_to_mut)]
749-
pub struct CastRefToMutDiag;
748+
#[diag(lint_invalid_reference_casting)]
749+
pub struct InvalidReferenceCastingDiag;
750750

751751
// hidden_unicode_codepoints.rs
752752
#[derive(LintDiagnostic)]

compiler/rustc_lint/src/cast_ref_to_mut.rs renamed to compiler/rustc_lint/src/reference_casting.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use rustc_hir::{Expr, ExprKind, MutTy, TyKind, UnOp};
33
use rustc_middle::ty;
44
use rustc_span::sym;
55

6-
use crate::{lints::CastRefToMutDiag, LateContext, LateLintPass, LintContext};
6+
use crate::{lints::InvalidReferenceCastingDiag, LateContext, LateLintPass, LintContext};
77

88
declare_lint! {
9-
/// The `cast_ref_to_mut` lint checks for casts of `&T` to `&mut T`
9+
/// The `invalid_reference_casting` lint checks for casts of `&T` to `&mut T`
1010
/// without using interior mutability.
1111
///
1212
/// ### Example
@@ -28,14 +28,14 @@ declare_lint! {
2828
///
2929
/// `UnsafeCell` is the only way to obtain aliasable data that is considered
3030
/// mutable.
31-
CAST_REF_TO_MUT,
31+
INVALID_REFERENCE_CASTING,
3232
Deny,
3333
"casts of `&T` to `&mut T` without interior mutability"
3434
}
3535

36-
declare_lint_pass!(CastRefToMut => [CAST_REF_TO_MUT]);
36+
declare_lint_pass!(InvalidReferenceCasting => [INVALID_REFERENCE_CASTING]);
3737

38-
impl<'tcx> LateLintPass<'tcx> for CastRefToMut {
38+
impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
3939
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
4040
let ExprKind::Unary(UnOp::Deref, e) = &expr.kind else {
4141
return;
@@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for CastRefToMut {
6868

6969
let e = e.peel_blocks();
7070
if let ty::Ref(..) = cx.typeck_results().node_type(e.hir_id).kind() {
71-
cx.emit_spanned_lint(CAST_REF_TO_MUT, expr.span, CastRefToMutDiag);
71+
cx.emit_spanned_lint(INVALID_REFERENCE_CASTING, expr.span, InvalidReferenceCastingDiag);
7272
}
7373
}
7474
}

src/tools/miri/tests/fail/both_borrows/shr_frozen_violation1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
33

4-
#![allow(cast_ref_to_mut)]
4+
#![allow(invalid_reference_casting)]
55

66
fn foo(x: &mut i32) -> i32 {
77
*x = 5;

src/tools/miri/tests/fail/modifying_constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This should fail even without validation/SB
22
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

4-
#![allow(cast_ref_to_mut)]
4+
#![allow(invalid_reference_casting)]
55

66
fn main() {
77
let x = &1; // the `&1` is promoted to a constant, but it used to be that only the pointer is marked static, not the pointee

tests/ui/const-generics/issues/issue-100313.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: casting `&T` to `&mut T` is undefined behavior, even if the reference is
44
LL | *(B as *const bool as *mut bool) = false;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `#[deny(cast_ref_to_mut)]` on by default
7+
= note: `#[deny(invalid_reference_casting)]` on by default
88

99
error[E0080]: evaluation of constant value failed
1010
--> $DIR/issue-100313.rs:10:13
File renamed without changes.

tests/ui/lint/cast_ref_to_mut.stderr renamed to tests/ui/lint/reference_casting.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
2-
--> $DIR/cast_ref_to_mut.rs:19:9
2+
--> $DIR/reference_casting.rs:19:9
33
|
44
LL | (*(a as *const _ as *mut String)).push_str(" world");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `#[deny(cast_ref_to_mut)]` on by default
7+
= note: `#[deny(invalid_reference_casting)]` on by default
88

99
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
10-
--> $DIR/cast_ref_to_mut.rs:21:9
10+
--> $DIR/reference_casting.rs:21:9
1111
|
1212
LL | *(a as *const _ as *mut _) = String::from("Replaced");
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
16-
--> $DIR/cast_ref_to_mut.rs:23:9
16+
--> $DIR/reference_casting.rs:23:9
1717
|
1818
LL | *(a as *const _ as *mut String) += " world";
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
22-
--> $DIR/cast_ref_to_mut.rs:25:25
22+
--> $DIR/reference_casting.rs:25:25
2323
|
2424
LL | let _num = &mut *(num as *const i32 as *mut i32);
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
28-
--> $DIR/cast_ref_to_mut.rs:27:25
28+
--> $DIR/reference_casting.rs:27:25
2929
|
3030
LL | let _num = &mut *(num as *const i32).cast_mut();
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

3333
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
34-
--> $DIR/cast_ref_to_mut.rs:29:20
34+
--> $DIR/reference_casting.rs:29:20
3535
|
3636
LL | let _num = *{ num as *const i32 }.cast_mut();
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
40-
--> $DIR/cast_ref_to_mut.rs:31:9
40+
--> $DIR/reference_casting.rs:31:9
4141
|
4242
LL | *std::ptr::from_ref(num).cast_mut() += 1;
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

4545
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
46-
--> $DIR/cast_ref_to_mut.rs:33:9
46+
--> $DIR/reference_casting.rs:33:9
4747
|
4848
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

5151
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
52-
--> $DIR/cast_ref_to_mut.rs:35:9
52+
--> $DIR/reference_casting.rs:35:9
5353
|
5454
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5656

5757
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
58-
--> $DIR/cast_ref_to_mut.rs:37:9
58+
--> $DIR/reference_casting.rs:37:9
5959
|
6060
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)