Skip to content

Commit ba1690b

Browse files
committedJun 1, 2023
Auto merge of #111567 - Urgau:uplift_cast_ref_to_mut, r=b-naber
Uplift `clippy::cast_ref_to_mut` lint This PR aims at uplifting the `clippy::cast_ref_to_mut` lint into rustc. ## `cast_ref_to_mut` (deny-by-default) The `cast_ref_to_mut` lint checks for casts of `&T` to `&mut T` without using interior mutability. ### Example ```rust,compile_fail fn x(r: &i32) { unsafe { *(r as *const i32 as *mut i32) += 1; } } ``` ### Explanation Casting `&T` to `&mut T` without interior mutability is undefined behavior, as it's a violation of Rust reference aliasing requirements. ----- Mostly followed the instructions for uplifting a clippy lint described here: #99696 (review) `@rustbot` label: +I-lang-nominated r? compiler ----- For Clippy: changelog: Moves: Uplifted `clippy::cast_ref_to_mut` into rustc
·
1.88.01.72.0
2 parents 9af3865 + 32d4e1c commit ba1690b

File tree

22 files changed

+276
-170
lines changed

22 files changed

+276
-170
lines changed
 

‎compiler/rustc_lint/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ 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+
158160
lint_check_name_deprecated = lint name `{$lint_name}` is deprecated and does not have an effect anymore. Use: {$new_name}
159161
160162
lint_check_name_unknown = unknown lint: `{$lint_name}`
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use rustc_ast::Mutability;
2+
use rustc_hir::{Expr, ExprKind, MutTy, TyKind, UnOp};
3+
use rustc_middle::ty;
4+
use rustc_span::sym;
5+
6+
use crate::{lints::CastRefToMutDiag, LateContext, LateLintPass, LintContext};
7+
8+
declare_lint! {
9+
/// The `cast_ref_to_mut` lint checks for casts of `&T` to `&mut T`
10+
/// without using interior mutability.
11+
///
12+
/// ### Example
13+
///
14+
/// ```rust,compile_fail
15+
/// fn x(r: &i32) {
16+
/// unsafe {
17+
/// *(r as *const i32 as *mut i32) += 1;
18+
/// }
19+
/// }
20+
/// ```
21+
///
22+
/// {{produces}}
23+
///
24+
/// ### Explanation
25+
///
26+
/// Casting `&T` to `&mut T` without using interior mutability is undefined behavior,
27+
/// as it's a violation of Rust reference aliasing requirements.
28+
///
29+
/// `UnsafeCell` is the only way to obtain aliasable data that is considered
30+
/// mutable.
31+
CAST_REF_TO_MUT,
32+
Deny,
33+
"casts of `&T` to `&mut T` without interior mutability"
34+
}
35+
36+
declare_lint_pass!(CastRefToMut => [CAST_REF_TO_MUT]);
37+
38+
impl<'tcx> LateLintPass<'tcx> for CastRefToMut {
39+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
40+
let ExprKind::Unary(UnOp::Deref, e) = &expr.kind else { return; };
41+
42+
let e = e.peel_blocks();
43+
let e = if let ExprKind::Cast(e, t) = e.kind
44+
&& let TyKind::Ptr(MutTy { mutbl: Mutability::Mut, .. }) = t.kind {
45+
e
46+
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
47+
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
48+
&& cx.tcx.is_diagnostic_item(sym::ptr_cast_mut, def_id) {
49+
expr
50+
} else {
51+
return;
52+
};
53+
54+
let e = e.peel_blocks();
55+
let e = if let ExprKind::Cast(e, t) = e.kind
56+
&& let TyKind::Ptr(MutTy { mutbl: Mutability::Not, .. }) = t.kind {
57+
e
58+
} else if let ExprKind::Call(path, [arg]) = e.kind
59+
&& let ExprKind::Path(ref qpath) = path.kind
60+
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
61+
&& cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {
62+
arg
63+
} else {
64+
return;
65+
};
66+
67+
let e = e.peel_blocks();
68+
if let ty::Ref(..) = cx.typeck_results().node_type(e.hir_id).kind() {
69+
cx.emit_spanned_lint(CAST_REF_TO_MUT, expr.span, CastRefToMutDiag);
70+
}
71+
}
72+
}

