diff --git a/CHANGELOG.md b/CHANGELOG.md index 7db95c081aa3..89709e082ef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6649,6 +6649,7 @@ Released 2018-09-13 [`mut_mut`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mut [`mut_mutex_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mutex_lock [`mut_range_bound`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_range_bound +[`mutable_adt_argument_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutable_adt_argument_transmute [`mutable_key_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type [`mutex_atomic`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutex_atomic [`mutex_integer`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 4a350dca2993..da61e008f313 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -716,6 +716,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[ crate::transmute::CROSSPOINTER_TRANSMUTE_INFO, crate::transmute::EAGER_TRANSMUTE_INFO, crate::transmute::MISSING_TRANSMUTE_ANNOTATIONS_INFO, + crate::transmute::MUTABLE_ADT_ARGUMENT_TRANSMUTE_INFO, crate::transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS_INFO, crate::transmute::TRANSMUTE_BYTES_TO_STR_INFO, crate::transmute::TRANSMUTE_INT_TO_BOOL_INFO, diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs index d643f7aea497..55a94be32ad7 100644 --- a/clippy_lints/src/transmute/mod.rs +++ b/clippy_lints/src/transmute/mod.rs @@ -1,6 +1,7 @@ mod crosspointer_transmute; mod eager_transmute; mod missing_transmute_annotations; +mod transmute_adt_argument; mod transmute_int_to_bool; mod transmute_int_to_non_zero; mod transmute_null_to_fn; @@ -44,6 +45,22 @@ declare_clippy_lint! { correctness, "transmutes that are confusing at best, undefined behavior at worst and always useless" } +declare_clippy_lint! { + /// ### What it does + /// Checks for transmutes between the same adt, where at least one of the type argument goes from &T to &mut T. + /// This is an a more complicated version of https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#mutable-transmutes. + /// ### Example + /// + /// ```ignore + /// unsafe { + /// std::mem::transmute::, Option<&mut i32>>(Some(&5)); + /// } + /// ``` + #[clippy::version = "1.92.0"] + pub MUTABLE_ADT_ARGUMENT_TRANSMUTE, + correctness, + "transmutes on the same adt where at least one of the type argument goes from &T to &mut T" +} declare_clippy_lint! { /// ### What it does @@ -476,6 +493,7 @@ impl_lint_pass!(Transmute => [ USELESS_TRANSMUTE, WRONG_TRANSMUTE, TRANSMUTE_BYTES_TO_STR, + MUTABLE_ADT_ARGUMENT_TRANSMUTE, TRANSMUTE_INT_TO_BOOL, TRANSMUTE_INT_TO_NON_ZERO, UNSOUND_COLLECTION_TRANSMUTE, @@ -517,6 +535,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute { } let linted = wrong_transmute::check(cx, e, from_ty, to_ty) + | transmute_adt_argument::check(cx, e, from_ty, to_ty) | crosspointer_transmute::check(cx, e, from_ty, to_ty) | transmuting_null::check(cx, e, arg, to_ty) | transmute_null_to_fn::check(cx, e, arg, to_ty) diff --git a/clippy_lints/src/transmute/transmute_adt_argument.rs b/clippy_lints/src/transmute/transmute_adt_argument.rs new file mode 100644 index 000000000000..681334309280 --- /dev/null +++ b/clippy_lints/src/transmute/transmute_adt_argument.rs @@ -0,0 +1,62 @@ +use super::MUTABLE_ADT_ARGUMENT_TRANSMUTE; +use clippy_utils::diagnostics::span_lint; +use rustc_hir::Expr; +use rustc_lint::LateContext; +use rustc_middle::ty::{self, GenericArgKind, Ty}; + +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool { + // assumes walk will return all types in the same order + let mut from_ty_walker = from_ty.walk(); + let mut to_ty_walker = to_ty.walk(); + let mut found = false; + while let Some((from_ty, to_ty)) = from_ty_walker.next().zip(to_ty_walker.next()) { + if let (GenericArgKind::Type(from_ty), GenericArgKind::Type(to_ty)) = (from_ty.kind(), to_ty.kind()) { + match (from_ty.kind(), to_ty.kind()) { + (ty::Bool, ty::Bool) + | (ty::Char, ty::Char) + | (ty::Int(_), ty::Int(_)) + | (ty::Uint(_), ty::Uint(_)) + | (ty::Float(_), ty::Float(_)) + | (ty::Foreign(_), ty::Foreign(_)) + | (ty::Str, ty::Str) + | (ty::Array(_, _), ty::Array(_, _)) + | (ty::Pat(_, _), ty::Pat(_, _)) + | (ty::Slice(_), ty::Slice(_)) + | (ty::RawPtr(_, _), ty::RawPtr(_, _)) + | (ty::FnDef(_, _), ty::FnDef(_, _)) + | (ty::FnPtr(_, _), ty::FnPtr(_, _)) + | (ty::UnsafeBinder(_), ty::UnsafeBinder(_)) + | (ty::Dynamic(_, _), ty::Dynamic(_, _)) + | (ty::Closure(_, _), ty::Closure(_, _)) + | (ty::CoroutineClosure(_, _), ty::CoroutineClosure(_, _)) + | (ty::Coroutine(_, _), ty::Coroutine(_, _)) + | (ty::CoroutineWitness(_, _), ty::CoroutineWitness(_, _)) + | (ty::Never, ty::Never) + | (ty::Tuple(_), ty::Tuple(_)) + | (ty::Alias(_, _), ty::Alias(_, _)) + | (ty::Param(_), ty::Param(_)) + | (ty::Bound(_, _), ty::Bound(_, _)) + | (ty::Placeholder(_), ty::Placeholder(_)) + | (ty::Infer(_), ty::Infer(_)) + | (ty::Error(_), ty::Error(_)) => {}, + (ty::Ref(_, _, from_mut), ty::Ref(_, _, to_mut)) => { + if from_mut < to_mut { + span_lint( + cx, + MUTABLE_ADT_ARGUMENT_TRANSMUTE, + e.span, + format!("transmute of type argument {from_ty} to {to_ty}"), + ); + found = true; + } + }, + (ty::Adt(adt1, _), ty::Adt(adt2, _)) if adt1 == adt2 => {}, + _ => { + from_ty_walker.skip_current_subtree(); + to_ty_walker.skip_current_subtree(); + }, + } + } + } + found +} diff --git a/tests/ui/mutable_adt_argument_transmute.rs b/tests/ui/mutable_adt_argument_transmute.rs new file mode 100644 index 000000000000..08f21f4de446 --- /dev/null +++ b/tests/ui/mutable_adt_argument_transmute.rs @@ -0,0 +1,18 @@ +#![warn(clippy::mutable_adt_argument_transmute)] + +fn main() { + unsafe { + let _: Option<&mut i32> = std::mem::transmute(Some(&5i32)); + //~^ mutable_adt_argument_transmute + let _: Result<&mut i32, ()> = std::mem::transmute(Result::<&i32, ()>::Ok(&5i32)); + //~^ mutable_adt_argument_transmute + let _: Result, ()> = + std::mem::transmute(Result::, ()>::Ok(Some(&"foo".to_string()))); + //~^ mutable_adt_argument_transmute + let _: Result<&mut f32, &usize> = std::mem::transmute(Result::<&f32, &usize>::Ok(&2f32)); + //~^ mutable_adt_argument_transmute + let _: Result<(), &mut usize> = std::mem::transmute(Result::<(), &usize>::Ok(())); + //~^ mutable_adt_argument_transmute + let _: Option<&i32> = std::mem::transmute(Some(&5i32)); + } +} diff --git a/tests/ui/mutable_adt_argument_transmute.stderr b/tests/ui/mutable_adt_argument_transmute.stderr new file mode 100644 index 000000000000..477bc18ed22c --- /dev/null +++ b/tests/ui/mutable_adt_argument_transmute.stderr @@ -0,0 +1,35 @@ +error: transmute of type argument &i32 to &mut i32 + --> tests/ui/mutable_adt_argument_transmute.rs:5:35 + | +LL | let _: Option<&mut i32> = std::mem::transmute(Some(&5i32)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::mutable-adt-argument-transmute` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::mutable_adt_argument_transmute)]` + +error: transmute of type argument &i32 to &mut i32 + --> tests/ui/mutable_adt_argument_transmute.rs:7:39 + | +LL | let _: Result<&mut i32, ()> = std::mem::transmute(Result::<&i32, ()>::Ok(&5i32)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: transmute of type argument &std::string::String to &mut std::string::String + --> tests/ui/mutable_adt_argument_transmute.rs:10:13 + | +LL | std::mem::transmute(Result::, ()>::Ok(Some(&"foo".to_string()))); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: transmute of type argument &f32 to &mut f32 + --> tests/ui/mutable_adt_argument_transmute.rs:12:43 + | +LL | let _: Result<&mut f32, &usize> = std::mem::transmute(Result::<&f32, &usize>::Ok(&2f32)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: transmute of type argument &usize to &mut usize + --> tests/ui/mutable_adt_argument_transmute.rs:14:41 + | +LL | let _: Result<(), &mut usize> = std::mem::transmute(Result::<(), &usize>::Ok(())); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 5 previous errors +