]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/identity_conversion.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / identity_conversion.rs
index 5b1bd0ada7be651285771bd665591daf3a02690c..7391f0a5208eba617164a51bac329353315d7a25 100644 (file)
@@ -1,23 +1,24 @@
-use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use crate::rustc::{declare_tool_lint, lint_array};
-use crate::rustc::hir::*;
-use crate::syntax::ast::NodeId;
-use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, span_lint_and_then};
-use crate::utils::{opt_def_id, paths, resolve_node};
-use crate::rustc_errors::Applicability;
+use crate::utils::{
+    in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
+};
+use crate::utils::{paths, resolve_node};
+use rustc::hir::*;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::{declare_tool_lint, lint_array};
+use rustc_errors::Applicability;
 
-/// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
-///
-/// **Why is this bad?** Redundant code.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// // format!() returns a `String`
-/// let s: String = format!("hello").into();
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
+    ///
+    /// **Why is this bad?** Redundant code.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// // format!() returns a `String`
+    /// let s: String = format!("hello").into();
+    /// ```
     pub IDENTITY_CONVERSION,
     complexity,
     "using always-identical `Into`/`From`/`IntoIter` conversions"
 
 #[derive(Default)]
 pub struct IdentityConversion {
-    try_desugar_arm: Vec<NodeId>,
+    try_desugar_arm: Vec<HirId>,
 }
 
 impl LintPass for IdentityConversion {
     fn get_lints(&self) -> LintArray {
         lint_array!(IDENTITY_CONVERSION)
     }
+
+    fn name(&self) -> &'static str {
+        "IdentityConversion"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
@@ -40,7 +45,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
             return;
         }
 
-        if Some(&e.id) == self.try_desugar_arm.last() {
+        if Some(&e.hir_id) == self.try_desugar_arm.last() {
             return;
         }
 
@@ -51,7 +56,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
                     _ => return,
                 };
                 if let ExprKind::Call(_, ref args) = e.node {
-                    self.try_desugar_arm.push(args[0].id);
+                    self.try_desugar_arm.push(args[0].hir_id);
                 } else {
                     return;
                 }
@@ -62,9 +67,10 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
                     let a = cx.tables.expr_ty(e);
                     let b = cx.tables.expr_ty(&args[0]);
                     if same_tys(cx, a, b) {
-                        let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
+                        let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
+
                         span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
-                            db.span_suggestion_with_applicability(
+                            db.span_suggestion(
                                 e.span,
                                 "consider removing `.into()`",
                                 sugg,
@@ -79,7 +85,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
                     if same_tys(cx, a, b) {
                         let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
                         span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
-                            db.span_suggestion_with_applicability(
+                            db.span_suggestion(
                                 e.span,
                                 "consider removing `.into_iter()`",
                                 sugg,
@@ -90,22 +96,25 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
                 }
             },
 
-            ExprKind::Call(ref path, ref args) => if let ExprKind::Path(ref qpath) = path.node {
-                if let Some(def_id) = opt_def_id(resolve_node(cx, qpath, path.hir_id)) {
-                    if match_def_path(cx.tcx, def_id, &paths::FROM_FROM[..]) {
-                        let a = cx.tables.expr_ty(e);
-                        let b = cx.tables.expr_ty(&args[0]);
-                        if same_tys(cx, a, b) {
-                            let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
-                            let sugg_msg = format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
-                            span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
-                                db.span_suggestion_with_applicability(
-                                    e.span,
-                                    &sugg_msg,
-                                    sugg,
-                                    Applicability::MachineApplicable, // snippet
-                                );
-                            });
+            ExprKind::Call(ref path, ref args) => {
+                if let ExprKind::Path(ref qpath) = path.node {
+                    if let Some(def_id) = resolve_node(cx, qpath, path.hir_id).opt_def_id() {
+                        if match_def_path(cx.tcx, def_id, &paths::FROM_FROM[..]) {
+                            let a = cx.tables.expr_ty(e);
+                            let b = cx.tables.expr_ty(&args[0]);
+                            if same_tys(cx, a, b) {
+                                let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
+                                let sugg_msg =
+                                    format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
+                                span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
+                                    db.span_suggestion(
+                                        e.span,
+                                        &sugg_msg,
+                                        sugg,
+                                        Applicability::MachineApplicable, // snippet
+                                    );
+                                });
+                            }
                         }
                     }
                 }
@@ -116,7 +125,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
     }
 
     fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
-        if Some(&e.id) == self.try_desugar_arm.last() {
+        if Some(&e.hir_id) == self.try_desugar_arm.last() {
             self.try_desugar_arm.pop();
         }
     }