|
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}; |
2 | 2 | use if_chain::if_chain;
|
3 | 3 | use rustc_ast::Mutability;
|
4 | 4 | use rustc_hir::{Expr, ExprKind, Node};
|
@@ -39,7 +39,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Opti
|
39 | 39 | start_ty.ty, from_size, end_ty.ty, to_size,
|
40 | 40 | ),
|
41 | 41 | |diag| {
|
42 |
| - let ptr_snippet = snippet_opt(cx, left_cast.span).unwrap(); |
| 42 | + let ptr_snippet = source::snippet(cx, left_cast.span, ".."); |
43 | 43 |
|
44 | 44 | let (mutbl_fn_str, mutbl_ptr_str) = match end_ty.mutbl {
|
45 | 45 | Mutability::Mut => ("_mut", "mut"),
|
@@ -119,16 +119,14 @@ fn expr_cast_chain_tys<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Optio
|
119 | 119 | if let ExprKind::Cast(cast_expr, _cast_to_hir_ty) = expr.peel_blocks().kind {
|
120 | 120 | let cast_to = cx.typeck_results().expr_ty(expr);
|
121 | 121 | 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) { |
128 | 127 | Some(CastChainInfo {
|
129 |
| - left_cast, |
130 |
| - start_ty, |
131 | 128 | end_ty: to_slice_ty,
|
| 129 | + ..prev_info |
132 | 130 | })
|
133 | 131 | } else {
|
134 | 132 | let cast_from = cx.typeck_results().expr_ty(cast_expr);
|
|
0 commit comments