diff options
| author | Yusuke Tanaka <yusuktan@maguro.dev> | 2021-02-11 01:27:44 +0900 |
|---|---|---|
| committer | flip1995 <philipp.krones@embecosm.com> | 2021-03-02 10:40:24 +0100 |
| commit | 25c221ec92bdf7be29335f4ea830edb9509a8ce9 (patch) | |
| tree | 16f261bc42384d105c1183ae95d3d131801e1c28 | |
| parent | c57a8260f22d4968f8c802ce92593a32e1e27f4c (diff) | |
| download | rust-25c221ec92bdf7be29335f4ea830edb9509a8ce9.tar.gz rust-25c221ec92bdf7be29335f4ea830edb9509a8ce9.zip | |
Move transmutes_expressible_as_ptr_casts to its own module
| -rw-r--r-- | clippy_lints/src/transmute/mod.rs | 25 | ||||
| -rw-r--r-- | clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs | 38 |
2 files changed, 42 insertions, 21 deletions
diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs index 8cb4f650057..3b010f39c8a 100644 --- a/clippy_lints/src/transmute/mod.rs +++ b/clippy_lints/src/transmute/mod.rs @@ -6,6 +6,7 @@ mod transmute_int_to_float; mod transmute_ptr_to_ptr; mod transmute_ptr_to_ref; mod transmute_ref_to_ref; +mod transmutes_expressible_as_ptr_casts; mod unsound_collection_transmute; mod useless_transmute; mod utils; @@ -389,27 +390,9 @@ impl<'tcx> LateLintPass<'tcx> for Transmute { if triggered { return; } - - match (&from_ty.kind(), &to_ty.kind()) { - (_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then( - cx, - TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, - e.span, - &format!( - "transmute from `{}` to `{}` which could be expressed as a pointer cast instead", - from_ty, - to_ty - ), - |diag| { - if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) { - let sugg = arg.as_ty(&to_ty.to_string()).to_string(); - diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable); - } - } - ), - _ => { - return; - }, + let triggered = transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, to_ty, args); + if triggered { + return; } } } diff --git a/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs b/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs new file mode 100644 index 00000000000..dea896622f1 --- /dev/null +++ b/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs @@ -0,0 +1,38 @@ +use super::utils::can_be_expressed_as_pointer_cast; +use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS; +use crate::utils::{span_lint_and_then, sugg}; +use rustc_errors::Applicability; +use rustc_hir::Expr; +use rustc_lint::LateContext; +use rustc_middle::ty::Ty; + +/// Checks for `transmutes_expressible_as_ptr_casts` lint. +/// Returns `true` if it's triggered, otherwise returns `false`. +pub(super) fn check<'tcx>( + cx: &LateContext<'tcx>, + e: &'tcx Expr<'_>, + from_ty: Ty<'tcx>, + to_ty: Ty<'tcx>, + args: &'tcx [Expr<'_>], +) -> bool { + if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) { + span_lint_and_then( + cx, + TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, + e.span, + &format!( + "transmute from `{}` to `{}` which could be expressed as a pointer cast instead", + from_ty, to_ty + ), + |diag| { + if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) { + let sugg = arg.as_ty(&to_ty.to_string()).to_string(); + diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable); + } + }, + ); + true + } else { + false + } +} |
