]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / tools / clippy / clippy_lints / src / transmute / transmutes_expressible_as_ptr_casts.rs
CommitLineData
f20569fa
XL
1use super::utils::can_be_expressed_as_pointer_cast;
2use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS;
3use crate::utils::{span_lint_and_then, sugg};
4use rustc_errors::Applicability;
5use rustc_hir::Expr;
6use rustc_lint::LateContext;
7use rustc_middle::ty::Ty;
8
9/// Checks for `transmutes_expressible_as_ptr_casts` lint.
10/// Returns `true` if it's triggered, otherwise returns `false`.
11pub(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}