]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs
Auto merge of #104673 - matthiaskrgr:rollup-85f65ov, r=matthiaskrgr
[rust.git] / src / tools / clippy / clippy_lints / src / transmute / transmutes_expressible_as_ptr_casts.rs
1 use super::utils::can_be_expressed_as_pointer_cast;
2 use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS;
3 use clippy_utils::diagnostics::span_lint_and_then;
4 use clippy_utils::sugg;
5 use rustc_errors::Applicability;
6 use rustc_hir::Expr;
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::Ty;
9
10 /// Checks for `transmutes_expressible_as_ptr_casts` lint.
11 /// Returns `true` if it's triggered, otherwise returns `false`.
12 pub(super) fn check<'tcx>(
13     cx: &LateContext<'tcx>,
14     e: &'tcx Expr<'_>,
15     from_ty: Ty<'tcx>,
16     to_ty: Ty<'tcx>,
17     arg: &'tcx Expr<'_>,
18 ) -> bool {
19     if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) {
20         span_lint_and_then(
21             cx,
22             TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
23             e.span,
24             &format!("transmute from `{from_ty}` to `{to_ty}` which could be expressed as a pointer cast instead"),
25             |diag| {
26                 if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
27                     let sugg = arg.as_ty(&to_ty.to_string()).to_string();
28                     diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
29                 }
30             },
31         );
32         true
33     } else {
34         false
35     }
36 }