]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | use super::utils::can_be_expressed_as_pointer_cast; |
2 | use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS; | |
3 | use crate::utils::{span_lint_and_then, sugg}; | |
4 | use rustc_errors::Applicability; | |
5 | use rustc_hir::Expr; | |
6 | use rustc_lint::LateContext; | |
7 | use rustc_middle::ty::Ty; | |
8 | ||
9 | /// Checks for `transmutes_expressible_as_ptr_casts` lint. | |
10 | /// Returns `true` if it's triggered, otherwise returns `false`. | |
11 | pub(super) fn check<'tcx>( | |
12 | cx: &LateContext<'tcx>, | |
13 | e: &'tcx Expr<'_>, | |
14 | from_ty: Ty<'tcx>, | |
15 | to_ty: Ty<'tcx>, | |
16 | args: &'tcx [Expr<'_>], | |
17 | ) -> bool { | |
18 | if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) { | |
19 | span_lint_and_then( | |
20 | cx, | |
21 | TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, | |
22 | e.span, | |
23 | &format!( | |
24 | "transmute from `{}` to `{}` which could be expressed as a pointer cast instead", | |
25 | from_ty, to_ty | |
26 | ), | |
27 | |diag| { | |
28 | if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) { | |
29 | let sugg = arg.as_ty(&to_ty.to_string()).to_string(); | |
30 | diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable); | |
31 | } | |
32 | }, | |
33 | ); | |
34 | true | |
35 | } else { | |
36 | false | |
37 | } | |
38 | } |