Skip to content

Commit 0441150

Browse files
committed
Auto merge of #113422 - Urgau:cast_ref_to_mut-pre-beta, r=Nilstrieb
Rename and allow `cast_ref_to_mut` lint This PR is a small subset of #112431, that is the renaming of the lint (`cast_ref_to_mut` -> `invalid_reference_casting`). BUT also temporarily change the default level of the lint from deny-by-default to allow-by-default until #112431 is merged. r? `@Nilstrieb`
2 parents 2dc6610 + f25ad54 commit 0441150

File tree

14 files changed

+42
-45
lines changed

14 files changed

+42
-45
lines changed

compiler/rustc_lint/messages.ftl

+2-2
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}`
@@ -320,6 +318,8 @@ lint_invalid_nan_comparisons_eq_ne = incorrect NaN comparison, NaN cannot be dir
320318
321319
lint_invalid_nan_comparisons_lt_le_gt_ge = incorrect NaN comparison, NaN is not orderable
322320
321+
lint_invalid_reference_casting = casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
322+
323323
lint_lintpass_by_hand = implementing `LintPass` by hand
324324
.help = try using `declare_lint_pass!` or `impl_lint_pass!` instead
325325

compiler/rustc_lint/src/lib.rs

+3-3
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

+3-3
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 compiler/rustc_lint/src/reference_casting.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ 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
1313
///
1414
/// ```rust,compile_fail
15+
/// # #![deny(invalid_reference_casting)]
1516
/// fn x(r: &i32) {
1617
/// unsafe {
1718
/// *(r as *const i32 as *mut i32) += 1;
@@ -28,14 +29,14 @@ declare_lint! {
2829
///
2930
/// `UnsafeCell` is the only way to obtain aliasable data that is considered
3031
/// mutable.
31-
CAST_REF_TO_MUT,
32-
Deny,
32+
INVALID_REFERENCE_CASTING,
33+
Allow,
3334
"casts of `&T` to `&mut T` without interior mutability"
3435
}
3536

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

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

6970
let e = e.peel_blocks();
7071
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);
72+
cx.emit_spanned_lint(INVALID_REFERENCE_CASTING, expr.span, InvalidReferenceCastingDiag);
7273
}
7374
}
7475
}

src/tools/clippy/clippy_lints/src/renamed_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
3131
("clippy::stutter", "clippy::module_name_repetitions"),
3232
("clippy::to_string_in_display", "clippy::recursive_format_impl"),
3333
("clippy::zero_width_space", "clippy::invisible_characters"),
34-
("clippy::cast_ref_to_mut", "cast_ref_to_mut"),
34+
("clippy::cast_ref_to_mut", "invalid_reference_casting"),
3535
("clippy::clone_double_ref", "suspicious_double_ref_op"),
3636
("clippy::cmp_nan", "invalid_nan_comparisons"),
3737
("clippy::drop_bounds", "drop_bounds"),

src/tools/clippy/tests/ui/rename.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#![allow(clippy::module_name_repetitions)]
2929
#![allow(clippy::recursive_format_impl)]
3030
#![allow(clippy::invisible_characters)]
31-
#![allow(cast_ref_to_mut)]
3231
#![allow(suspicious_double_ref_op)]
3332
#![allow(invalid_nan_comparisons)]
33+
#![allow(invalid_reference_casting)]
3434
#![allow(drop_bounds)]
3535
#![allow(dropping_copy_types)]
3636
#![allow(dropping_references)]
@@ -79,7 +79,7 @@
7979
#![warn(clippy::module_name_repetitions)]
8080
#![warn(clippy::recursive_format_impl)]
8181
#![warn(clippy::invisible_characters)]
82-
#![warn(cast_ref_to_mut)]
82+
#![warn(invalid_reference_casting)]
8383
#![warn(suspicious_double_ref_op)]
8484
#![warn(invalid_nan_comparisons)]
8585
#![warn(drop_bounds)]

