2 in_macro_or_desugar, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite,
5 use crate::utils::{paths, resolve_node};
7 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
8 use rustc::{declare_tool_lint, impl_lint_pass};
9 use rustc_errors::Applicability;
11 declare_clippy_lint! {
12 /// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
14 /// **Why is this bad?** Redundant code.
16 /// **Known problems:** None.
20 /// // format!() returns a `String`
21 /// let s: String = format!("hello").into();
23 pub IDENTITY_CONVERSION,
25 "using always-identical `Into`/`From`/`IntoIter` conversions"
29 pub struct IdentityConversion {
30 try_desugar_arm: Vec<HirId>,
33 impl_lint_pass!(IdentityConversion => [IDENTITY_CONVERSION]);
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
36 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
37 if in_macro_or_desugar(e.span) {
41 if Some(&e.hir_id) == self.try_desugar_arm.last() {
46 ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
47 let e = match arms[0].body.node {
48 ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
51 if let ExprKind::Call(_, ref args) = e.node {
52 self.try_desugar_arm.push(args[0].hir_id);
58 ExprKind::MethodCall(ref name, .., ref args) => {
59 if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
60 let a = cx.tables.expr_ty(e);
61 let b = cx.tables.expr_ty(&args[0]);
62 if same_tys(cx, a, b) {
63 let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
65 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
68 "consider removing `.into()`",
70 Applicability::MachineApplicable, // snippet
75 if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
76 let a = cx.tables.expr_ty(e);
77 let b = cx.tables.expr_ty(&args[0]);
78 if same_tys(cx, a, b) {
79 let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
80 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
83 "consider removing `.into_iter()`",
85 Applicability::MachineApplicable, // snippet
92 ExprKind::Call(ref path, ref args) => {
93 if let ExprKind::Path(ref qpath) = path.node {
94 if let Some(def_id) = resolve_node(cx, qpath, path.hir_id).opt_def_id() {
95 if match_def_path(cx, def_id, &paths::FROM_FROM) {
96 let a = cx.tables.expr_ty(e);
97 let b = cx.tables.expr_ty(&args[0]);
98 if same_tys(cx, a, b) {
99 let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
101 format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
102 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
107 Applicability::MachineApplicable, // snippet
120 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
121 if Some(&e.hir_id) == self.try_desugar_arm.last() {
122 self.try_desugar_arm.pop();