‎compiler/rustc_lint/src/lib.rs

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

5151
mod array_into_iter;
5252
pub mod builtin;
53+
mod cast_ref_to_mut;
5354
mod context;
5455
mod deref_into_dyn_supertrait;
5556
mod drop_forget_useless;
@@ -97,6 +98,7 @@ use rustc_span::Span;
9798

9899
use array_into_iter::ArrayIntoIter;
99100
use builtin::*;
101+
use cast_ref_to_mut::*;
100102
use deref_into_dyn_supertrait::*;
101103
use drop_forget_useless::*;
102104
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
@@ -214,6 +216,7 @@ late_lint_methods!(
214216
BoxPointers: BoxPointers,
215217
PathStatements: PathStatements,
216218
LetUnderscore: LetUnderscore,
219+
CastRefToMut: CastRefToMut,
217220
// Depends on referenced function signatures in expressions
218221
UnusedResults: UnusedResults,
219222
NonUpperCaseGlobals: NonUpperCaseGlobals,

‎compiler/rustc_lint/src/lints.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,11 @@ pub enum InvalidFromUtf8Diag {
718718
},
719719
}
720720

721+
// cast_ref_to_mut.rs
722+
#[derive(LintDiagnostic)]
723+
#[diag(lint_cast_ref_to_mut)]
724+
pub struct CastRefToMutDiag;
725+
721726
// hidden_unicode_codepoints.rs
722727
#[derive(LintDiagnostic)]
723728
#[diag(lint_hidden_unicode_codepoints)]