src/tools/clippy/tests/ui/rename.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#![allow(clippy::module_name_repetitions)]
2929
#![allow(clippy::recursive_format_impl)]
3030
#![allow(clippy::invisible_characters)]
31-
#![allow(cast_ref_to_mut)]
3231
#![allow(suspicious_double_ref_op)]
3332
#![allow(invalid_nan_comparisons)]
33+
#![allow(invalid_reference_casting)]
3434
#![allow(drop_bounds)]
3535
#![allow(dropping_copy_types)]
3636
#![allow(dropping_references)]

src/tools/clippy/tests/ui/rename.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ error: lint `clippy::zero_width_space` has been renamed to `clippy::invisible_ch
174174
LL | #![warn(clippy::zero_width_space)]
175175
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::invisible_characters`
176176

177-
error: lint `clippy::cast_ref_to_mut` has been renamed to `cast_ref_to_mut`
177+
error: lint `clippy::cast_ref_to_mut` has been renamed to `invalid_reference_casting`
178178
--> $DIR/rename.rs:82:9
179179
|
180180
LL | #![warn(clippy::cast_ref_to_mut)]
181-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `cast_ref_to_mut`
181+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_reference_casting`
182182

183183
error: lint `clippy::clone_double_ref` has been renamed to `suspicious_double_ref_op`
184184
--> $DIR/rename.rs:83:9

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

+1-1
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

+1-1
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.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ impl <const B: &'static bool> T<B> {
99
unsafe {
1010
*(B as *const bool as *mut bool) = false;
1111
//~^ ERROR evaluation of constant value failed [E0080]
12-
//~| ERROR casting `&T` to `&mut T` is undefined behavior
1312
}
1413
}
1514
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
2-
--> $DIR/issue-100313.rs:10:13
3-
|
4-
LL | *(B as *const bool as *mut bool) = false;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: `#[deny(cast_ref_to_mut)]` on by default
8-
91
error[E0080]: evaluation of constant value failed
102
--> $DIR/issue-100313.rs:10:13
113
|
@@ -18,11 +10,11 @@ note: inside `T::<&true>::set_false`
1810
LL | *(B as *const bool as *mut bool) = false;
1911
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2012
note: inside `_`
21-
--> $DIR/issue-100313.rs:19:5
13+
--> $DIR/issue-100313.rs:18:5
2214
|
2315
LL | x.set_false();
2416
| ^^^^^^^^^^^^^
2517

26-
error: aborting due to 2 previous errors
18+
error: aborting due to previous error
2719

2820
For more information about this error, try `rustc --explain E0080`.

tests/ui/lint/cast_ref_to_mut.rs tests/ui/lint/reference_casting.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// check-fail
22

33
#![feature(ptr_from_ref)]
4+
#![deny(invalid_reference_casting)]
45

56
extern "C" {
67
// N.B., mutability can be easily incorrect in FFI calls -- as

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
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:20: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: the lint level is defined here
8+
--> $DIR/reference_casting.rs:4:9
9+
|
10+
LL | #![deny(invalid_reference_casting)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
812

913
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
14+
--> $DIR/reference_casting.rs:22:9
1115
|
1216
LL | *(a as *const _ as *mut _) = String::from("Replaced");
1317
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1418

1519
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
20+
--> $DIR/reference_casting.rs:24:9
1721
|
1822
LL | *(a as *const _ as *mut String) += " world";
1923
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024

2125
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
26+
--> $DIR/reference_casting.rs:26:25
2327
|
2428
LL | let _num = &mut *(num as *const i32 as *mut i32);
2529
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2630

2731
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
32+
--> $DIR/reference_casting.rs:28:25
2933
|
3034
LL | let _num = &mut *(num as *const i32).cast_mut();
3135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3236

3337
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
38+
--> $DIR/reference_casting.rs:30:20
3539
|
3640
LL | let _num = *{ num as *const i32 }.cast_mut();
3741
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3842

3943
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
44+
--> $DIR/reference_casting.rs:32:9
4145
|
4246
LL | *std::ptr::from_ref(num).cast_mut() += 1;
4347
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4448

4549
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
50+
--> $DIR/reference_casting.rs:34:9
4751
|
4852
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
4953
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5054

5155
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
56+
--> $DIR/reference_casting.rs:36:9
5357
|
5458
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
5559
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5660

5761
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
62+
--> $DIR/reference_casting.rs:38:9
5963
|
6064
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
6165
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)