Skip to content

Commit 8f8fc9f

Browse files
committed
use non-panicking snippet, use struct update syntax and add comment
1 parent cf99f50 commit 8f8fc9f

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

clippy_lints/src/casts/cast_slice_different_sizes.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{diagnostics::span_lint_and_then, meets_msrv, msrvs, source::snippet_opt};
1+
use clippy_utils::{diagnostics::span_lint_and_then, meets_msrv, msrvs, source};
22
use if_chain::if_chain;
33
use rustc_ast::Mutability;
44
use rustc_hir::{Expr, ExprKind, Node};
@@ -39,7 +39,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Opti
3939
start_ty.ty, from_size, end_ty.ty, to_size,
4040
),
4141
|diag| {
42-
let ptr_snippet = snippet_opt(cx, left_cast.span).unwrap();
42+
let ptr_snippet = source::snippet(cx, left_cast.span, "..");
4343

4444
let (mutbl_fn_str, mutbl_ptr_str) = match end_ty.mutbl {
4545
Mutability::Mut => ("_mut", "mut"),
@@ -119,16 +119,14 @@ fn expr_cast_chain_tys<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Optio
119119
if let ExprKind::Cast(cast_expr, _cast_to_hir_ty) = expr.peel_blocks().kind {
120120
let cast_to = cx.typeck_results().expr_ty(expr);
121121
let to_slice_ty = get_raw_slice_ty_mut(cast_to)?;
122-
if let Some(CastChainInfo {
123-
left_cast,
124-
start_ty,
125-
end_ty: _,
126-
}) = expr_cast_chain_tys(cx, cast_expr)
127-
{
122+
123+
// If the expression that makes up the source of this cast is itself a cast, recursively
124+
// call `expr_cast_chain_tys` and update the end type with the final tartet type.
125+
// Otherwise, this cast is not immediately nested, just construct the info for this cast
126+
if let Some(prev_info) = expr_cast_chain_tys(cx, cast_expr) {
128127
Some(CastChainInfo {
129-
left_cast,
130-
start_ty,
131128
end_ty: to_slice_ty,
129+
..prev_info
132130
})
133131
} else {
134132
let cast_from = cx.typeck_results().expr_ty(cast_expr);

0 commit comments

Comments
 (0)