Apply suggestions from PR review

* Move the lint to pedantic
* Import used types instead of prefixing with `hir::`
This commit is contained in:
Eduardo Broto
2020-04-20 20:05:15 +02:00
parent 00b4f2819f
commit b7f85e8706
3 changed files with 18 additions and 31 deletions

View File

@@ -3,9 +3,11 @@ use crate::utils::{
is_automatically_derived, is_copy, match_path, span_lint_and_help, span_lint_and_note, span_lint_and_then, is_automatically_derived, is_copy, match_path, span_lint_and_help, span_lint_and_note, span_lint_and_then,
}; };
use if_chain::if_chain; use if_chain::if_chain;
use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, NestedVisitorMap, Visitor}; use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, NestedVisitorMap, Visitor};
use rustc_hir::{
BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, TraitRef, UnsafeSource, Unsafety,
};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::map::Map; use rustc_middle::hir::map::Map;
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty};
@@ -97,15 +99,15 @@ declare_clippy_lint! {
/// } /// }
/// ``` /// ```
pub UNSAFE_DERIVE_DESERIALIZE, pub UNSAFE_DERIVE_DESERIALIZE,
correctness, pedantic,
"deriving `serde::Deserialize` on a type that has methods using `unsafe`" "deriving `serde::Deserialize` on a type that has methods using `unsafe`"
} }
declare_lint_pass!(Derive => [EXPL_IMPL_CLONE_ON_COPY, DERIVE_HASH_XOR_EQ, UNSAFE_DERIVE_DESERIALIZE]); declare_lint_pass!(Derive => [EXPL_IMPL_CLONE_ON_COPY, DERIVE_HASH_XOR_EQ, UNSAFE_DERIVE_DESERIALIZE]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'_>) { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
if let hir::ItemKind::Impl { if let ItemKind::Impl {
of_trait: Some(ref trait_ref), of_trait: Some(ref trait_ref),
.. ..
} = item.kind } = item.kind
@@ -128,7 +130,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
fn check_hash_peq<'a, 'tcx>( fn check_hash_peq<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>, cx: &LateContext<'a, 'tcx>,
span: Span, span: Span,
trait_ref: &hir::TraitRef<'_>, trait_ref: &TraitRef<'_>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
hash_is_automatically_derived: bool, hash_is_automatically_derived: bool,
) { ) {
@@ -175,12 +177,7 @@ fn check_hash_peq<'a, 'tcx>(
} }
/// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint. /// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
fn check_copy_clone<'a, 'tcx>( fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item<'_>, trait_ref: &TraitRef<'_>, ty: Ty<'tcx>) {
cx: &LateContext<'a, 'tcx>,
item: &hir::Item<'_>,
trait_ref: &hir::TraitRef<'_>,
ty: Ty<'tcx>,
) {
if match_path(&trait_ref.path, &paths::CLONE_TRAIT) { if match_path(&trait_ref.path, &paths::CLONE_TRAIT) {
if !is_copy(cx, ty) { if !is_copy(cx, ty) {
return; return;
@@ -223,16 +220,16 @@ fn check_copy_clone<'a, 'tcx>(
/// Implementation of the `UNSAFE_DERIVE_DESERIALIZE` lint. /// Implementation of the `UNSAFE_DERIVE_DESERIALIZE` lint.
fn check_unsafe_derive_deserialize<'a, 'tcx>( fn check_unsafe_derive_deserialize<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>, cx: &LateContext<'a, 'tcx>,
item: &hir::Item<'_>, item: &Item<'_>,
trait_ref: &hir::TraitRef<'_>, trait_ref: &TraitRef<'_>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
) { ) {
fn item_from_def_id<'tcx>(cx: &LateContext<'_, 'tcx>, def_id: DefId) -> &'tcx hir::Item<'tcx> { fn item_from_def_id<'tcx>(cx: &LateContext<'_, 'tcx>, def_id: DefId) -> &'tcx Item<'tcx> {
let hir_id = cx.tcx.hir().as_local_hir_id(def_id).unwrap(); let hir_id = cx.tcx.hir().as_local_hir_id(def_id).unwrap();
cx.tcx.hir().expect_item(hir_id) cx.tcx.hir().expect_item(hir_id)
} }
fn has_unsafe<'tcx>(cx: &LateContext<'_, 'tcx>, item: &'tcx hir::Item<'_>) -> bool { fn has_unsafe<'tcx>(cx: &LateContext<'_, 'tcx>, item: &'tcx Item<'_>) -> bool {
let mut visitor = UnsafeVisitor { cx, has_unsafe: false }; let mut visitor = UnsafeVisitor { cx, has_unsafe: false };
walk_item(&mut visitor, item); walk_item(&mut visitor, item);
visitor.has_unsafe visitor.has_unsafe
@@ -267,21 +264,14 @@ struct UnsafeVisitor<'a, 'tcx> {
impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> { impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
type Map = Map<'tcx>; type Map = Map<'tcx>;
fn visit_fn( fn visit_fn(&mut self, kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body_id: BodyId, span: Span, id: HirId) {
&mut self,
kind: FnKind<'tcx>,
decl: &'tcx hir::FnDecl<'_>,
body_id: hir::BodyId,
span: Span,
id: hir::HirId,
) {
if self.has_unsafe { if self.has_unsafe {
return; return;
} }
if_chain! { if_chain! {
if let Some(header) = kind.header(); if let Some(header) = kind.header();
if let hir::Unsafety::Unsafe = header.unsafety; if let Unsafety::Unsafe = header.unsafety;
then { then {
self.has_unsafe = true; self.has_unsafe = true;
} }
@@ -290,14 +280,12 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
walk_fn(self, kind, decl, body_id, span, id); walk_fn(self, kind, decl, body_id, span, id);
} }
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) { fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
if self.has_unsafe { if self.has_unsafe {
return; return;
} }
if let hir::ExprKind::Block(block, _) = expr.kind { if let ExprKind::Block(block, _) = expr.kind {
use hir::{BlockCheckMode, UnsafeSource};
match block.rules { match block.rules {
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
| BlockCheckMode::PushUnsafeBlock(UnsafeSource::UserProvided) | BlockCheckMode::PushUnsafeBlock(UnsafeSource::UserProvided)

View File

@@ -1106,6 +1106,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&default_trait_access::DEFAULT_TRAIT_ACCESS), LintId::of(&default_trait_access::DEFAULT_TRAIT_ACCESS),
LintId::of(&dereference::EXPLICIT_DEREF_METHODS), LintId::of(&dereference::EXPLICIT_DEREF_METHODS),
LintId::of(&derive::EXPL_IMPL_CLONE_ON_COPY), LintId::of(&derive::EXPL_IMPL_CLONE_ON_COPY),
LintId::of(&derive::UNSAFE_DERIVE_DESERIALIZE),
LintId::of(&doc::DOC_MARKDOWN), LintId::of(&doc::DOC_MARKDOWN),
LintId::of(&doc::MISSING_ERRORS_DOC), LintId::of(&doc::MISSING_ERRORS_DOC),
LintId::of(&empty_enum::EMPTY_ENUM), LintId::of(&empty_enum::EMPTY_ENUM),
@@ -1197,7 +1198,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&copies::IFS_SAME_COND), LintId::of(&copies::IFS_SAME_COND),
LintId::of(&copies::IF_SAME_THEN_ELSE), LintId::of(&copies::IF_SAME_THEN_ELSE),
LintId::of(&derive::DERIVE_HASH_XOR_EQ), LintId::of(&derive::DERIVE_HASH_XOR_EQ),
LintId::of(&derive::UNSAFE_DERIVE_DESERIALIZE),
LintId::of(&doc::MISSING_SAFETY_DOC), LintId::of(&doc::MISSING_SAFETY_DOC),
LintId::of(&doc::NEEDLESS_DOCTEST_MAIN), LintId::of(&doc::NEEDLESS_DOCTEST_MAIN),
LintId::of(&double_comparison::DOUBLE_COMPARISONS), LintId::of(&double_comparison::DOUBLE_COMPARISONS),
@@ -1610,7 +1610,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&copies::IFS_SAME_COND), LintId::of(&copies::IFS_SAME_COND),
LintId::of(&copies::IF_SAME_THEN_ELSE), LintId::of(&copies::IF_SAME_THEN_ELSE),
LintId::of(&derive::DERIVE_HASH_XOR_EQ), LintId::of(&derive::DERIVE_HASH_XOR_EQ),
LintId::of(&derive::UNSAFE_DERIVE_DESERIALIZE),
LintId::of(&drop_bounds::DROP_BOUNDS), LintId::of(&drop_bounds::DROP_BOUNDS),
LintId::of(&drop_forget_ref::DROP_COPY), LintId::of(&drop_forget_ref::DROP_COPY),
LintId::of(&drop_forget_ref::DROP_REF), LintId::of(&drop_forget_ref::DROP_REF),

View File

@@ -2336,7 +2336,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
}, },
Lint { Lint {
name: "unsafe_derive_deserialize", name: "unsafe_derive_deserialize",
group: "correctness", group: "pedantic",
desc: "deriving `serde::Deserialize` on a type that has methods using `unsafe`", desc: "deriving `serde::Deserialize` on a type that has methods using `unsafe`",
deprecation: None, deprecation: None,
module: "derive", module: "derive",