X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fmem_discriminant.rs;h=7895ba9f1e07687154eb16e46d063964a12546ef;hb=b094bb1bd7f1cc702823c91ca509f338fedee24a;hp=0f8ccc7dedbc8304b17a99d428bfb9092514e441;hpb=ece8b8e7d6a37caa809c1fc1c8c8dd6263ea11ff;p=rust.git diff --git a/clippy_lints/src/mem_discriminant.rs b/clippy_lints/src/mem_discriminant.rs index 0f8ccc7dedb..7895ba9f1e0 100644 --- a/clippy_lints/src/mem_discriminant.rs +++ b/clippy_lints/src/mem_discriminant.rs @@ -1,57 +1,46 @@ -// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use crate::utils::{match_def_path, opt_def_id, paths, snippet, span_lint_and_then, walk_ptrs_ty_depth}; +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::source::snippet; +use clippy_utils::ty::walk_ptrs_ty_depth; +use clippy_utils::{match_def_path, paths}; use if_chain::if_chain; -use rustc::hir::{Expr, ExprKind}; -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; - +use rustc_hir::{BorrowKind, Expr, ExprKind}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; use std::iter; -/// **What it does:** Checks for calls of `mem::discriminant()` on a non-enum type. -/// -/// **Why is this bad?** The value of `mem::discriminant()` on non-enum types -/// is unspecified. -/// -/// **Known problems:** None. -/// -/// **Example:** -/// ```rust -/// mem::discriminant(&"hello"); -/// mem::discriminant(&&Some(2)); -/// ``` declare_clippy_lint! { + /// **What it does:** Checks for calls of `mem::discriminant()` on a non-enum type. + /// + /// **Why is this bad?** The value of `mem::discriminant()` on non-enum types + /// is unspecified. + /// + /// **Known problems:** None. + /// + /// **Example:** + /// ```rust + /// use std::mem; + /// + /// mem::discriminant(&"hello"); + /// mem::discriminant(&&Some(2)); + /// ``` pub MEM_DISCRIMINANT_NON_ENUM, correctness, - "calling mem::descriminant on non-enum type" + "calling `mem::descriminant` on non-enum type" } -pub struct MemDiscriminant; - -impl LintPass for MemDiscriminant { - fn get_lints(&self) -> LintArray { - lint_array![MEM_DISCRIMINANT_NON_ENUM] - } -} +declare_lint_pass!(MemDiscriminant => [MEM_DISCRIMINANT_NON_ENUM]); -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemDiscriminant { - fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { +impl<'tcx> LateLintPass<'tcx> for MemDiscriminant { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if_chain! { - if let ExprKind::Call(ref func, ref func_args) = expr.node; + if let ExprKind::Call(ref func, ref func_args) = expr.kind; // is `mem::discriminant` - if let ExprKind::Path(ref func_qpath) = func.node; - if let Some(def_id) = opt_def_id(cx.tables.qpath_def(func_qpath, func.hir_id)); - if match_def_path(cx.tcx, def_id, &paths::MEM_DISCRIMINANT); + if let ExprKind::Path(ref func_qpath) = func.kind; + if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id(); + if match_def_path(cx, def_id, &paths::MEM_DISCRIMINANT); // type is non-enum - let ty_param = cx.tables.node_substs(func.hir_id).type_at(0); + let ty_param = cx.typeck_results().node_substs(func.hir_id).type_at(0); if !ty_param.is_enum(); then { @@ -60,7 +49,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { MEM_DISCRIMINANT_NON_ENUM, expr.span, &format!("calling `mem::discriminant` on non-enum type `{}`", ty_param), - |db| { + |diag| { // if this is a reference to an enum, suggest dereferencing let (base_ty, ptr_depth) = walk_ptrs_ty_depth(ty_param); if ptr_depth >= 1 && base_ty.is_enum() { @@ -70,7 +59,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { let mut derefs_needed = ptr_depth; let mut cur_expr = param; while derefs_needed > 0 { - if let ExprKind::AddrOf(_, ref inner_expr) = cur_expr.node { + if let ExprKind::AddrOf(BorrowKind::Ref, _, ref inner_expr) = cur_expr.kind { derefs_needed -= 1; cur_expr = inner_expr; } else { @@ -79,7 +68,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { } let derefs: String = iter::repeat('*').take(derefs_needed).collect(); - db.span_suggestion_with_applicability( + diag.span_suggestion( param.span, "try dereferencing", format!("{}{}", derefs, snippet(cx, cur_expr.span, "")),