Skip to content

Commit f6c7786

Browse files
clean-up ptr_cast_constness (#15478)
changelog: none
2 parents 64d5553 + 0e5b1b1 commit f6c7786

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

clippy_lints/src/casts/ptr_cast_constness.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::msrvs::{self, Msrv};
3+
use clippy_utils::source::snippet_with_applicability;
34
use clippy_utils::sugg::Sugg;
45
use clippy_utils::{std_or_core, sym};
56
use rustc_errors::Applicability;
6-
use rustc_hir::{Expr, ExprKind, Mutability, QPath};
7+
use rustc_hir::{Expr, ExprKind, QPath};
78
use rustc_lint::LateContext;
89
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
910

@@ -12,26 +13,23 @@ use super::PTR_CAST_CONSTNESS;
1213
pub(super) fn check<'tcx>(
1314
cx: &LateContext<'_>,
1415
expr: &Expr<'_>,
15-
cast_expr: &Expr<'_>,
16+
cast_from_expr: &Expr<'_>,
1617
cast_from: Ty<'tcx>,
1718
cast_to: Ty<'tcx>,
1819
msrv: Msrv,
1920
) {
2021
if let ty::RawPtr(from_ty, from_mutbl) = cast_from.kind()
2122
&& let ty::RawPtr(to_ty, to_mutbl) = cast_to.kind()
22-
&& matches!(
23-
(from_mutbl, to_mutbl),
24-
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not)
25-
)
23+
&& from_mutbl != to_mutbl
2624
&& from_ty == to_ty
2725
&& !from_ty.has_erased_regions()
2826
{
29-
if let ExprKind::Call(func, []) = cast_expr.kind
27+
if let ExprKind::Call(func, []) = cast_from_expr.kind
3028
&& let ExprKind::Path(QPath::Resolved(None, path)) = func.kind
3129
&& let Some(defid) = path.res.opt_def_id()
3230
&& let Some(prefix) = std_or_core(cx)
3331
&& let mut app = Applicability::MachineApplicable
34-
&& let sugg = format!("{}", Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app))
32+
&& let sugg = snippet_with_applicability(cx, cast_from_expr.span, "_", &mut app)
3533
&& let Some((_, after_lt)) = sugg.split_once("::<")
3634
&& let Some((source, target, target_func)) = match cx.tcx.get_diagnostic_name(defid) {
3735
Some(sym::ptr_null) => Some(("const", "mutable", "null_mut")),
@@ -53,11 +51,8 @@ pub(super) fn check<'tcx>(
5351

5452
if msrv.meets(cx, msrvs::POINTER_CAST_CONSTNESS) {
5553
let mut app = Applicability::MachineApplicable;
56-
let sugg = Sugg::hir_with_context(cx, cast_expr, expr.span.ctxt(), "_", &mut app);
57-
let constness = match *to_mutbl {
58-
Mutability::Not => "const",
59-
Mutability::Mut => "mut",
60-
};
54+
let sugg = Sugg::hir_with_context(cx, cast_from_expr, expr.span.ctxt(), "_", &mut app);
55+
let constness = to_mutbl.ptr_str();
6156

6257
span_lint_and_sugg(
6358
cx,
@@ -73,8 +68,8 @@ pub(super) fn check<'tcx>(
7368
}
7469

7570
pub(super) fn check_null_ptr_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>) {
76-
if let ExprKind::MethodCall(method, cast_expr, [], _) = expr.kind
77-
&& let ExprKind::Call(func, []) = cast_expr.kind
71+
if let ExprKind::MethodCall(method, cast_from_expr, [], _) = expr.kind
72+
&& let ExprKind::Call(func, []) = cast_from_expr.kind
7873
&& let ExprKind::Path(QPath::Resolved(None, path)) = func.kind
7974
&& let Some(defid) = path.res.opt_def_id()
8075
&& let method = match (cx.tcx.get_diagnostic_name(defid), method.ident.name) {
@@ -84,7 +79,7 @@ pub(super) fn check_null_ptr_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>)
8479
}
8580
&& let Some(prefix) = std_or_core(cx)
8681
&& let mut app = Applicability::MachineApplicable
87-
&& let sugg = format!("{}", Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app))
82+
&& let sugg = snippet_with_applicability(cx, cast_from_expr.span, "_", &mut app)
8883
&& let Some((_, after_lt)) = sugg.split_once("::<")
8984
{
9085
span_lint_and_sugg(

0 commit comments

Comments
 (0)