2 in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
4 use crate::utils::{opt_def_id, paths, resolve_node};
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_tool_lint, lint_array};
8 use rustc_errors::Applicability;
9 use syntax::ast::NodeId;
11 /// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
13 /// **Why is this bad?** Redundant code.
15 /// **Known problems:** None.
19 /// // format!() returns a `String`
20 /// let s: String = format!("hello").into();
22 declare_clippy_lint! {
23 pub IDENTITY_CONVERSION,
25 "using always-identical `Into`/`From`/`IntoIter` conversions"
29 pub struct IdentityConversion {
30 try_desugar_arm: Vec<NodeId>,
33 impl LintPass for IdentityConversion {
34 fn get_lints(&self) -> LintArray {
35 lint_array!(IDENTITY_CONVERSION)
39 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
40 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
45 if Some(&e.id) == self.try_desugar_arm.last() {
50 ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
51 let e = match arms[0].body.node {
52 ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
55 if let ExprKind::Call(_, ref args) = e.node {
56 self.try_desugar_arm.push(args[0].id);
62 ExprKind::MethodCall(ref name, .., ref args) => {
63 if match_trait_method(cx, e, &paths::INTO[..]) && &*name.ident.as_str() == "into" {
64 let a = cx.tables.expr_ty(e);
65 let b = cx.tables.expr_ty(&args[0]);
66 if same_tys(cx, a, b) {
67 let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
69 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
70 db.span_suggestion_with_applicability(
72 "consider removing `.into()`",
74 Applicability::MachineApplicable, // snippet
79 if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
80 let a = cx.tables.expr_ty(e);
81 let b = cx.tables.expr_ty(&args[0]);
82 if same_tys(cx, a, b) {
83 let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
84 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
85 db.span_suggestion_with_applicability(
87 "consider removing `.into_iter()`",
89 Applicability::MachineApplicable, // snippet
96 ExprKind::Call(ref path, ref args) => {
97 if let ExprKind::Path(ref qpath) = path.node {
98 if let Some(def_id) = opt_def_id(resolve_node(cx, qpath, path.hir_id)) {
99 if match_def_path(cx.tcx, def_id, &paths::FROM_FROM[..]) {
100 let a = cx.tables.expr_ty(e);
101 let b = cx.tables.expr_ty(&args[0]);
102 if same_tys(cx, a, b) {
103 let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
105 format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
106 span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
107 db.span_suggestion_with_applicability(
111 Applicability::MachineApplicable, // snippet
124 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
125 if Some(&e.id) == self.try_desugar_arm.last() {
126 self.try_desugar_arm.pop();