]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/casts/ptr_as_ptr.rs
Auto merge of #6916 - camsteffen:diagnostics-util, r=Manishearth
[rust.git] / clippy_lints / src / casts / ptr_as_ptr.rs
1 use std::borrow::Cow;
2
3 use rustc_errors::Applicability;
4 use rustc_hir::{Expr, ExprKind, Mutability, TyKind};
5 use rustc_lint::LateContext;
6 use rustc_middle::ty::{self, TypeAndMut};
7 use rustc_semver::RustcVersion;
8
9 use if_chain::if_chain;
10
11 use crate::utils::meets_msrv;
12 use crate::utils::sugg::Sugg;
13 use clippy_utils::diagnostics::span_lint_and_sugg;
14
15 use super::PTR_AS_PTR;
16
17 const PTR_AS_PTR_MSRV: RustcVersion = RustcVersion::new(1, 38, 0);
18
19 pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: &Option<RustcVersion>) {
20     if !meets_msrv(msrv.as_ref(), &PTR_AS_PTR_MSRV) {
21         return;
22     }
23
24     if_chain! {
25         if let ExprKind::Cast(cast_expr, cast_to_hir_ty) = expr.kind;
26         let (cast_from, cast_to) = (cx.typeck_results().expr_ty(cast_expr), cx.typeck_results().expr_ty(expr));
27         if let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, .. }) = cast_from.kind();
28         if let ty::RawPtr(TypeAndMut { ty: to_pointee_ty, mutbl: to_mutbl }) = cast_to.kind();
29         if matches!((from_mutbl, to_mutbl),
30             (Mutability::Not, Mutability::Not) | (Mutability::Mut, Mutability::Mut));
31         // The `U` in `pointer::cast` have to be `Sized`
32         // as explained here: https://github.com/rust-lang/rust/issues/60602.
33         if to_pointee_ty.is_sized(cx.tcx.at(expr.span), cx.param_env);
34         then {
35             let mut applicability = Applicability::MachineApplicable;
36             let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut applicability);
37             let turbofish = match &cast_to_hir_ty.kind {
38                     TyKind::Infer => Cow::Borrowed(""),
39                     TyKind::Ptr(mut_ty) if matches!(mut_ty.ty.kind, TyKind::Infer) => Cow::Borrowed(""),
40                     _ => Cow::Owned(format!("::<{}>", to_pointee_ty)),
41                 };
42             span_lint_and_sugg(
43                 cx,
44                 PTR_AS_PTR,
45                 expr.span,
46                 "`as` casting between raw pointers without changing its mutability",
47                 "try `pointer::cast`, a safer alternative",
48                 format!("{}.cast{}()", cast_expr_sugg.maybe_par(), turbofish),
49                 applicability,
50             );
51         }
52     }
53 }