X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fidentity_conversion.rs;h=c37bb02708f0b55c851e3741d0fcad7db6f78448;hb=6d1225981177587fbb68d9c4902c770c3dbaafb0;hp=b09031b553f9ffccfc7c4b9e18b298c332201baa;hpb=6e8931c5f5d72a324d06ea36216c0156fbefaac1;p=rust.git diff --git a/clippy_lints/src/identity_conversion.rs b/clippy_lints/src/identity_conversion.rs index b09031b553f..c37bb02708f 100644 --- a/clippy_lints/src/identity_conversion.rs +++ b/clippy_lints/src/identity_conversion.rs @@ -1,24 +1,24 @@ use crate::utils::{ - in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then, + match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then, }; -use crate::utils::{opt_def_id, paths, resolve_node}; use rustc::hir::*; +use rustc::impl_lint_pass; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; +use rustc_session::declare_tool_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(); -/// ``` 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" @@ -29,19 +29,11 @@ pub struct IdentityConversion { try_desugar_arm: Vec, } -impl LintPass for IdentityConversion { - fn get_lints(&self) -> LintArray { - lint_array!(IDENTITY_CONVERSION) - } - - fn name(&self) -> &'static str { - "IdentityConversion" - } -} +impl_lint_pass!(IdentityConversion => [IDENTITY_CONVERSION]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { - if in_macro(e.span) { + if e.span.from_expansion() { return; } @@ -49,21 +41,19 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { return; } - match e.node { + match e.kind { ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => { - let e = match arms[0].body.node { + let e = match arms[0].body.kind { ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e, _ => return, }; - if let ExprKind::Call(_, ref args) = e.node { + if let ExprKind::Call(_, ref args) = e.kind { self.try_desugar_arm.push(args[0].hir_id); - } else { - return; } }, ExprKind::MethodCall(ref name, .., ref args) => { - if match_trait_method(cx, e, &paths::INTO[..]) && &*name.ident.as_str() == "into" { + if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" { let a = cx.tables.expr_ty(e); let b = cx.tables.expr_ty(&args[0]); if same_tys(cx, a, b) { @@ -97,9 +87,9 @@ 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[..]) { + if let ExprKind::Path(ref qpath) = path.kind { + if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id() { + if match_def_path(cx, 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) {