Skip to content

Commit b77d740

Browse files
Deprecate misaligned_transmute
1 parent c6bc682 commit b77d740

File tree

7 files changed

+33
-58
lines changed

7 files changed

+33
-58
lines changed

clippy_lints/src/deprecated_lints.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,14 @@ declare_deprecated_lint! {
7171
pub STRING_TO_STRING,
7272
"using `string::to_string` is common even today and specialization will likely happen soon"
7373
}
74+
75+
/// **What it does:** Nothing. This lint has been deprecated.
76+
///
77+
/// **Deprecation reason:** This lint should never have applied to non-pointer types, as transmuting
78+
/// between non-pointer types of differing alignment is well-defined behavior (it's semantically
79+
/// equivalent to a memcpy). This lint has thus been refactored into two separate lints:
80+
/// cast_ptr_alignment and transmute_ptr_to_ptr.
81+
declare_deprecated_lint! {
82+
pub MISALIGNED_TRANSMUTE,
83+
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr"
84+
}

clippy_lints/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
277277
"string_to_string",
278278
"using `string::to_string` is common even today and specialization will likely happen soon",
279279
);
280+
store.register_removed(
281+
"misaligned_transmute",
282+
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr",
283+
);
280284
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
281285

282286
reg.register_late_lint_pass(box serde_api::Serde);
@@ -635,7 +639,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
635639
swap::MANUAL_SWAP,
636640
temporary_assignment::TEMPORARY_ASSIGNMENT,
637641
transmute::CROSSPOINTER_TRANSMUTE,
638-
transmute::MISALIGNED_TRANSMUTE,
639642
transmute::TRANSMUTE_BYTES_TO_STR,
640643
transmute::TRANSMUTE_INT_TO_BOOL,
641644
transmute::TRANSMUTE_INT_TO_CHAR,
@@ -787,7 +790,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
787790
swap::MANUAL_SWAP,
788791
temporary_assignment::TEMPORARY_ASSIGNMENT,
789792
transmute::CROSSPOINTER_TRANSMUTE,
790-
transmute::MISALIGNED_TRANSMUTE,
791793
transmute::TRANSMUTE_BYTES_TO_STR,
792794
transmute::TRANSMUTE_INT_TO_BOOL,
793795
transmute::TRANSMUTE_INT_TO_CHAR,

clippy_lints/src/transmute.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc::lint::*;
22
use rustc::ty::{self, Ty};
33
use rustc::hir::*;
4-
use rustc::ty::layout::LayoutOf;
54
use std::borrow::Cow;
65
use syntax::ast;
76
use utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then};
@@ -169,23 +168,6 @@ declare_clippy_lint! {
169168
"transmutes from an integer to a float"
170169
}
171170

