Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions clippy_lints/src/casts/ptr_cast_constness.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::{std_or_core, sym};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Mutability, QPath};
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};

Expand All @@ -12,26 +13,23 @@ use super::PTR_CAST_CONSTNESS;
pub(super) fn check<'tcx>(
cx: &LateContext<'_>,
expr: &Expr<'_>,
cast_expr: &Expr<'_>,
cast_from_expr: &Expr<'_>,
cast_from: Ty<'tcx>,
cast_to: Ty<'tcx>,
msrv: Msrv,
) {
if let ty::RawPtr(from_ty, from_mutbl) = cast_from.kind()
&& let ty::RawPtr(to_ty, to_mutbl) = cast_to.kind()
&& matches!(
(from_mutbl, to_mutbl),
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not)
)
&& from_mutbl != to_mutbl
&& from_ty == to_ty
&& !from_ty.has_erased_regions()
{
if let ExprKind::Call(func, []) = cast_expr.kind
if let ExprKind::Call(func, []) = cast_from_expr.kind
&& let ExprKind::Path(QPath::Resolved(None, path)) = func.kind
&& let Some(defid) = path.res.opt_def_id()
&& let Some(prefix) = std_or_core(cx)
&& let mut app = Applicability::MachineApplicable
&& let sugg = format!("{}", Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app))
&& let sugg = snippet_with_applicability(cx, cast_from_expr.span, "_", &mut app)
&& let Some((_, after_lt)) = sugg.split_once("::<")
&& let Some((source, target, target_func)) = match cx.tcx.get_diagnostic_name(defid) {
Some(sym::ptr_null) => Some(("const", "mutable", "null_mut")),
Expand All @@ -53,11 +51,8 @@ pub(super) fn check<'tcx>(

if msrv.meets(cx, msrvs::POINTER_CAST_CONSTNESS) {
let mut app = Applicability::MachineApplicable;
let sugg = Sugg::hir_with_context(cx, cast_expr, expr.span.ctxt(), "_", &mut app);
let constness = match *to_mutbl {
Mutability::Not => "const",
Mutability::Mut => "mut",
};
let sugg = Sugg::hir_with_context(cx, cast_from_expr, expr.span.ctxt(), "_", &mut app);
let constness = to_mutbl.ptr_str();

span_lint_and_sugg(
cx,
Expand All @@ -73,8 +68,8 @@ pub(super) fn check<'tcx>(
}

pub(super) fn check_null_ptr_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>) {
if let ExprKind::MethodCall(method, cast_expr, [], _) = expr.kind
&& let ExprKind::Call(func, []) = cast_expr.kind
if let ExprKind::MethodCall(method, cast_from_expr, [], _) = expr.kind
&& let ExprKind::Call(func, []) = cast_from_expr.kind
&& let ExprKind::Path(QPath::Resolved(None, path)) = func.kind
&& let Some(defid) = path.res.opt_def_id()
&& let method = match (cx.tcx.get_diagnostic_name(defid), method.ident.name) {
Expand All @@ -84,7 +79,7 @@ pub(super) fn check_null_ptr_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>)
}
&& let Some(prefix) = std_or_core(cx)
&& let mut app = Applicability::MachineApplicable
&& let sugg = format!("{}", Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app))
&& let sugg = snippet_with_applicability(cx, cast_from_expr.span, "_", &mut app)
&& let Some((_, after_lt)) = sugg.split_once("::<")
{
span_lint_and_sugg(
Expand Down