‎compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,8 @@ symbols! {
11461146
profiler_builtins,
11471147
profiler_runtime,
11481148
ptr,
1149+
ptr_cast_mut,
1150+
ptr_from_ref,
11491151
ptr_guaranteed_cmp,
11501152
ptr_mask,
11511153
ptr_null,

‎library/core/src/ptr/const_ptr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl<T: ?Sized> *const T {
104104
/// refactored.
105105
#[stable(feature = "ptr_const_cast", since = "1.65.0")]
106106
#[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")]
107+
#[rustc_diagnostic_item = "ptr_cast_mut"]
107108
#[inline(always)]
108109
pub const fn cast_mut(self) -> *mut T {
109110
self as _

‎library/core/src/ptr/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ where
698698
#[inline(always)]
699699
#[must_use]
700700
#[unstable(feature = "ptr_from_ref", issue = "106116")]
701+
#[rustc_diagnostic_item = "ptr_from_ref"]
701702
pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {
702703
r
703704
}

‎src/tools/clippy/clippy_lints/src/casts/cast_ref_to_mut.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎src/tools/clippy/clippy_lints/src/casts/mod.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ mod cast_possible_truncation;
99
mod cast_possible_wrap;
1010
mod cast_precision_loss;
1111
mod cast_ptr_alignment;
12-
mod cast_ref_to_mut;
1312
mod cast_sign_loss;
1413
mod cast_slice_different_sizes;
1514
mod cast_slice_from_raw_parts;
@@ -330,41 +329,6 @@ declare_clippy_lint! {
330329
"casting a function pointer to any integer type"
331330
}
332331

333-
declare_clippy_lint! {
334-
/// ### What it does
335-
/// Checks for casts of `&T` to `&mut T` anywhere in the code.
336-
///
337-
/// ### Why is this bad?
338-
/// It’s basically guaranteed to be undefined behavior.
339-
/// `UnsafeCell` is the only way to obtain aliasable data that is considered
340-
/// mutable.
341-
///
342-
/// ### Example
343-
/// ```rust,ignore
344-
/// fn x(r: &i32) {
345-
/// unsafe {
346-
/// *(r as *const _ as *mut _) += 1;
347-
/// }
348-
/// }
349-
/// ```
350-
///
351-
/// Instead consider using interior mutability types.
352-
///
353-
/// ```rust
354-
/// use std::cell::UnsafeCell;
355-
///
356-
/// fn x(r: &UnsafeCell<i32>) {
357-
/// unsafe {
358-
/// *r.get() += 1;
359-
/// }
360-
/// }
361-
/// ```
362-
#[clippy::version = "1.33.0"]
363-
pub CAST_REF_TO_MUT,
364-
correctness,
365-
"a cast of reference to a mutable pointer"
366-
}
367-
368332
declare_clippy_lint! {
369333
/// ### What it does
370334
/// Checks for expressions where a character literal is cast
@@ -680,7 +644,6 @@ impl_lint_pass!(Casts => [
680644
CAST_POSSIBLE_TRUNCATION,
681645
CAST_POSSIBLE_WRAP,
682646
CAST_LOSSLESS,
683-
CAST_REF_TO_MUT,
684647
CAST_PTR_ALIGNMENT,
685648
CAST_SLICE_DIFFERENT_SIZES,
686649
UNNECESSARY_CAST,
@@ -747,7 +710,6 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
747710
}
748711
}
749712

750-
cast_ref_to_mut::check(cx, expr);
751713
cast_ptr_alignment::check(cx, expr);
752714
char_lit_as_u8::check(cx, expr);
753715
ptr_as_ptr::check(cx, expr, &self.msrv);

‎src/tools/clippy/clippy_lints/src/declared_lints.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
8181
crate::casts::CAST_POSSIBLE_WRAP_INFO,
8282
crate::casts::CAST_PRECISION_LOSS_INFO,
8383
crate::casts::CAST_PTR_ALIGNMENT_INFO,
84-
crate::casts::CAST_REF_TO_MUT_INFO,
8584
crate::casts::CAST_SIGN_LOSS_INFO,
8685
crate::casts::CAST_SLICE_DIFFERENT_SIZES_INFO,
8786
crate::casts::CAST_SLICE_FROM_RAW_PARTS_INFO,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +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"),
3435
("clippy::clone_double_ref", "suspicious_double_ref_op"),
3536
("clippy::drop_bounds", "drop_bounds"),
3637
("clippy::drop_copy", "dropping_copy_types"),

‎src/tools/clippy/tests/ui/cast_ref_to_mut.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎src/tools/clippy/tests/ui/cast_ref_to_mut.stderr

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![allow(clippy::recursive_format_impl)]
3030
#![allow(clippy::invisible_characters)]
3131
#![allow(suspicious_double_ref_op)]
32+
#![allow(cast_ref_to_mut)]
3233
#![allow(drop_bounds)]
3334
#![allow(dropping_copy_types)]
3435
#![allow(dropping_references)]
@@ -76,6 +77,7 @@
7677
#![warn(clippy::module_name_repetitions)]
7778
#![warn(clippy::recursive_format_impl)]
7879
#![warn(clippy::invisible_characters)]
80+
#![warn(cast_ref_to_mut)]
7981
#![warn(suspicious_double_ref_op)]
8082
#![warn(drop_bounds)]
8183
#![warn(dropping_copy_types)]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![allow(clippy::recursive_format_impl)]
3030
#![allow(clippy::invisible_characters)]
3131
#![allow(suspicious_double_ref_op)]
32+
#![allow(cast_ref_to_mut)]
3233
#![allow(drop_bounds)]
3334
#![allow(dropping_copy_types)]
3435
#![allow(dropping_references)]
@@ -76,6 +77,7 @@
7677
#![warn(clippy::stutter)]
7778
#![warn(clippy::to_string_in_display)]
7879
#![warn(clippy::zero_width_space)]
80+
#![warn(clippy::cast_ref_to_mut)]
7981
#![warn(clippy::clone_double_ref)]
8082
#![warn(clippy::drop_bounds)]
8183
#![warn(clippy::drop_copy)]
Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,298 +1,304 @@
11
error: lint `clippy::almost_complete_letter_range` has been renamed to `clippy::almost_complete_range`
2-
--> $DIR/rename.rs:50:9
2+
--> $DIR/rename.rs:51:9
33
|
44
LL | #![warn(clippy::almost_complete_letter_range)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::almost_complete_range`
66
|
77
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
88

99
error: lint `clippy::blacklisted_name` has been renamed to `clippy::disallowed_names`
10-
--> $DIR/rename.rs:51:9
10+
--> $DIR/rename.rs:52:9
1111
|
1212
LL | #![warn(clippy::blacklisted_name)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_names`
1414

1515
error: lint `clippy::block_in_if_condition_expr` has been renamed to `clippy::blocks_in_if_conditions`
16-
--> $DIR/rename.rs:52:9
16+
--> $DIR/rename.rs:53:9
1717
|
1818
LL | #![warn(clippy::block_in_if_condition_expr)]
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::blocks_in_if_conditions`
2020

2121
error: lint `clippy::block_in_if_condition_stmt` has been renamed to `clippy::blocks_in_if_conditions`
22-
--> $DIR/rename.rs:53:9
22+
--> $DIR/rename.rs:54:9
2323
|
2424
LL | #![warn(clippy::block_in_if_condition_stmt)]
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::blocks_in_if_conditions`
2626

2727
error: lint `clippy::box_vec` has been renamed to `clippy::box_collection`
28-
--> $DIR/rename.rs:54:9
28+
--> $DIR/rename.rs:55:9
2929
|
3030
LL | #![warn(clippy::box_vec)]
3131
| ^^^^^^^^^^^^^^^ help: use the new name: `clippy::box_collection`
3232

3333
error: lint `clippy::const_static_lifetime` has been renamed to `clippy::redundant_static_lifetimes`
34-
--> $DIR/rename.rs:55:9
34+
--> $DIR/rename.rs:56:9
3535
|
3636
LL | #![warn(clippy::const_static_lifetime)]
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::redundant_static_lifetimes`
3838

3939
error: lint `clippy::cyclomatic_complexity` has been renamed to `clippy::cognitive_complexity`
40-
--> $DIR/rename.rs:56:9
40+
--> $DIR/rename.rs:57:9
4141
|
4242
LL | #![warn(clippy::cyclomatic_complexity)]
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::cognitive_complexity`
4444

4545
error: lint `clippy::derive_hash_xor_eq` has been renamed to `clippy::derived_hash_with_manual_eq`
46-
--> $DIR/rename.rs:57:9
46+
--> $DIR/rename.rs:58:9
4747
|
4848
LL | #![warn(clippy::derive_hash_xor_eq)]
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::derived_hash_with_manual_eq`
5050

5151
error: lint `clippy::disallowed_method` has been renamed to `clippy::disallowed_methods`
52-
--> $DIR/rename.rs:58:9
52+
--> $DIR/rename.rs:59:9
5353
|
5454
LL | #![warn(clippy::disallowed_method)]
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_methods`
5656

5757
error: lint `clippy::disallowed_type` has been renamed to `clippy::disallowed_types`
58-
--> $DIR/rename.rs:59:9
58+
--> $DIR/rename.rs:60:9
5959
|
6060
LL | #![warn(clippy::disallowed_type)]
6161
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_types`
6262

6363
error: lint `clippy::eval_order_dependence` has been renamed to `clippy::mixed_read_write_in_expression`
64-
--> $DIR/rename.rs:60:9
64+
--> $DIR/rename.rs:61:9
6565
|
6666
LL | #![warn(clippy::eval_order_dependence)]
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::mixed_read_write_in_expression`
6868

6969
error: lint `clippy::identity_conversion` has been renamed to `clippy::useless_conversion`
70-
--> $DIR/rename.rs:61:9
70+
--> $DIR/rename.rs:62:9
7171
|
7272
LL | #![warn(clippy::identity_conversion)]
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::useless_conversion`
7474

7575
error: lint `clippy::if_let_some_result` has been renamed to `clippy::match_result_ok`
76-
--> $DIR/rename.rs:62:9
76+
--> $DIR/rename.rs:63:9
7777
|
7878
LL | #![warn(clippy::if_let_some_result)]
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::match_result_ok`
8080

8181
error: lint `clippy::integer_arithmetic` has been renamed to `clippy::arithmetic_side_effects`
82-
--> $DIR/rename.rs:63:9
82+
--> $DIR/rename.rs:64:9
8383
|
8484
LL | #![warn(clippy::integer_arithmetic)]
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::arithmetic_side_effects`
8686

8787
error: lint `clippy::logic_bug` has been renamed to `clippy::overly_complex_bool_expr`
88-
--> $DIR/rename.rs:64:9
88+
--> $DIR/rename.rs:65:9
8989
|
9090
LL | #![warn(clippy::logic_bug)]
9191
| ^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::overly_complex_bool_expr`
9292

9393
error: lint `clippy::new_without_default_derive` has been renamed to `clippy::new_without_default`
94-
--> $DIR/rename.rs:65:9
94+
--> $DIR/rename.rs:66:9
9595
|
9696
LL | #![warn(clippy::new_without_default_derive)]
9797
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::new_without_default`
9898

9999
error: lint `clippy::option_and_then_some` has been renamed to `clippy::bind_instead_of_map`
100-
--> $DIR/rename.rs:66:9
100+
--> $DIR/rename.rs:67:9
101101
|
102102
LL | #![warn(clippy::option_and_then_some)]
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::bind_instead_of_map`
104104

105105
error: lint `clippy::option_expect_used` has been renamed to `clippy::expect_used`
106-
--> $DIR/rename.rs:67:9
106+
--> $DIR/rename.rs:68:9
107107
|
108108
LL | #![warn(clippy::option_expect_used)]
109109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::expect_used`
110110

111111
error: lint `clippy::option_map_unwrap_or` has been renamed to `clippy::map_unwrap_or`
112-
--> $DIR/rename.rs:68:9
112+
--> $DIR/rename.rs:69:9
113113
|
114114
LL | #![warn(clippy::option_map_unwrap_or)]
115115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or`
116116

117117
error: lint `clippy::option_map_unwrap_or_else` has been renamed to `clippy::map_unwrap_or`
118-
--> $DIR/rename.rs:69:9
118+
--> $DIR/rename.rs:70:9
119119
|
120120
LL | #![warn(clippy::option_map_unwrap_or_else)]
121121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or`
122122

123123
error: lint `clippy::option_unwrap_used` has been renamed to `clippy::unwrap_used`
124-
--> $DIR/rename.rs:70:9
124+
--> $DIR/rename.rs:71:9
125125
|
126126
LL | #![warn(clippy::option_unwrap_used)]
127127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::unwrap_used`
128128

129129
error: lint `clippy::ref_in_deref` has been renamed to `clippy::needless_borrow`
130-
--> $DIR/rename.rs:71:9
130+
--> $DIR/rename.rs:72:9
131131
|
132132
LL | #![warn(clippy::ref_in_deref)]
133133
| ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::needless_borrow`
134134

135135
error: lint `clippy::result_expect_used` has been renamed to `clippy::expect_used`
136-
--> $DIR/rename.rs:72:9
136+
--> $DIR/rename.rs:73:9
137137
|
138138
LL | #![warn(clippy::result_expect_used)]
139139
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::expect_used`
140140

141141
error: lint `clippy::result_map_unwrap_or_else` has been renamed to `clippy::map_unwrap_or`
142-
--> $DIR/rename.rs:73:9
142+
--> $DIR/rename.rs:74:9
143143
|
144144
LL | #![warn(clippy::result_map_unwrap_or_else)]
145145
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or`
146146

147147
error: lint `clippy::result_unwrap_used` has been renamed to `clippy::unwrap_used`
148-
--> $DIR/rename.rs:74:9
148+
--> $DIR/rename.rs:75:9
149149
|
150150
LL | #![warn(clippy::result_unwrap_used)]
151151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::unwrap_used`
152152

153153
error: lint `clippy::single_char_push_str` has been renamed to `clippy::single_char_add_str`
154-
--> $DIR/rename.rs:75:9
154+
--> $DIR/rename.rs:76:9
155155
|
156156
LL | #![warn(clippy::single_char_push_str)]
157157
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::single_char_add_str`
158158

159159
error: lint `clippy::stutter` has been renamed to `clippy::module_name_repetitions`
160-
--> $DIR/rename.rs:76:9
160+
--> $DIR/rename.rs:77:9
161161
|
162162
LL | #![warn(clippy::stutter)]
163163
| ^^^^^^^^^^^^^^^ help: use the new name: `clippy::module_name_repetitions`
164164

165165
error: lint `clippy::to_string_in_display` has been renamed to `clippy::recursive_format_impl`
166-
--> $DIR/rename.rs:77:9
166+
--> $DIR/rename.rs:78:9
167167
|
168168
LL | #![warn(clippy::to_string_in_display)]
169169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::recursive_format_impl`
170170

171171
error: lint `clippy::zero_width_space` has been renamed to `clippy::invisible_characters`
172-
--> $DIR/rename.rs:78:9
172+
--> $DIR/rename.rs:79:9
173173
|
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`
178+
--> $DIR/rename.rs:80:9
179+
|
180+
LL | #![warn(clippy::cast_ref_to_mut)]
181+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `cast_ref_to_mut`
182+
177183
error: lint `clippy::clone_double_ref` has been renamed to `suspicious_double_ref_op`
178-
--> $DIR/rename.rs:79:9
184+
--> $DIR/rename.rs:81:9
179185
|
180186
LL | #![warn(clippy::clone_double_ref)]
181187
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `suspicious_double_ref_op`
182188

183189
error: lint `clippy::drop_bounds` has been renamed to `drop_bounds`
184-
--> $DIR/rename.rs:80:9
190+
--> $DIR/rename.rs:82:9
185191
|
186192
LL | #![warn(clippy::drop_bounds)]
187193
| ^^^^^^^^^^^^^^^^^^^ help: use the new name: `drop_bounds`
188194

189195
error: lint `clippy::drop_copy` has been renamed to `dropping_copy_types`
190-
--> $DIR/rename.rs:81:9
196+
--> $DIR/rename.rs:83:9
191197
|
192198
LL | #![warn(clippy::drop_copy)]
193199
| ^^^^^^^^^^^^^^^^^ help: use the new name: `dropping_copy_types`
194200

195201
error: lint `clippy::drop_ref` has been renamed to `dropping_references`
196-
--> $DIR/rename.rs:82:9
202+
--> $DIR/rename.rs:84:9
197203
|
198204
LL | #![warn(clippy::drop_ref)]
199205
| ^^^^^^^^^^^^^^^^ help: use the new name: `dropping_references`
200206

201207
error: lint `clippy::for_loop_over_option` has been renamed to `for_loops_over_fallibles`
202-
--> $DIR/rename.rs:83:9
208+
--> $DIR/rename.rs:85:9
203209
|
204210
LL | #![warn(clippy::for_loop_over_option)]
205211
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles`
206212

207213
error: lint `clippy::for_loop_over_result` has been renamed to `for_loops_over_fallibles`
208-
--> $DIR/rename.rs:84:9
214+
--> $DIR/rename.rs:86:9
209215
|
210216
LL | #![warn(clippy::for_loop_over_result)]
211217
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles`
212218

213219
error: lint `clippy::for_loops_over_fallibles` has been renamed to `for_loops_over_fallibles`
214-
--> $DIR/rename.rs:85:9
220+
--> $DIR/rename.rs:87:9
215221
|
216222
LL | #![warn(clippy::for_loops_over_fallibles)]
217223
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles`
218224

219225
error: lint `clippy::forget_copy` has been renamed to `forgetting_copy_types`
220-
--> $DIR/rename.rs:86:9
226+
--> $DIR/rename.rs:88:9
221227
|
222228
LL | #![warn(clippy::forget_copy)]
223229
| ^^^^^^^^^^^^^^^^^^^ help: use the new name: `forgetting_copy_types`
224230

225231
error: lint `clippy::forget_ref` has been renamed to `forgetting_references`
226-
--> $DIR/rename.rs:87:9
232+
--> $DIR/rename.rs:89:9
227233
|
228234
LL | #![warn(clippy::forget_ref)]
229235
| ^^^^^^^^^^^^^^^^^^ help: use the new name: `forgetting_references`
230236

231237
error: lint `clippy::into_iter_on_array` has been renamed to `array_into_iter`
232-
--> $DIR/rename.rs:88:9
238+
--> $DIR/rename.rs:90:9
233239
|
234240
LL | #![warn(clippy::into_iter_on_array)]
235241
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `array_into_iter`
236242

237243
error: lint `clippy::invalid_atomic_ordering` has been renamed to `invalid_atomic_ordering`
238-
--> $DIR/rename.rs:89:9
244+
--> $DIR/rename.rs:91:9
239245
|
240246
LL | #![warn(clippy::invalid_atomic_ordering)]
241247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_atomic_ordering`
242248

243249
error: lint `clippy::invalid_ref` has been renamed to `invalid_value`
244-
--> $DIR/rename.rs:90:9
250+
--> $DIR/rename.rs:92:9
245251
|
246252
LL | #![warn(clippy::invalid_ref)]
247253
| ^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_value`
248254

249255
error: lint `clippy::invalid_utf8_in_unchecked` has been renamed to `invalid_from_utf8_unchecked`
250-
--> $DIR/rename.rs:91:9
256+
--> $DIR/rename.rs:93:9
251257
|
252258
LL | #![warn(clippy::invalid_utf8_in_unchecked)]
253259
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_from_utf8_unchecked`
254260

255261
error: lint `clippy::let_underscore_drop` has been renamed to `let_underscore_drop`
256-
--> $DIR/rename.rs:92:9
262+
--> $DIR/rename.rs:94:9
257263
|
258264
LL | #![warn(clippy::let_underscore_drop)]
259265
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `let_underscore_drop`
260266

261267
error: lint `clippy::mem_discriminant_non_enum` has been renamed to `enum_intrinsics_non_enums`
262-
--> $DIR/rename.rs:93:9
268+
--> $DIR/rename.rs:95:9
263269
|
264270
LL | #![warn(clippy::mem_discriminant_non_enum)]
265271
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `enum_intrinsics_non_enums`
266272

267273
error: lint `clippy::panic_params` has been renamed to `non_fmt_panics`
268-
--> $DIR/rename.rs:94:9
274+
--> $DIR/rename.rs:96:9
269275
|
270276
LL | #![warn(clippy::panic_params)]
271277
| ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `non_fmt_panics`
272278

273279
error: lint `clippy::positional_named_format_parameters` has been renamed to `named_arguments_used_positionally`
274-
--> $DIR/rename.rs:95:9
280+
--> $DIR/rename.rs:97:9
275281
|
276282
LL | #![warn(clippy::positional_named_format_parameters)]
277283
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `named_arguments_used_positionally`
278284

279285
error: lint `clippy::temporary_cstring_as_ptr` has been renamed to `temporary_cstring_as_ptr`
280-
--> $DIR/rename.rs:96:9
286+
--> $DIR/rename.rs:98:9
281287
|
282288
LL | #![warn(clippy::temporary_cstring_as_ptr)]
283289
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `temporary_cstring_as_ptr`
284290

285291
error: lint `clippy::unknown_clippy_lints` has been renamed to `unknown_lints`
286-
--> $DIR/rename.rs:97:9
292+
--> $DIR/rename.rs:99:9
287293
|
288294
LL | #![warn(clippy::unknown_clippy_lints)]
289295
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unknown_lints`
290296

291297
error: lint `clippy::unused_label` has been renamed to `unused_labels`
292-
--> $DIR/rename.rs:98:9
298+
--> $DIR/rename.rs:100:9
293299
|
294300
LL | #![warn(clippy::unused_label)]
295301
| ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unused_labels`
296302

297-
error: aborting due to 49 previous errors
303+
error: aborting due to 50 previous errors
298304

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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)]
5+
46
fn main() {
57
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
68
let y = unsafe { &mut *(x as *const i32 as *mut i32) };

‎src/tools/miri/tests/fail/stacked_borrows/shr_frozen_violation1.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(cast_ref_to_mut)]
2+
13
fn foo(x: &mut i32) -> i32 {
24
*x = 5;
35
unknown_code(&*x);

‎tests/ui/const-generics/issues/issue-100313.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ 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
1213
}
1314
}
1415
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
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+
19
error[E0080]: evaluation of constant value failed
210
--> $DIR/issue-100313.rs:10:13
311
|
@@ -10,11 +18,11 @@ note: inside `T::<&true>::set_false`
1018
LL | *(B as *const bool as *mut bool) = false;
1119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1220
note: inside `_`
13-
--> $DIR/issue-100313.rs:18:5
21+
--> $DIR/issue-100313.rs:19:5
1422
|
1523
LL | x.set_false();
1624
| ^^^^^^^^^^^^^
1725

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

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

‎tests/ui/lint/cast_ref_to_mut.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// check-fail
2+
3+
#![feature(ptr_from_ref)]
4+
5+
extern "C" {
6+
// N.B., mutability can be easily incorrect in FFI calls -- as
7+
// in C, the default is mutable pointers.
8+
fn ffi(c: *mut u8);
9+
fn int_ffi(c: *mut i32);
10+
}
11+
12+
fn main() {
13+
let s = String::from("Hello");
14+
let a = &s;
15+
unsafe {
16+
let num = &3i32;
17+
let mut_num = &mut 3i32;
18+
19+
(*(a as *const _ as *mut String)).push_str(" world");
20+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
21+
*(a as *const _ as *mut _) = String::from("Replaced");
22+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
23+
*(a as *const _ as *mut String) += " world";
24+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
25+
let _num = &mut *(num as *const i32 as *mut i32);
26+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
27+
let _num = &mut *(num as *const i32).cast_mut();
28+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
29+
let _num = *{ num as *const i32 }.cast_mut();
30+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
31+
*std::ptr::from_ref(num).cast_mut() += 1;
32+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
33+
*std::ptr::from_ref({ num }).cast_mut() += 1;
34+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
35+
*{ std::ptr::from_ref(num) }.cast_mut() += 1;
36+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
37+
*(std::ptr::from_ref({ num }) as *mut i32) += 1;
38+
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
39+
40+
// Shouldn't be warned against
41+
println!("{}", *(num as *const _ as *const i16));
42+
println!("{}", *(mut_num as *mut _ as *mut i16));
43+
ffi(a.as_ptr() as *mut _);
44+
int_ffi(num as *const _ as *mut _);
45+
int_ffi(&3 as *const _ as *mut _);
46+
let mut value = 3;
47+
let value: *const i32 = &mut value;
48+
*(value as *const i16 as *mut i16) = 42;
49+
}
50+
}

‎tests/ui/lint/cast_ref_to_mut.stderr

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
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
3+
|
4+
LL | (*(a as *const _ as *mut String)).push_str(" world");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[deny(cast_ref_to_mut)]` on by default
8+
9+
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
11+
|
12+
LL | *(a as *const _ as *mut _) = String::from("Replaced");
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
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
17+
|
18+
LL | *(a as *const _ as *mut String) += " world";
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
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
23+
|
24+
LL | let _num = &mut *(num as *const i32 as *mut i32);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
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
29+
|
30+
LL | let _num = &mut *(num as *const i32).cast_mut();
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
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
35+
|
36+
LL | let _num = *{ num as *const i32 }.cast_mut();
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
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
41+
|
42+
LL | *std::ptr::from_ref(num).cast_mut() += 1;
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
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
47+
|
48+
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
51+
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
53+
|
54+
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
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
59+
|
60+
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
62+
63+
error: aborting due to 10 previous errors
64+

0 commit comments

Comments
 (0)
Please sign in to comment.