|  | 
| 1 | 1 | use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then}; | 
| 2 | 2 | use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_context}; | 
| 3 |  | -use clippy_utils::sugg::Sugg; | 
|  | 3 | +use clippy_utils::sugg::{DiagExt as _, Sugg}; | 
| 4 | 4 | use clippy_utils::ty::{is_copy, is_type_diagnostic_item, same_type_and_consts}; | 
| 5 |  | -use clippy_utils::{get_parent_expr, is_trait_method, is_ty_alias, path_to_local}; | 
|  | 5 | +use clippy_utils::{get_parent_expr, is_trait_item, is_trait_method, is_ty_alias, path_to_local}; | 
| 6 | 6 | use rustc_errors::Applicability; | 
| 7 | 7 | use rustc_hir::def_id::DefId; | 
| 8 | 8 | use rustc_hir::{BindingMode, Expr, ExprKind, HirId, MatchSource, Node, PatKind}; | 
| @@ -382,3 +382,27 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion { | 
| 382 | 382 |         } | 
| 383 | 383 |     } | 
| 384 | 384 | } | 
|  | 385 | + | 
|  | 386 | +/// Check if `arg` is a `Into::into` or `From::from` applied to `receiver` to give `expr`, through a | 
|  | 387 | +/// higher-order mapping function. | 
|  | 388 | +pub fn check_function_application(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, arg: &Expr<'_>) { | 
|  | 389 | +    let expr_ty = cx.typeck_results().expr_ty_adjusted(expr); | 
|  | 390 | +    if same_type_and_consts(expr_ty, cx.typeck_results().expr_ty_adjusted(receiver)) | 
|  | 391 | +        && (is_trait_item(cx, arg, sym::Into) || is_trait_item(cx, arg, sym::From)) | 
|  | 392 | +    { | 
|  | 393 | +        span_lint_and_then( | 
|  | 394 | +            cx, | 
|  | 395 | +            USELESS_CONVERSION, | 
|  | 396 | +            expr.span.with_lo(receiver.span.hi()), | 
|  | 397 | +            format!("useless conversion to the same type: `{expr_ty}`"), | 
|  | 398 | +            |diag| { | 
|  | 399 | +                diag.suggest_remove_item( | 
|  | 400 | +                    cx, | 
|  | 401 | +                    expr.span.with_lo(receiver.span.hi()), | 
|  | 402 | +                    "consider removing", | 
|  | 403 | +                    Applicability::MachineApplicable, | 
|  | 404 | +                ); | 
|  | 405 | +            }, | 
|  | 406 | +        ); | 
|  | 407 | +    } | 
|  | 408 | +} | 
0 commit comments