Skip to content

Commit 22c47f0

Browse files
ptr_as_ptr: move to if (#15480)
changelog: none
2 parents 75d01b7 + d4a6061 commit 22c47f0

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

clippy_lints/src/casts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
875875
cast_slice_from_raw_parts::check(cx, expr, cast_from_expr, cast_to, self.msrv);
876876
cast_ptr_alignment::check(cx, expr, cast_from, cast_to);
877877
ptr_cast_constness::check(cx, expr, cast_from_expr, cast_from, cast_to, self.msrv);
878+
ptr_as_ptr::check(cx, expr, cast_from_expr, cast_from, cast_to_hir, cast_to, self.msrv);
878879
as_ptr_cast_mut::check(cx, expr, cast_from_expr, cast_to);
879880
fn_to_numeric_cast_any::check(cx, expr, cast_from_expr, cast_from, cast_to);
880881
confusing_method_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
@@ -916,7 +917,6 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
916917
cast_slice_from_raw_parts::check_implicit_cast(cx, expr);
917918
}
918919
cast_ptr_alignment::check_cast_method(cx, expr);
919-
ptr_as_ptr::check(cx, expr, self.msrv);
920920
cast_slice_different_sizes::check(cx, expr, self.msrv);
921921
ptr_cast_constness::check_null_ptr_cast_method(cx, expr);
922922
}

clippy_lints/src/casts/ptr_as_ptr.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::sugg::Sugg;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Expr, ExprKind, Mutability, QPath, TyKind};
7+
use rustc_hir::{self as hir, Expr, ExprKind, QPath, TyKind};
88
use rustc_lint::LateContext;
9-
use rustc_middle::ty;
9+
use rustc_middle::ty::{self, Ty};
1010
use rustc_span::{Span, sym};
1111

1212
use super::PTR_AS_PTR;
@@ -26,21 +26,26 @@ impl OmitFollowedCastReason<'_> {
2626
}
2727
}
2828

29-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv) {
30-
if let ExprKind::Cast(cast_expr, cast_to_hir_ty) = expr.kind
31-
&& let (cast_from, cast_to) = (cx.typeck_results().expr_ty(cast_expr), cx.typeck_results().expr_ty(expr))
32-
&& let ty::RawPtr(_, from_mutbl) = cast_from.kind()
29+
pub(super) fn check<'tcx>(
30+
cx: &LateContext<'tcx>,
31+
expr: &Expr<'tcx>,
32+
cast_from_expr: &Expr<'_>,
33+
cast_from: Ty<'_>,
34+
cast_to_hir: &hir::Ty<'_>,
35+
cast_to: Ty<'tcx>,
36+
msrv: Msrv,
37+
) {
38+
if let ty::RawPtr(_, from_mutbl) = cast_from.kind()
3339
&& let ty::RawPtr(to_pointee_ty, to_mutbl) = cast_to.kind()
34-
&& matches!((from_mutbl, to_mutbl),
35-
(Mutability::Not, Mutability::Not) | (Mutability::Mut, Mutability::Mut))
40+
&& from_mutbl == to_mutbl
3641
// The `U` in `pointer::cast` have to be `Sized`
3742
// as explained here: https://github.com/rust-lang/rust/issues/60602.
3843
&& to_pointee_ty.is_sized(cx.tcx, cx.typing_env())
3944
&& msrv.meets(cx, msrvs::POINTER_CAST)
4045
&& !is_from_proc_macro(cx, expr)
4146
{
4247
let mut app = Applicability::MachineApplicable;
43-
let turbofish = match &cast_to_hir_ty.kind {
48+
let turbofish = match &cast_to_hir.kind {
4449
TyKind::Infer(()) => String::new(),
4550
TyKind::Ptr(mut_ty) => {
4651
if matches!(mut_ty.ty.kind, TyKind::Infer(())) {
@@ -58,7 +63,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
5863
// following `cast` does not compile because it fails to infer what type is expected
5964
// as type argument to `std::ptr::ptr_null` or `std::ptr::ptr_null_mut`, so
6065
// we omit following `cast`:
61-
let omit_cast = if let ExprKind::Call(func, []) = cast_expr.kind
66+
let omit_cast = if let ExprKind::Call(func, []) = cast_from_expr.kind
6267
&& let ExprKind::Path(ref qpath @ QPath::Resolved(None, path)) = func.kind
6368
&& let Some(method_defid) = path.res.opt_def_id()
6469
{
@@ -76,7 +81,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
7681
let method = snippet_with_applicability(cx, qpath_span_without_turbofish(method), "..", &mut app);
7782
("try call directly", format!("{method}{turbofish}()"))
7883
} else {
79-
let cast_expr_sugg = Sugg::hir_with_context(cx, cast_expr, expr.span.ctxt(), "_", &mut app);
84+
let cast_expr_sugg = Sugg::hir_with_context(cx, cast_from_expr, expr.span.ctxt(), "_", &mut app);
8085

8186
(
8287
"try `pointer::cast`, a safer alternative",

0 commit comments

Comments
 (0)