@@ -50,6 +50,29 @@ declare_clippy_lint! {
5050 "transmutes that have the same to and from types or could be a cast/coercion"
5151}
5252
53+ // FIXME: Merge this lint with USELESS_TRANSMUTE once that is out of the nursery.
54+ declare_clippy_lint ! {
55+ /// **What it does:**Checks for transmutes that could be a pointer cast.
56+ ///
57+ /// **Why is this bad?** Readability. The code tricks people into thinking that
58+ /// something complex is going on.
59+ ///
60+ /// **Known problems:** None.
61+ ///
62+ /// **Example:**
63+ ///
64+ /// ```rust,ignore
65+ /// core::intrinsics::transmute::<*const [i32], *const [u16]>(p)
66+ /// ```
67+ /// Use instead:
68+ /// ```rust
69+ /// p as *const [u16]
70+ /// ```
71+ pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS ,
72+ complexity,
73+ "transmutes that could be a pointer cast"
74+ }
75+
5376declare_clippy_lint ! {
5477 /// **What it does:** Checks for transmutes between a type `T` and `*T`.
5578 ///
@@ -272,27 +295,6 @@ declare_clippy_lint! {
272295 "transmute between collections of layout-incompatible types"
273296}
274297
275- declare_clippy_lint ! {
276- /// **What it does:**
277- ///
278- /// **Why is this bad?**
279- ///
280- /// **Known problems:** None.
281- ///
282- /// **Example:**
283- ///
284- /// ```rust
285- /// // example code where clippy issues a warning
286- /// ```
287- /// Use instead:
288- /// ```rust
289- /// // example code which does not raise clippy warning
290- /// ```
291- pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS ,
292- complexity,
293- "default lint description"
294- }
295-
296298declare_lint_pass ! ( Transmute => [
297299 CROSSPOINTER_TRANSMUTE ,
298300 TRANSMUTE_PTR_TO_REF ,
@@ -330,26 +332,6 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
330332 let from_ty = cx. typeck_results( ) . expr_ty( & args[ 0 ] ) ;
331333 let to_ty = cx. typeck_results( ) . expr_ty( e) ;
332334
333- if can_be_expressed_as_pointer_cast( cx, e, from_ty, to_ty) {
334- span_lint_and_then(
335- cx,
336- TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS ,
337- e. span,
338- & format!(
339- "transmute from `{}` to `{}` which could be expressed as a pointer cast instead" ,
340- from_ty,
341- to_ty
342- ) ,
343- |diag| {
344- if let Some ( arg) = sugg:: Sugg :: hir_opt( cx, & args[ 0 ] ) {
345- let sugg = format!( "{} as {}" , arg, to_ty) ;
346- diag. span_suggestion( e. span, "try" , sugg, Applicability :: Unspecified ) ;
347- }
348- }
349- ) ;
350- return
351- }
352-
353335 match ( & from_ty. kind, & to_ty. kind) {
354336 _ if from_ty == to_ty => span_lint(
355337 cx,
@@ -646,6 +628,22 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
646628 ) ;
647629 }
648630 } ,
631+ ( _, _) if can_be_expressed_as_pointer_cast( cx, e, from_ty, to_ty) => span_lint_and_then(
632+ cx,
633+ TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS ,
634+ e. span,
635+ & format!(
636+ "transmute from `{}` to `{}` which could be expressed as a pointer cast instead" ,
637+ from_ty,
638+ to_ty
639+ ) ,
640+ |diag| {
641+ if let Some ( arg) = sugg:: Sugg :: hir_opt( cx, & args[ 0 ] ) {
642+ let sugg = arg. as_ty( & to_ty. to_string( ) ) . to_string( ) ;
643+ diag. span_suggestion( e. span, "try" , sugg, Applicability :: Unspecified ) ;
644+ }
645+ }
646+ ) ,
649647 _ => {
650648 return ;
651649 } ,
0 commit comments