|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::get_parent_expr; |
| 3 | +use clippy_utils::ty::implements_trait; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::{Expr, ExprKind}; |
| 6 | +use rustc_lint::LateContext; |
| 7 | +use rustc_middle::ty; |
| 8 | +use rustc_span::{sym, Span}; |
| 9 | + |
| 10 | +use super::UNNECESSARY_FALLIBLE_CONVERSIONS; |
| 11 | + |
| 12 | +/// What function is being called and whether that call is written as a method call or a function |
| 13 | +/// call |
| 14 | +#[derive(Copy, Clone)] |
| 15 | +#[expect(clippy::enum_variant_names)] |
| 16 | +enum FunctionKind { |
| 17 | + /// `T::try_from(U)` |
| 18 | + TryFromFunction, |
| 19 | + /// `t.try_into()` |
| 20 | + TryIntoMethod, |
| 21 | + /// `U::try_into(t)` |
| 22 | + TryIntoFunction, |
| 23 | +} |
| 24 | + |
| 25 | +fn check<'tcx>( |
| 26 | + cx: &LateContext<'tcx>, |
| 27 | + expr: &Expr<'_>, |
| 28 | + node_args: ty::GenericArgsRef<'tcx>, |
| 29 | + kind: FunctionKind, |
| 30 | + primary_span: Span, |
| 31 | +) { |
| 32 | + if let &[self_ty, other_ty] = node_args.as_slice() |
| 33 | + // useless_conversion already warns `T::try_from(T)`, so ignore it here |
| 34 | + && self_ty != other_ty |
| 35 | + && let Some(self_ty) = self_ty.as_type() |
| 36 | + && let Some(from_into_trait) = cx.tcx.get_diagnostic_item(match kind { |
| 37 | + FunctionKind::TryFromFunction => sym::From, |
| 38 | + FunctionKind::TryIntoMethod | FunctionKind::TryIntoFunction => sym::Into, |
| 39 | + }) |
| 40 | + // If `T: TryFrom<U>` and `T: From<U>` both exist, then that means that the `TryFrom` |
| 41 | + // _must_ be from the blanket impl and cannot have been manually implemented |
| 42 | + // (else there would be conflicting impls, even with #![feature(spec)]), so we don't even need to check |
| 43 | + // what `<T as TryFrom<U>>::Error` is: it's always `Infallible` |
| 44 | + && implements_trait(cx, self_ty, from_into_trait, &[other_ty]) |
| 45 | + { |
| 46 | + let parent_unwrap_call = get_parent_expr(cx, expr) |
| 47 | + .and_then(|parent| { |
| 48 | + if let ExprKind::MethodCall(path, .., span) = parent.kind |
| 49 | + && let sym::unwrap | sym::expect = path.ident.name |
| 50 | + { |
| 51 | + Some(span) |
| 52 | + } else { |
| 53 | + None |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + let (sugg, span, applicability) = match kind { |
| 58 | + FunctionKind::TryIntoMethod if let Some(unwrap_span) = parent_unwrap_call => { |
| 59 | + // Extend the span to include the unwrap/expect call: |
| 60 | + // `foo.try_into().expect("..")` |
| 61 | + // ^^^^^^^^^^^^^^^^^^^^^^^ |
| 62 | + // |
| 63 | + // `try_into().unwrap()` specifically can be trivially replaced with just `into()`, |
| 64 | + // so that can be machine-applicable |
| 65 | + |
| 66 | + ("into()", primary_span.with_hi(unwrap_span.hi()), Applicability::MachineApplicable) |
| 67 | + } |
| 68 | + FunctionKind::TryFromFunction => ("From::from", primary_span, Applicability::Unspecified), |
| 69 | + FunctionKind::TryIntoFunction => ("Into::into", primary_span, Applicability::Unspecified), |
| 70 | + FunctionKind::TryIntoMethod => ("into", primary_span, Applicability::Unspecified), |
| 71 | + }; |
| 72 | + |
| 73 | + span_lint_and_sugg( |
| 74 | + cx, |
| 75 | + UNNECESSARY_FALLIBLE_CONVERSIONS, |
| 76 | + span, |
| 77 | + "use of a fallible conversion when an infallible one could be used", |
| 78 | + "use", |
| 79 | + sugg.into(), |
| 80 | + applicability |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Checks method call exprs: |
| 86 | +/// - `0i32.try_into()` |
| 87 | +pub(super) fn check_method(cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 88 | + if let ExprKind::MethodCall(path, ..) = expr.kind { |
| 89 | + check( |
| 90 | + cx, |
| 91 | + expr, |
| 92 | + cx.typeck_results().node_args(expr.hir_id), |
| 93 | + FunctionKind::TryIntoMethod, |
| 94 | + path.ident.span, |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// Checks function call exprs: |
| 100 | +/// - `<i64 as TryFrom<_>>::try_from(0i32)` |
| 101 | +/// - `<_ as TryInto<i64>>::try_into(0i32)` |
| 102 | +pub(super) fn check_function(cx: &LateContext<'_>, expr: &Expr<'_>, callee: &Expr<'_>) { |
| 103 | + if let ExprKind::Path(ref qpath) = callee.kind |
| 104 | + && let Some(item_def_id) = cx.qpath_res(qpath, callee.hir_id).opt_def_id() |
| 105 | + && let Some(trait_def_id) = cx.tcx.trait_of_item(item_def_id) |
| 106 | + { |
| 107 | + check( |
| 108 | + cx, |
| 109 | + expr, |
| 110 | + cx.typeck_results().node_args(callee.hir_id), |
| 111 | + match cx.tcx.get_diagnostic_name(trait_def_id) { |
| 112 | + Some(sym::TryFrom) => FunctionKind::TryFromFunction, |
| 113 | + Some(sym::TryInto) => FunctionKind::TryIntoFunction, |
| 114 | + _ => return, |
| 115 | + }, |
| 116 | + callee.span, |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments