|
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,26 @@ 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`. |
| 387 | +pub fn check_function_application(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, arg: &Expr<'_>) { |
| 388 | + let expr_ty = cx.typeck_results().expr_ty_adjusted(expr); |
| 389 | + if same_type_and_consts(expr_ty, cx.typeck_results().expr_ty_adjusted(receiver)) |
| 390 | + && (is_trait_item(cx, arg, sym::Into) || is_trait_item(cx, arg, sym::From)) |
| 391 | + { |
| 392 | + span_lint_and_then( |
| 393 | + cx, |
| 394 | + USELESS_CONVERSION, |
| 395 | + expr.span.with_lo(receiver.span.hi()), |
| 396 | + format!("useless conversion to the same type: `{expr_ty}`"), |
| 397 | + |diag| { |
| 398 | + diag.suggest_remove_item( |
| 399 | + cx, |
| 400 | + expr.span.with_lo(receiver.span.hi()), |
| 401 | + "consider removing", |
| 402 | + Applicability::MachineApplicable, |
| 403 | + ); |
| 404 | + }, |
| 405 | + ); |
| 406 | + } |
| 407 | +} |
0 commit comments