|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{GenericArg, HirId, Local, Node, Path, TyKind}; |
| 4 | +use rustc_lint::LateContext; |
| 5 | +use rustc_middle::lint::in_external_macro; |
| 6 | +use rustc_middle::ty::Ty; |
| 7 | + |
| 8 | +use crate::transmute::MISSING_TRANSMUTE_ANNOTATIONS; |
| 9 | + |
| 10 | +fn get_parent_local_binding_ty<'tcx>(cx: &LateContext<'tcx>, expr_hir_id: HirId) -> Option<Local<'tcx>> { |
| 11 | + let mut parent_iter = cx.tcx.hir().parent_iter(expr_hir_id); |
| 12 | + if let Some((_, node)) = parent_iter.next() { |
| 13 | + match node { |
| 14 | + Node::Local(local) => Some(*local), |
| 15 | + Node::Block(_) => { |
| 16 | + if let Some((parent_hir_id, Node::Expr(expr))) = parent_iter.next() |
| 17 | + && matches!(expr.kind, rustc_hir::ExprKind::Block(_, _)) |
| 18 | + { |
| 19 | + get_parent_local_binding_ty(cx, parent_hir_id) |
| 20 | + } else { |
| 21 | + None |
| 22 | + } |
| 23 | + }, |
| 24 | + _ => None, |
| 25 | + } |
| 26 | + } else { |
| 27 | + None |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +fn is_function_block(cx: &LateContext<'_>, expr_hir_id: HirId) -> bool { |
| 32 | + let def_id = cx.tcx.hir().enclosing_body_owner(expr_hir_id); |
| 33 | + if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(def_id) { |
| 34 | + let body = cx.tcx.hir().body(body_id); |
| 35 | + return body.value.peel_blocks().hir_id == expr_hir_id; |
| 36 | + } |
| 37 | + false |
| 38 | +} |
| 39 | + |
| 40 | +pub(super) fn check<'tcx>( |
| 41 | + cx: &LateContext<'tcx>, |
| 42 | + path: &Path<'tcx>, |
| 43 | + from_ty: Ty<'tcx>, |
| 44 | + to_ty: Ty<'tcx>, |
| 45 | + expr_hir_id: HirId, |
| 46 | +) -> bool { |
| 47 | + let last = path.segments.last().unwrap(); |
| 48 | + if in_external_macro(cx.tcx.sess, last.ident.span) { |
| 49 | + // If it comes from a non-local macro, we ignore it. |
| 50 | + return false; |
| 51 | + } |
| 52 | + let args = last.args; |
| 53 | + let missing_generic = match args { |
| 54 | + Some(args) if !args.args.is_empty() => args.args.iter().any(|arg| match arg { |
| 55 | + GenericArg::Infer(_) => true, |
| 56 | + GenericArg::Type(ty) => matches!(ty.kind, TyKind::Infer), |
| 57 | + _ => false, |
| 58 | + }), |
| 59 | + _ => true, |
| 60 | + }; |
| 61 | + if !missing_generic { |
| 62 | + return false; |
| 63 | + } |
| 64 | + // If it's being set as a local variable value... |
| 65 | + if let Some(local) = get_parent_local_binding_ty(cx, expr_hir_id) { |
| 66 | + // ... which does have type annotations. |
| 67 | + if let Some(ty) = local.ty |
| 68 | + // If this is a `let x: _ =`, we should lint. |
| 69 | + && !matches!(ty.kind, TyKind::Infer) |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + // We check if this transmute is not the only element in the function |
| 74 | + } else if is_function_block(cx, expr_hir_id) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + span_lint_and_sugg( |
| 78 | + cx, |
| 79 | + MISSING_TRANSMUTE_ANNOTATIONS, |
| 80 | + last.ident.span.with_hi(path.span.hi()), |
| 81 | + "transmute used without annotations", |
| 82 | + "consider adding missing annotations", |
| 83 | + format!("{}::<{from_ty}, {to_ty}>", last.ident.as_str()), |
| 84 | + Applicability::MaybeIncorrect, |
| 85 | + ); |
| 86 | + true |
| 87 | +} |
0 commit comments