]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs
Update upstream source from tag 'upstream/1.52.1+dfsg1'
[rustc.git] / src / tools / clippy / clippy_lints / src / transmute / transmute_ptr_to_ptr.rs
CommitLineData
f20569fa
XL
1use super::TRANSMUTE_PTR_TO_PTR;
2use crate::utils::{span_lint_and_then, sugg};
3use rustc_errors::Applicability;
4use rustc_hir::Expr;
5use rustc_lint::LateContext;
6use rustc_middle::ty::{self, Ty};
7
8/// Checks for `transmute_ptr_to_ptr` lint.
9/// Returns `true` if it's triggered, otherwise returns `false`.
10pub(super) fn check<'tcx>(
11 cx: &LateContext<'tcx>,
12 e: &'tcx Expr<'_>,
13 from_ty: Ty<'tcx>,
14 to_ty: Ty<'tcx>,
15 args: &'tcx [Expr<'_>],
16) -> bool {
17 match (&from_ty.kind(), &to_ty.kind()) {
18 (ty::RawPtr(_), ty::RawPtr(to_ty)) => {
19 span_lint_and_then(
20 cx,
21 TRANSMUTE_PTR_TO_PTR,
22 e.span,
23 "transmute from a pointer to a pointer",
24 |diag| {
25 if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
26 let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
27 diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
28 }
29 },
30 );
31 true
32 },
33 _ => false,
34 }
35}