172-
/// **What it does:** Checks for transmutes to a potentially less-aligned type.
173-
///
174-
/// **Why is this bad?** This might result in undefined behavior.
175-
///
176-
/// **Known problems:** None.
177-
///
178-
/// **Example:**
179-
/// ```rust
180-
/// // u32 is 32-bit aligned; u8 is 8-bit aligned
181-
/// let _: u32 = unsafe { std::mem::transmute([0u8; 4]) };
182-
/// ```
183-
declare_clippy_lint! {
184-
pub MISALIGNED_TRANSMUTE,
185-
complexity,
186-
"transmutes to a potentially less-aligned type"
187-
}
188-
189171
/// **What it does:** Checks for transmutes from a pointer to a pointer, or
190172
/// from a reference to a reference.
191173
///
@@ -227,7 +209,6 @@ impl LintPass for Transmute {
227209
TRANSMUTE_BYTES_TO_STR,
228210
TRANSMUTE_INT_TO_BOOL,
229211
TRANSMUTE_INT_TO_FLOAT,
230-
MISALIGNED_TRANSMUTE
231212
)
232213
}
233214
}
@@ -248,18 +229,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
248229
e.span,
249230
&format!("transmute from a type (`{}`) to itself", from_ty),
250231
),
251-
_ if cx.layout_of(from_ty).ok().map(|a| a.align.abi())
252-
< cx.layout_of(to_ty).ok().map(|a| a.align.abi())
253-
=> span_lint(
254-
cx,
255-
MISALIGNED_TRANSMUTE,
256-
e.span,
257-
&format!(
258-
"transmute from `{}` to a less-aligned type (`{}`)",
259-
from_ty,
260-
to_ty,
261-
)
262-
),
263232
(&ty::TyRef(_, rty), &ty::TyRawPtr(ptr_ty)) => span_lint_and_then(
264233
cx,
265234
USELESS_TRANSMUTE,

tests/ui/deprecated.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99

1010
#[warn(unstable_as_mut_slice)]
1111

12+
#[warn(misaligned_transmute)]
13+
1214
fn main() {}

tests/ui/deprecated.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ error: lint unstable_as_mut_slice has been removed: `Vec::as_mut_slice` has been
2424
10 | #[warn(unstable_as_mut_slice)]
2525
| ^^^^^^^^^^^^^^^^^^^^^
2626

27-
error: aborting due to 4 previous errors
27+
error: lint misaligned_transmute has been removed: this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr
28+
--> $DIR/deprecated.rs:12:8
29+
|
30+
12 | #[warn(misaligned_transmute)]
31+
| ^^^^^^^^^^^^^^^^^^^^
32+
33+
error: aborting due to 5 previous errors
2834

tests/ui/transmute.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,6 @@ fn bytes_to_str(b: &[u8], mb: &mut [u8]) {
140140
let _: &mut str = unsafe { std::mem::transmute(mb) };
141141
}
142142

143-
#[warn(misaligned_transmute)]
144-
fn misaligned_transmute() {
145-
let _: u32 = unsafe { std::mem::transmute([0u8; 4]) }; // err
146-
let _: u32 = unsafe { std::mem::transmute(0f32) }; // ok (alignment-wise)
147-
let _: [u8; 4] = unsafe { std::mem::transmute(0u32) }; // ok (alignment-wise)
148-
}
149-
150143
#[warn(transmute_ptr_to_ptr)]
151144
fn transmute_ptr_to_ptr() {
152145
let ptr = &1u32 as *const u32;

tests/ui/transmute.stderr

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -204,39 +204,31 @@ error: transmute from a `&mut [u8]` to a `&mut str`
204204
140 | let _: &mut str = unsafe { std::mem::transmute(mb) };
205205
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`
206206

207-
error: transmute from `[u8; 4]` to a less-aligned type (`u32`)
208-
--> $DIR/transmute.rs:145:27
209-
|
210-
145 | let _: u32 = unsafe { std::mem::transmute([0u8; 4]) }; // err
211-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
212-
|
213-
= note: `-D misaligned-transmute` implied by `-D warnings`
214-
215207
error: transmute from a pointer to a pointer
216-
--> $DIR/transmute.rs:156:29
208+
--> $DIR/transmute.rs:149:29
217209
|
218-
156 | let _: *const f32 = std::mem::transmute(ptr);
210+
149 | let _: *const f32 = std::mem::transmute(ptr);
219211
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr as *const f32`
220212
|
221213
= note: `-D transmute-ptr-to-ptr` implied by `-D warnings`
222214

223215
error: transmute from a pointer to a pointer
224-
--> $DIR/transmute.rs:157:27
216+
--> $DIR/transmute.rs:150:27
225217
|
226-
157 | let _: *mut f32 = std::mem::transmute(mut_ptr);
218+
150 | let _: *mut f32 = std::mem::transmute(mut_ptr);
227219
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `mut_ptr as *mut f32`
228220

229221
error: transmute from a reference to a reference
230-
--> $DIR/transmute.rs:159:23
222+
--> $DIR/transmute.rs:152:23
231223
|
232-
159 | let _: &f32 = std::mem::transmute(&1u32);
224+
152 | let _: &f32 = std::mem::transmute(&1u32);
233225
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)`
234226

235227
error: transmute from a reference to a reference
236-
--> $DIR/transmute.rs:160:27
228+
--> $DIR/transmute.rs:153:27
237229
|
238-
160 | let _: &mut f32 = std::mem::transmute(&mut 1u32);
230+
153 | let _: &mut f32 = std::mem::transmute(&mut 1u32);
239231
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)`
240232

241-
error: aborting due to 37 previous errors
233+
error: aborting due to 36 previous errors
242234

0 commit comments

Comments
 (0)