]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/transmute.rs
Allow UUID style formatting for `inconsistent_digit_grouping` lint
[rust.git] / clippy_lints / src / transmute.rs
1 use crate::utils::{
2     is_normalizable, last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then, sugg,
3 };
4 use if_chain::if_chain;
5 use rustc_ast::ast;
6 use rustc_errors::Applicability;
7 use rustc_hir::{Expr, ExprKind, GenericArg, Mutability, QPath, TyKind, UnOp};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::ty::{self, Ty};
10 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 use std::borrow::Cow;
12
13 declare_clippy_lint! {
14     /// **What it does:** Checks for transmutes that can't ever be correct on any
15     /// architecture.
16     ///
17     /// **Why is this bad?** It's basically guaranteed to be undefined behaviour.
18     ///
19     /// **Known problems:** When accessing C, users might want to store pointer
20     /// sized objects in `extradata` arguments to save an allocation.
21     ///
22     /// **Example:**
23     /// ```ignore
24     /// let ptr: *const T = core::intrinsics::transmute('x')
25     /// ```
26     pub WRONG_TRANSMUTE,
27     correctness,
28     "transmutes that are confusing at best, undefined behaviour at worst and always useless"
29 }
30
31 // FIXME: Move this to `complexity` again, after #5343 is fixed
32 declare_clippy_lint! {
33     /// **What it does:** Checks for transmutes to the original type of the object
34     /// and transmutes that could be a cast.
35     ///
36     /// **Why is this bad?** Readability. The code tricks people into thinking that
37     /// something complex is going on.
38     ///
39     /// **Known problems:** None.
40     ///
41     /// **Example:**
42     /// ```rust,ignore
43     /// core::intrinsics::transmute(t); // where the result type is the same as `t`'s
44     /// ```
45     pub USELESS_TRANSMUTE,
46     nursery,
47     "transmutes that have the same to and from types or could be a cast/coercion"
48 }
49
50 declare_clippy_lint! {
51     /// **What it does:** Checks for transmutes between a type `T` and `*T`.
52     ///
53     /// **Why is this bad?** It's easy to mistakenly transmute between a type and a
54     /// pointer to that type.
55     ///
56     /// **Known problems:** None.
57     ///
58     /// **Example:**
59     /// ```rust,ignore
60     /// core::intrinsics::transmute(t) // where the result type is the same as
61     ///                                // `*t` or `&t`'s
62     /// ```
63     pub CROSSPOINTER_TRANSMUTE,
64     complexity,
65     "transmutes that have to or from types that are a pointer to the other"
66 }
67
68 declare_clippy_lint! {
69     /// **What it does:** Checks for transmutes from a pointer to a reference.
70     ///
71     /// **Why is this bad?** This can always be rewritten with `&` and `*`.
72     ///
73     /// **Known problems:** None.
74     ///
75     /// **Example:**
76     /// ```rust,ignore
77     /// unsafe {
78     ///     let _: &T = std::mem::transmute(p); // where p: *const T
79     /// }
80     ///
81     /// // can be written:
82     /// let _: &T = &*p;
83     /// ```
84     pub TRANSMUTE_PTR_TO_REF,
85     complexity,
86     "transmutes from a pointer to a reference type"
87 }
88
89 declare_clippy_lint! {
90     /// **What it does:** Checks for transmutes from an integer to a `char`.
91     ///
92     /// **Why is this bad?** Not every integer is a Unicode scalar value.
93     ///
94     /// **Known problems:**
95     /// - [`from_u32`] which this lint suggests using is slower than `transmute`
96     /// as it needs to validate the input.
97     /// If you are certain that the input is always a valid Unicode scalar value,
98     /// use [`from_u32_unchecked`] which is as fast as `transmute`
99     /// but has a semantically meaningful name.
100     /// - You might want to handle `None` returned from [`from_u32`] instead of calling `unwrap`.
101     ///
102     /// [`from_u32`]: https://doc.rust-lang.org/std/char/fn.from_u32.html
103     /// [`from_u32_unchecked`]: https://doc.rust-lang.org/std/char/fn.from_u32_unchecked.html
104     ///
105     /// **Example:**
106     /// ```rust
107     /// let x = 1_u32;
108     /// unsafe {
109     ///     let _: char = std::mem::transmute(x); // where x: u32
110     /// }
111     ///
112     /// // should be:
113     /// let _ = std::char::from_u32(x).unwrap();
114     /// ```
115     pub TRANSMUTE_INT_TO_CHAR,
116     complexity,
117     "transmutes from an integer to a `char`"
118 }
119
120 declare_clippy_lint! {
121     /// **What it does:** Checks for transmutes from a `&[u8]` to a `&str`.
122     ///
123     /// **Why is this bad?** Not every byte slice is a valid UTF-8 string.
124     ///
125     /// **Known problems:**
126     /// - [`from_utf8`] which this lint suggests using is slower than `transmute`
127     /// as it needs to validate the input.
128     /// If you are certain that the input is always a valid UTF-8,
129     /// use [`from_utf8_unchecked`] which is as fast as `transmute`
130     /// but has a semantically meaningful name.
131     /// - You might want to handle errors returned from [`from_utf8`] instead of calling `unwrap`.
132     ///
133     /// [`from_utf8`]: https://doc.rust-lang.org/std/str/fn.from_utf8.html
134     /// [`from_utf8_unchecked`]: https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html
135     ///
136     /// **Example:**
137     /// ```rust
138     /// let b: &[u8] = &[1_u8, 2_u8];
139     /// unsafe {
140     ///     let _: &str = std::mem::transmute(b); // where b: &[u8]
141     /// }
142     ///
143     /// // should be:
144     /// let _ = std::str::from_utf8(b).unwrap();
145     /// ```
146     pub TRANSMUTE_BYTES_TO_STR,
147     complexity,
148     "transmutes from a `&[u8]` to a `&str`"
149 }
150
151 declare_clippy_lint! {
152     /// **What it does:** Checks for transmutes from an integer to a `bool`.
153     ///
154     /// **Why is this bad?** This might result in an invalid in-memory representation of a `bool`.
155     ///
156     /// **Known problems:** None.
157     ///
158     /// **Example:**
159     /// ```rust
160     /// let x = 1_u8;
161     /// unsafe {
162     ///     let _: bool = std::mem::transmute(x); // where x: u8
163     /// }
164     ///
165     /// // should be:
166     /// let _: bool = x != 0;
167     /// ```
168     pub TRANSMUTE_INT_TO_BOOL,
169     complexity,
170     "transmutes from an integer to a `bool`"
171 }
172
173 declare_clippy_lint! {
174     /// **What it does:** Checks for transmutes from an integer to a float.
175     ///
176     /// **Why is this bad?** Transmutes are dangerous and error-prone, whereas `from_bits` is intuitive
177     /// and safe.
178     ///
179     /// **Known problems:** None.
180     ///
181     /// **Example:**
182     /// ```rust
183     /// unsafe {
184     ///     let _: f32 = std::mem::transmute(1_u32); // where x: u32
185     /// }
186     ///
187     /// // should be:
188     /// let _: f32 = f32::from_bits(1_u32);
189     /// ```
190     pub TRANSMUTE_INT_TO_FLOAT,
191     complexity,
192     "transmutes from an integer to a float"
193 }
194
195 declare_clippy_lint! {
196     /// **What it does:** Checks for transmutes from a float to an integer.
197     ///
198     /// **Why is this bad?** Transmutes are dangerous and error-prone, whereas `to_bits` is intuitive
199     /// and safe.
200     ///
201     /// **Known problems:** None.
202     ///
203     /// **Example:**
204     /// ```rust
205     /// unsafe {
206     ///     let _: u32 = std::mem::transmute(1f32);
207     /// }
208     ///
209     /// // should be:
210     /// let _: u32 = 1f32.to_bits();
211     /// ```
212     pub TRANSMUTE_FLOAT_TO_INT,
213     complexity,
214     "transmutes from a float to an integer"
215 }
216
217 declare_clippy_lint! {
218     /// **What it does:** Checks for transmutes from a pointer to a pointer, or
219     /// from a reference to a reference.
220     ///
221     /// **Why is this bad?** Transmutes are dangerous, and these can instead be
222     /// written as casts.
223     ///
224     /// **Known problems:** None.
225     ///
226     /// **Example:**
227     /// ```rust
228     /// let ptr = &1u32 as *const u32;
229     /// unsafe {
230     ///     // pointer-to-pointer transmute
231     ///     let _: *const f32 = std::mem::transmute(ptr);
232     ///     // ref-ref transmute
233     ///     let _: &f32 = std::mem::transmute(&1u32);
234     /// }
235     /// // These can be respectively written:
236     /// let _ = ptr as *const f32;
237     /// let _ = unsafe{ &*(&1u32 as *const u32 as *const f32) };
238     /// ```
239     pub TRANSMUTE_PTR_TO_PTR,
240     complexity,
241     "transmutes from a pointer to a pointer / a reference to a reference"
242 }
243
244 declare_clippy_lint! {
245     /// **What it does:** Checks for transmutes between collections whose
246     /// types have different ABI, size or alignment.
247     ///
248     /// **Why is this bad?** This is undefined behavior.
249     ///
250     /// **Known problems:** Currently, we cannot know whether a type is a
251     /// collection, so we just lint the ones that come with `std`.
252     ///
253     /// **Example:**
254     /// ```rust
255     /// // different size, therefore likely out-of-bounds memory access
256     /// // You absolutely do not want this in your code!
257     /// unsafe {
258     ///     std::mem::transmute::<_, Vec<u32>>(vec![2_u16])
259     /// };
260     /// ```
261     ///
262     /// You must always iterate, map and collect the values:
263     ///
264     /// ```rust
265     /// vec![2_u16].into_iter().map(u32::from).collect::<Vec<_>>();
266     /// ```
267     pub UNSOUND_COLLECTION_TRANSMUTE,
268     correctness,
269     "transmute between collections of layout-incompatible types"
270 }
271 declare_lint_pass!(Transmute => [
272     CROSSPOINTER_TRANSMUTE,
273     TRANSMUTE_PTR_TO_REF,
274     TRANSMUTE_PTR_TO_PTR,
275     USELESS_TRANSMUTE,
276     WRONG_TRANSMUTE,
277     TRANSMUTE_INT_TO_CHAR,
278     TRANSMUTE_BYTES_TO_STR,
279     TRANSMUTE_INT_TO_BOOL,
280     TRANSMUTE_INT_TO_FLOAT,
281     TRANSMUTE_FLOAT_TO_INT,
282     UNSOUND_COLLECTION_TRANSMUTE,
283 ]);
284
285 // used to check for UNSOUND_COLLECTION_TRANSMUTE
286 static COLLECTIONS: &[&[&str]] = &[
287     &paths::VEC,
288     &paths::VEC_DEQUE,
289     &paths::BINARY_HEAP,
290     &paths::BTREESET,
291     &paths::BTREEMAP,
292     &paths::HASHSET,
293     &paths::HASHMAP,
294 ];
295 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
296     #[allow(clippy::similar_names, clippy::too_many_lines)]
297     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
298         if_chain! {
299             if let ExprKind::Call(ref path_expr, ref args) = e.kind;
300             if let ExprKind::Path(ref qpath) = path_expr.kind;
301             if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id();
302             if match_def_path(cx, def_id, &paths::TRANSMUTE);
303             then {
304                 let from_ty = cx.tables.expr_ty(&args[0]);
305                 let to_ty = cx.tables.expr_ty(e);
306
307                 match (&from_ty.kind, &to_ty.kind) {
308                     _ if from_ty == to_ty => span_lint(
309                         cx,
310                         USELESS_TRANSMUTE,
311                         e.span,
312                         &format!("transmute from a type (`{}`) to itself", from_ty),
313                     ),
314                     (&ty::Ref(_, rty, rty_mutbl), &ty::RawPtr(ptr_ty)) => span_lint_and_then(
315                         cx,
316                         USELESS_TRANSMUTE,
317                         e.span,
318                         "transmute from a reference to a pointer",
319                         |db| {
320                             if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
321                                 let rty_and_mut = ty::TypeAndMut {
322                                     ty: rty,
323                                     mutbl: rty_mutbl,
324                                 };
325
326                                 let sugg = if ptr_ty == rty_and_mut {
327                                     arg.as_ty(to_ty)
328                                 } else {
329                                     arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
330                                 };
331
332                                 db.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
333                             }
334                         },
335                     ),
336                     (&ty::Int(_), &ty::RawPtr(_)) | (&ty::Uint(_), &ty::RawPtr(_)) => span_lint_and_then(
337                         cx,
338                         USELESS_TRANSMUTE,
339                         e.span,
340                         "transmute from an integer to a pointer",
341                         |db| {
342                             if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
343                                 db.span_suggestion(
344                                     e.span,
345                                     "try",
346                                     arg.as_ty(&to_ty.to_string()).to_string(),
347                                     Applicability::Unspecified,
348                                 );
349                             }
350                         },
351                     ),
352                     (&ty::Float(_), &ty::Ref(..))
353                     | (&ty::Float(_), &ty::RawPtr(_))
354                     | (&ty::Char, &ty::Ref(..))
355                     | (&ty::Char, &ty::RawPtr(_)) => span_lint(
356                         cx,
357                         WRONG_TRANSMUTE,
358                         e.span,
359                         &format!("transmute from a `{}` to a pointer", from_ty),
360                     ),
361                     (&ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
362                         cx,
363                         CROSSPOINTER_TRANSMUTE,
364                         e.span,
365                         &format!(
366                             "transmute from a type (`{}`) to the type that it points to (`{}`)",
367                             from_ty, to_ty
368                         ),
369                     ),
370                     (_, &ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => span_lint(
371                         cx,
372                         CROSSPOINTER_TRANSMUTE,
373                         e.span,
374                         &format!(
375                             "transmute from a type (`{}`) to a pointer to that type (`{}`)",
376                             from_ty, to_ty
377                         ),
378                     ),
379                     (&ty::RawPtr(from_pty), &ty::Ref(_, to_ref_ty, mutbl)) => span_lint_and_then(
380                         cx,
381                         TRANSMUTE_PTR_TO_REF,
382                         e.span,
383                         &format!(
384                             "transmute from a pointer type (`{}`) to a reference type \
385                              (`{}`)",
386                             from_ty, to_ty
387                         ),
388                         |db| {
389                             let arg = sugg::Sugg::hir(cx, &args[0], "..");
390                             let (deref, cast) = if mutbl == Mutability::Mut {
391                                 ("&mut *", "*mut")
392                             } else {
393                                 ("&*", "*const")
394                             };
395
396                             let arg = if from_pty.ty == to_ref_ty {
397                                 arg
398                             } else {
399                                 arg.as_ty(&format!("{} {}", cast, get_type_snippet(cx, qpath, to_ref_ty)))
400                             };
401
402                             db.span_suggestion(
403                                 e.span,
404                                 "try",
405                                 sugg::make_unop(deref, arg).to_string(),
406                                 Applicability::Unspecified,
407                             );
408                         },
409                     ),
410                     (&ty::Int(ast::IntTy::I32), &ty::Char) | (&ty::Uint(ast::UintTy::U32), &ty::Char) => {
411                         span_lint_and_then(
412                             cx,
413                             TRANSMUTE_INT_TO_CHAR,
414                             e.span,
415                             &format!("transmute from a `{}` to a `char`", from_ty),
416                             |db| {
417                                 let arg = sugg::Sugg::hir(cx, &args[0], "..");
418                                 let arg = if let ty::Int(_) = from_ty.kind {
419                                     arg.as_ty(ast::UintTy::U32.name_str())
420                                 } else {
421                                     arg
422                                 };
423                                 db.span_suggestion(
424                                     e.span,
425                                     "consider using",
426                                     format!("std::char::from_u32({}).unwrap()", arg.to_string()),
427                                     Applicability::Unspecified,
428                                 );
429                             },
430                         )
431                     },
432                     (&ty::Ref(_, ty_from, from_mutbl), &ty::Ref(_, ty_to, to_mutbl)) => {
433                         if_chain! {
434                             if let (&ty::Slice(slice_ty), &ty::Str) = (&ty_from.kind, &ty_to.kind);
435                             if let ty::Uint(ast::UintTy::U8) = slice_ty.kind;
436                             if from_mutbl == to_mutbl;
437                             then {
438                                 let postfix = if from_mutbl == Mutability::Mut {
439                                     "_mut"
440                                 } else {
441                                     ""
442                                 };
443
444                                 span_lint_and_then(
445                                     cx,
446                                     TRANSMUTE_BYTES_TO_STR,
447                                     e.span,
448                                     &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
449                                     |db| {
450                                         db.span_suggestion(
451                                             e.span,
452                                             "consider using",
453                                             format!(
454                                                 "std::str::from_utf8{}({}).unwrap()",
455                                                 postfix,
456                                                 snippet(cx, args[0].span, ".."),
457                                             ),
458                                             Applicability::Unspecified,
459                                         );
460                                     }
461                                 )
462                             } else {
463                                 if cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty) {
464                                     span_lint_and_then(
465                                         cx,
466                                         TRANSMUTE_PTR_TO_PTR,
467                                         e.span,
468                                         "transmute from a reference to a reference",
469                                         |db| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
470                                             let ty_from_and_mut = ty::TypeAndMut {
471                                                 ty: ty_from,
472                                                 mutbl: from_mutbl
473                                             };
474                                             let ty_to_and_mut = ty::TypeAndMut { ty: ty_to, mutbl: to_mutbl };
475                                             let sugg_paren = arg
476                                                 .as_ty(cx.tcx.mk_ptr(ty_from_and_mut))
477                                                 .as_ty(cx.tcx.mk_ptr(ty_to_and_mut));
478                                             let sugg = if to_mutbl == Mutability::Mut {
479                                                 sugg_paren.mut_addr_deref()
480                                             } else {
481                                                 sugg_paren.addr_deref()
482                                             };
483                                             db.span_suggestion(
484                                                 e.span,
485                                                 "try",
486                                                 sugg.to_string(),
487                                                 Applicability::Unspecified,
488                                             );
489                                         },
490                                     )
491                                 }
492                             }
493                         }
494                     },
495                     (&ty::RawPtr(_), &ty::RawPtr(to_ty)) => span_lint_and_then(
496                         cx,
497                         TRANSMUTE_PTR_TO_PTR,
498                         e.span,
499                         "transmute from a pointer to a pointer",
500                         |db| {
501                             if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
502                                 let sugg = arg.as_ty(cx.tcx.mk_ptr(to_ty));
503                                 db.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
504                             }
505                         },
506                     ),
507                     (&ty::Int(ast::IntTy::I8), &ty::Bool) | (&ty::Uint(ast::UintTy::U8), &ty::Bool) => {
508                         span_lint_and_then(
509                             cx,
510                             TRANSMUTE_INT_TO_BOOL,
511                             e.span,
512                             &format!("transmute from a `{}` to a `bool`", from_ty),
513                             |db| {
514                                 let arg = sugg::Sugg::hir(cx, &args[0], "..");
515                                 let zero = sugg::Sugg::NonParen(Cow::from("0"));
516                                 db.span_suggestion(
517                                     e.span,
518                                     "consider using",
519                                     sugg::make_binop(ast::BinOpKind::Ne, &arg, &zero).to_string(),
520                                     Applicability::Unspecified,
521                                 );
522                             },
523                         )
524                     },
525                     (&ty::Int(_), &ty::Float(_)) | (&ty::Uint(_), &ty::Float(_)) => span_lint_and_then(
526                         cx,
527                         TRANSMUTE_INT_TO_FLOAT,
528                         e.span,
529                         &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
530                         |db| {
531                             let arg = sugg::Sugg::hir(cx, &args[0], "..");
532                             let arg = if let ty::Int(int_ty) = from_ty.kind {
533                                 arg.as_ty(format!(
534                                     "u{}",
535                                     int_ty.bit_width().map_or_else(|| "size".to_string(), |v| v.to_string())
536                                 ))
537                             } else {
538                                 arg
539                             };
540                             db.span_suggestion(
541                                 e.span,
542                                 "consider using",
543                                 format!("{}::from_bits({})", to_ty, arg.to_string()),
544                                 Applicability::Unspecified,
545                             );
546                         },
547                     ),
548                     (&ty::Float(float_ty), &ty::Int(_)) | (&ty::Float(float_ty), &ty::Uint(_)) => span_lint_and_then(
549                         cx,
550                         TRANSMUTE_FLOAT_TO_INT,
551                         e.span,
552                         &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
553                         |db| {
554                             let mut expr = &args[0];
555                             let mut arg = sugg::Sugg::hir(cx, expr, "..");
556
557                             if let ExprKind::Unary(UnOp::UnNeg, inner_expr) = &expr.kind {
558                                 expr = &inner_expr;
559                             }
560
561                             if_chain! {
562                                 // if the expression is a float literal and it is unsuffixed then
563                                 // add a suffix so the suggestion is valid and unambiguous
564                                 let op = format!("{}{}", arg, float_ty.name_str()).into();
565                                 if let ExprKind::Lit(lit) = &expr.kind;
566                                 if let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node;
567                                 then {
568                                     match arg {
569                                         sugg::Sugg::MaybeParen(_) => arg = sugg::Sugg::MaybeParen(op),
570                                         _ => arg = sugg::Sugg::NonParen(op)
571                                     }
572                                 }
573                             }
574
575                             arg = sugg::Sugg::NonParen(format!("{}.to_bits()", arg.maybe_par()).into());
576
577                             // cast the result of `to_bits` if `to_ty` is signed
578                             arg = if let ty::Int(int_ty) = to_ty.kind {
579                                 arg.as_ty(int_ty.name_str().to_string())
580                             } else {
581                                 arg
582                             };
583
584                             db.span_suggestion(
585                                 e.span,
586                                 "consider using",
587                                 arg.to_string(),
588                                 Applicability::Unspecified,
589                             );
590                         },
591                     ),
592                     (&ty::Adt(ref from_adt, ref from_substs), &ty::Adt(ref to_adt, ref to_substs)) => {
593                         if from_adt.did != to_adt.did ||
594                                 !COLLECTIONS.iter().any(|path| match_def_path(cx, to_adt.did, path)) {
595                             return;
596                         }
597                         if from_substs.types().zip(to_substs.types())
598                                               .any(|(from_ty, to_ty)| is_layout_incompatible(cx, from_ty, to_ty)) {
599                             span_lint(
600                                 cx,
601                                 UNSOUND_COLLECTION_TRANSMUTE,
602                                 e.span,
603                                 &format!(
604                                     "transmute from `{}` to `{}` with mismatched layout is unsound",
605                                     from_ty,
606                                     to_ty
607                                 )
608                             );
609                         }
610                     },
611                     _ => return,
612                 }
613             }
614         }
615     }
616 }
617
618 /// Gets the snippet of `Bar` in `…::transmute<Foo, &Bar>`. If that snippet is
619 /// not available , use
620 /// the type's `ToString` implementation. In weird cases it could lead to types
621 /// with invalid `'_`
622 /// lifetime, but it should be rare.
623 fn get_type_snippet(cx: &LateContext<'_, '_>, path: &QPath<'_>, to_ref_ty: Ty<'_>) -> String {
624     let seg = last_path_segment(path);
625     if_chain! {
626         if let Some(ref params) = seg.args;
627         if !params.parenthesized;
628         if let Some(to_ty) = params.args.iter().filter_map(|arg| match arg {
629             GenericArg::Type(ty) => Some(ty),
630             _ => None,
631         }).nth(1);
632         if let TyKind::Rptr(_, ref to_ty) = to_ty.kind;
633         then {
634             return snippet(cx, to_ty.ty.span, &to_ref_ty.to_string()).to_string();
635         }
636     }
637
638     to_ref_ty.to_string()
639 }
640
641 // check if the component types of the transmuted collection and the result have different ABI,
642 // size or alignment
643 fn is_layout_incompatible<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, from: Ty<'tcx>, to: Ty<'tcx>) -> bool {
644     let empty_param_env = ty::ParamEnv::empty();
645     // check if `from` and `to` are normalizable to avoid ICE (#4968)
646     if !(is_normalizable(cx, empty_param_env, from) && is_normalizable(cx, empty_param_env, to)) {
647         return false;
648     }
649     let from_ty_layout = cx.tcx.layout_of(empty_param_env.and(from));
650     let to_ty_layout = cx.tcx.layout_of(empty_param_env.and(to));
651     if let (Ok(from_layout), Ok(to_layout)) = (from_ty_layout, to_ty_layout) {
652         from_layout.size != to_layout.size || from_layout.align != to_layout.align || from_layout.abi != to_layout.abi
653     } else {
654         // no idea about layout, so don't lint
655         false
656     }
657 }