|
| 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()` |
| 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. |
| 44 | + && implements_trait(cx, self_ty, from_into_trait, &[other_ty]) |
| 45 | + // Avoid linting if this is an argument to a function: `f(i32.try_into())`. |
| 46 | + // The function expects a `Result` either way, so users would need to wrap it back in `Ok(..)` |
| 47 | + // and save on nothing |
| 48 | + && get_parent_expr(cx, expr).map_or(true, |parent| !matches!(parent.kind, ExprKind::Call(..))) |
| 49 | + { |
| 50 | + span_lint_and_sugg( |
| 51 | + cx, |
| 52 | + UNNECESSARY_FALLIBLE_CONVERSIONS, |
| 53 | + primary_span, |
| 54 | + match kind { |
| 55 | + FunctionKind::TryFromFunction => "calling `TryFrom::try_from` when `From` could be used", |
| 56 | + FunctionKind::TryIntoMethod |
| 57 | + | FunctionKind::TryIntoFunction => "calling `TryInto::try_into` when `Into` could be used", |
| 58 | + }, |
| 59 | + "use", |
| 60 | + match kind { |
| 61 | + FunctionKind::TryFromFunction => "From::from".into(), |
| 62 | + FunctionKind::TryIntoFunction => "Into::into".into(), |
| 63 | + FunctionKind::TryIntoMethod => "into".into(), |
| 64 | + }, |
| 65 | + // Suggestion likely results in compile errors, so it needs to be adjusted a bit by the user |
| 66 | + // (e.g. removing `unwrap()`s as a result, changing type annotations, ...) |
| 67 | + Applicability::Unspecified |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// Checks method call exprs: |
| 73 | +/// - `0i32.try_into()` |
| 74 | +pub(super) fn check_method(cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 75 | + if let ExprKind::MethodCall(path, ..) = expr.kind { |
| 76 | + check( |
| 77 | + cx, |
| 78 | + expr, |
| 79 | + cx.typeck_results().node_args(expr.hir_id), |
| 80 | + FunctionKind::TryIntoMethod, |
| 81 | + path.ident.span, |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// Checks function call exprs: |
| 87 | +/// - `<i64 as TryFrom<_>>::try_from(0i32)` |
| 88 | +/// - `<_ as TryInto<i64>>::try_into(0i32)` |
| 89 | +pub(super) fn check_function(cx: &LateContext<'_>, expr: &Expr<'_>, callee: &Expr<'_>) { |
| 90 | + if let ExprKind::Path(ref qpath) = callee.kind |
| 91 | + && let Some(item_def_id) = cx.qpath_res(qpath, callee.hir_id).opt_def_id() |
| 92 | + && let Some(trait_def_id) = cx.tcx.trait_of_item(item_def_id) |
| 93 | + { |
| 94 | + check( |
| 95 | + cx, |
| 96 | + expr, |
| 97 | + cx.typeck_results().node_args(callee.hir_id), |
| 98 | + match cx.tcx.get_diagnostic_name(trait_def_id) { |
| 99 | + Some(sym::TryFrom) => FunctionKind::TryFromFunction, |
| 100 | + Some(sym::TryInto) => FunctionKind::TryIntoFunction, |
| 101 | + _ => return, |
| 102 | + }, |
| 103 | + callee.span, |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments