Skip to content

Commit 75d01b7

Browse files
authored
split cast_ptr_alignment (#15479)
This lint consists of two distinct cases: one for the `as` cast, and one for `.cast()`. Splitting them in two separate functions allowed moving the `as` one into the `if let ExprKind::Cast` branch of `casts/mod.rs`, and reusing the variables created there changelog: none
2 parents 9373413 + 74be6f7 commit 75d01b7

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@ use rustc_middle::ty::{self, Ty};
88

99
use super::CAST_PTR_ALIGNMENT;
1010

11-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
12-
if let ExprKind::Cast(cast_expr, cast_to) = expr.kind {
13-
if is_hir_ty_cfg_dependant(cx, cast_to) {
14-
return;
15-
}
16-
let (cast_from, cast_to) = (
17-
cx.typeck_results().expr_ty(cast_expr),
18-
cx.typeck_results().expr_ty(expr),
19-
);
20-
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
21-
} else if let ExprKind::MethodCall(method_path, self_arg, [], _) = &expr.kind
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, cast_from: Ty<'tcx>, cast_to: Ty<'tcx>) {
12+
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
13+
}
14+
15+
pub(super) fn check_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>) {
16+
if let ExprKind::MethodCall(method_path, self_arg, [], _) = &expr.kind
2217
&& method_path.ident.name == sym::cast
2318
&& let Some(generic_args) = method_path.args
2419
&& let [GenericArg::Type(cast_to)] = generic_args.args
@@ -74,14 +69,13 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
7469
ExprKind::Call(func, [arg, ..]) if arg.hir_id == e.hir_id => {
7570
if let ExprKind::Path(path) = &func.kind
7671
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
72+
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id)
7773
&& matches!(
78-
cx.tcx.get_diagnostic_name(def_id),
79-
Some(
80-
sym::ptr_write_unaligned
81-
| sym::ptr_read_unaligned
82-
| sym::intrinsics_unaligned_volatile_load
83-
| sym::intrinsics_unaligned_volatile_store
84-
)
74+
name,
75+
sym::ptr_write_unaligned
76+
| sym::ptr_read_unaligned
77+
| sym::intrinsics_unaligned_volatile_load
78+
| sym::intrinsics_unaligned_volatile_store
8579
)
8680
{
8781
true

clippy_lints/src/casts/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
873873
}
874874
char_lit_as_u8::check(cx, expr, cast_from_expr, cast_to);
875875
cast_slice_from_raw_parts::check(cx, expr, cast_from_expr, cast_to, self.msrv);
876+
cast_ptr_alignment::check(cx, expr, cast_from, cast_to);
876877
ptr_cast_constness::check(cx, expr, cast_from_expr, cast_from, cast_to, self.msrv);
877878
as_ptr_cast_mut::check(cx, expr, cast_from_expr, cast_to);
878879
fn_to_numeric_cast_any::check(cx, expr, cast_from_expr, cast_from, cast_to);
@@ -914,7 +915,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
914915
if self.msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS) {
915916
cast_slice_from_raw_parts::check_implicit_cast(cx, expr);
916917
}
917-
cast_ptr_alignment::check(cx, expr);
918+
cast_ptr_alignment::check_cast_method(cx, expr);
918919
ptr_as_ptr::check(cx, expr, self.msrv);
919920
cast_slice_different_sizes::check(cx, expr, self.msrv);
920921
ptr_cast_constness::check_null_ptr_cast_method(cx, expr);

0 commit comments

Comments
 (0)