]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs
Auto merge of #7794 - ThibsG:FieldReassignDefault6312, r=llogiq
[rust.git] / 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     args: &'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!(
25                 "transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
26                 from_ty, to_ty
27             ),
28             |diag| {
29                 if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
30                     let sugg = arg.as_ty(&to_ty.to_string()).to_string();
31                     diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
32                 }
33             },
34         );
35         true
36     } else {
37         false
38     }
39 }