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.
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::rustc::hir::*;
14 use crate::syntax::ast::NodeId;
15 use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then};
16 use crate::utils::{opt_def_id, paths, resolve_node};
17 use crate::rustc_errors::Applicability;
19 /// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
21 /// **Why is this bad?** Redundant code.
23 /// **Known problems:** None.
27 /// // format!() returns a `String`
28 /// let s: String = format!("hello").into();
30 declare_clippy_lint! {
31 pub IDENTITY_CONVERSION,
33 "using always-identical `Into`/`From`/`IntoIter` conversions"
37 pub struct IdentityConversion {
38 try_desugar_arm: Vec<NodeId>,
41 impl LintPass for IdentityConversion {
42 fn get_lints(&self) -> LintArray {
43 lint_array!(IDENTITY_CONVERSION)
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
48 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
53 if Some(&e.id) == self.try_desugar_arm.last() {
58 ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
59 let e = match arms[0].body.node {
60 ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
63 if let ExprKind::Call(_, ref args) = e.node {
64 self.try_desugar_arm.push(args[0].id);
70 ExprKind::MethodCall(ref name, .., ref args) => {
71 if match_trait_method(cx, e, &paths::INTO[..]) && &*name.ident.as_str() == "into" {
72 let a = cx.tables.expr_ty(e);
73 let b = cx.tables.expr_ty(&args[0]);
74 if same_tys(cx, a, b) {
75 let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
77 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
78 db.span_suggestion_with_applicability(
80 "consider removing `.into()`",
82 Applicability::MachineApplicable, // snippet
87 if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
88 let a = cx.tables.expr_ty(e);
89 let b = cx.tables.expr_ty(&args[0]);
90 if same_tys(cx, a, b) {
91 let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
92 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
93 db.span_suggestion_with_applicability(
95 "consider removing `.into_iter()`",
97 Applicability::MachineApplicable, // snippet
104 ExprKind::Call(ref path, ref args) => if let ExprKind::Path(ref qpath) = path.node {
105 if let Some(def_id) = opt_def_id(resolve_node(cx, qpath, path.hir_id)) {
106 if match_def_path(cx.tcx, def_id, &paths::FROM_FROM[..]) {
107 let a = cx.tables.expr_ty(e);
108 let b = cx.tables.expr_ty(&args[0]);
109 if same_tys(cx, a, b) {
110 let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
111 let sugg_msg = format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
112 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
113 db.span_suggestion_with_applicability(
117 Applicability::MachineApplicable, // snippet
129 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
130 if Some(&e.id) == self.try_desugar_arm.last() {
131 self.try_desugar_arm.pop();