1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::rustc::hir::*;
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::rustc_errors::Applicability;
14 use crate::syntax::ast::NodeId;
16 in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
18 use crate::utils::{opt_def_id, paths, resolve_node};
20 /// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
22 /// **Why is this bad?** Redundant code.
24 /// **Known problems:** None.
28 /// // format!() returns a `String`
29 /// let s: String = format!("hello").into();
31 declare_clippy_lint! {
32 pub IDENTITY_CONVERSION,
34 "using always-identical `Into`/`From`/`IntoIter` conversions"
38 pub struct IdentityConversion {
39 try_desugar_arm: Vec<NodeId>,
42 impl LintPass for IdentityConversion {
43 fn get_lints(&self) -> LintArray {
44 lint_array!(IDENTITY_CONVERSION)
48 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
49 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
54 if Some(&e.id) == self.try_desugar_arm.last() {
59 ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
60 let e = match arms[0].body.node {
61 ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
64 if let ExprKind::Call(_, ref args) = e.node {
65 self.try_desugar_arm.push(args[0].id);
71 ExprKind::MethodCall(ref name, .., ref args) => {
72 if match_trait_method(cx, e, &paths::INTO[..]) && &*name.ident.as_str() == "into" {
73 let a = cx.tables.expr_ty(e);
74 let b = cx.tables.expr_ty(&args[0]);
75 if same_tys(cx, a, b) {
76 let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
78 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
79 db.span_suggestion_with_applicability(
81 "consider removing `.into()`",
83 Applicability::MachineApplicable, // snippet
88 if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
89 let a = cx.tables.expr_ty(e);
90 let b = cx.tables.expr_ty(&args[0]);
91 if same_tys(cx, a, b) {
92 let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
93 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
94 db.span_suggestion_with_applicability(
96 "consider removing `.into_iter()`",
98 Applicability::MachineApplicable, // snippet
105 ExprKind::Call(ref path, ref args) => {
106 if let ExprKind::Path(ref qpath) = path.node {
107 if let Some(def_id) = opt_def_id(resolve_node(cx, qpath, path.hir_id)) {
108 if match_def_path(cx.tcx, def_id, &paths::FROM_FROM[..]) {
109 let a = cx.tables.expr_ty(e);
110 let b = cx.tables.expr_ty(&args[0]);
111 if same_tys(cx, a, b) {
112 let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
114 format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
115 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
116 db.span_suggestion_with_applicability(
120 Applicability::MachineApplicable, // snippet
133 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
134 if Some(&e.id) == self.try_desugar_arm.last() {
135 self.try_desugar_arm.pop();