diff --git a/clippy_lints/src/deprecated_lints.rs b/clippy_lints/src/deprecated_lints.rs index e51d7cc6d..1edeb3056 100644 --- a/clippy_lints/src/deprecated_lints.rs +++ b/clippy_lints/src/deprecated_lints.rs @@ -71,3 +71,14 @@ declare_deprecated_lint! { pub STRING_TO_STRING, "using `string::to_string` is common even today and specialization will likely happen soon" } + +/// **What it does:** Nothing. This lint has been deprecated. +/// +/// **Deprecation reason:** This lint should never have applied to non-pointer types, as transmuting +/// between non-pointer types of differing alignment is well-defined behavior (it's semantically +/// equivalent to a memcpy). This lint has thus been refactored into two separate lints: +/// cast_ptr_alignment and transmute_ptr_to_ptr. +declare_deprecated_lint! { + pub MISALIGNED_TRANSMUTE, + "this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr" +} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index a9ebd4dfd..190d65be8 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -277,6 +277,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { "string_to_string", "using `string::to_string` is common even today and specialization will likely happen soon", ); + store.register_removed( + "misaligned_transmute", + "this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr", + ); // end deprecated lints, do not remove this comment, it’s used in `update_lints` reg.register_late_lint_pass(box serde_api::Serde); @@ -635,7 +639,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { swap::MANUAL_SWAP, temporary_assignment::TEMPORARY_ASSIGNMENT, transmute::CROSSPOINTER_TRANSMUTE, - transmute::MISALIGNED_TRANSMUTE, transmute::TRANSMUTE_BYTES_TO_STR, transmute::TRANSMUTE_INT_TO_BOOL, transmute::TRANSMUTE_INT_TO_CHAR, @@ -787,7 +790,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { swap::MANUAL_SWAP, temporary_assignment::TEMPORARY_ASSIGNMENT, transmute::CROSSPOINTER_TRANSMUTE, - transmute::MISALIGNED_TRANSMUTE, transmute::TRANSMUTE_BYTES_TO_STR, transmute::TRANSMUTE_INT_TO_BOOL, transmute::TRANSMUTE_INT_TO_CHAR, diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index 7b591a583..68321e7bd 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -1,7 +1,6 @@ use rustc::lint::*; use rustc::ty::{self, Ty}; use rustc::hir::*; -use rustc::ty::layout::LayoutOf; use std::borrow::Cow; use syntax::ast; use utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then}; @@ -169,23 +168,6 @@ declare_clippy_lint! { "transmutes from an integer to a float" } -/// **What it does:** Checks for transmutes to a potentially less-aligned type. -/// -/// **Why is this bad?** This might result in undefined behavior. -/// -/// **Known problems:** None. -/// -/// **Example:** -/// ```rust -/// // u32 is 32-bit aligned; u8 is 8-bit aligned -/// let _: u32 = unsafe { std::mem::transmute([0u8; 4]) }; -/// ``` -declare_clippy_lint! { - pub MISALIGNED_TRANSMUTE, - complexity, - "transmutes to a potentially less-aligned type" -} - /// **What it does:** Checks for transmutes from a pointer to a pointer, or /// from a reference to a reference. /// @@ -227,7 +209,6 @@ impl LintPass for Transmute { TRANSMUTE_BYTES_TO_STR, TRANSMUTE_INT_TO_BOOL, TRANSMUTE_INT_TO_FLOAT, - MISALIGNED_TRANSMUTE ) } } @@ -248,18 +229,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute { e.span, &format!("transmute from a type (`{}`) to itself", from_ty), ), - _ if cx.layout_of(from_ty).ok().map(|a| a.align.abi()) - < cx.layout_of(to_ty).ok().map(|a| a.align.abi()) - => span_lint( - cx, - MISALIGNED_TRANSMUTE, - e.span, - &format!( - "transmute from `{}` to a less-aligned type (`{}`)", - from_ty, - to_ty, - ) - ), (&ty::TyRef(_, rty), &ty::TyRawPtr(ptr_ty)) => span_lint_and_then( cx, USELESS_TRANSMUTE, diff --git a/tests/ui/deprecated.rs b/tests/ui/deprecated.rs index 0598e174e..f456c4172 100644 --- a/tests/ui/deprecated.rs +++ b/tests/ui/deprecated.rs @@ -9,4 +9,6 @@ #[warn(unstable_as_mut_slice)] +#[warn(misaligned_transmute)] + fn main() {} diff --git a/tests/ui/deprecated.stderr b/tests/ui/deprecated.stderr index 7d5d594cf..aa62ccbd0 100644 --- a/tests/ui/deprecated.stderr +++ b/tests/ui/deprecated.stderr @@ -24,5 +24,11 @@ error: lint unstable_as_mut_slice has been removed: `Vec::as_mut_slice` has been 10 | #[warn(unstable_as_mut_slice)] | ^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 4 previous errors +error: lint misaligned_transmute has been removed: this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr + --> $DIR/deprecated.rs:12:8 + | +12 | #[warn(misaligned_transmute)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 5 previous errors diff --git a/tests/ui/transmute.rs b/tests/ui/transmute.rs index 3ff444b38..7c5e3f03d 100644 --- a/tests/ui/transmute.rs +++ b/tests/ui/transmute.rs @@ -140,13 +140,6 @@ fn bytes_to_str(b: &[u8], mb: &mut [u8]) { let _: &mut str = unsafe { std::mem::transmute(mb) }; } -#[warn(misaligned_transmute)] -fn misaligned_transmute() { - let _: u32 = unsafe { std::mem::transmute([0u8; 4]) }; // err - let _: u32 = unsafe { std::mem::transmute(0f32) }; // ok (alignment-wise) - let _: [u8; 4] = unsafe { std::mem::transmute(0u32) }; // ok (alignment-wise) -} - #[warn(transmute_ptr_to_ptr)] fn transmute_ptr_to_ptr() { let ptr = &1u32 as *const u32; diff --git a/tests/ui/transmute.stderr b/tests/ui/transmute.stderr index f7090f0dc..a343a8a9c 100644 --- a/tests/ui/transmute.stderr +++ b/tests/ui/transmute.stderr @@ -204,39 +204,31 @@ error: transmute from a `&mut [u8]` to a `&mut str` 140 | let _: &mut str = unsafe { std::mem::transmute(mb) }; | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()` -error: transmute from `[u8; 4]` to a less-aligned type (`u32`) - --> $DIR/transmute.rs:145:27 - | -145 | let _: u32 = unsafe { std::mem::transmute([0u8; 4]) }; // err - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: `-D misaligned-transmute` implied by `-D warnings` - error: transmute from a pointer to a pointer - --> $DIR/transmute.rs:156:29 + --> $DIR/transmute.rs:149:29 | -156 | let _: *const f32 = std::mem::transmute(ptr); +149 | let _: *const f32 = std::mem::transmute(ptr); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr as *const f32` | = note: `-D transmute-ptr-to-ptr` implied by `-D warnings` error: transmute from a pointer to a pointer - --> $DIR/transmute.rs:157:27 + --> $DIR/transmute.rs:150:27 | -157 | let _: *mut f32 = std::mem::transmute(mut_ptr); +150 | let _: *mut f32 = std::mem::transmute(mut_ptr); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `mut_ptr as *mut f32` error: transmute from a reference to a reference - --> $DIR/transmute.rs:159:23 + --> $DIR/transmute.rs:152:23 | -159 | let _: &f32 = std::mem::transmute(&1u32); +152 | let _: &f32 = std::mem::transmute(&1u32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)` error: transmute from a reference to a reference - --> $DIR/transmute.rs:160:27 + --> $DIR/transmute.rs:153:27 | -160 | let _: &mut f32 = std::mem::transmute(&mut 1u32); +153 | let _: &mut f32 = std::mem::transmute(&mut 1u32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)` -error: aborting due to 37 previous errors +error: aborting due to 36 previous errors