Skip to content

Commit b7f85e8

Browse files
committed
Apply suggestions from PR review
* Move the lint to pedantic * Import used types instead of prefixing with `hir::`
1 parent 00b4f28 commit b7f85e8

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

clippy_lints/src/derive.rs

+16-28
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ use crate::utils::{
33
is_automatically_derived, is_copy, match_path, span_lint_and_help, span_lint_and_note, span_lint_and_then,
44
};
55
use if_chain::if_chain;
6-
use rustc_hir as hir;
76
use rustc_hir::def_id::DefId;
87
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, NestedVisitorMap, Visitor};
8+
use rustc_hir::{
9+
BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, TraitRef, UnsafeSource, Unsafety,
10+
};
911
use rustc_lint::{LateContext, LateLintPass};
1012
use rustc_middle::hir::map::Map;
1113
use rustc_middle::ty::{self, Ty};
@@ -97,15 +99,15 @@ declare_clippy_lint! {
9799
/// }
98100
/// ```
99101
pub UNSAFE_DERIVE_DESERIALIZE,
100-
correctness,
102+
pedantic,
101103
"deriving `serde::Deserialize` on a type that has methods using `unsafe`"
102104
}
103105

104106
declare_lint_pass!(Derive => [EXPL_IMPL_CLONE_ON_COPY, DERIVE_HASH_XOR_EQ, UNSAFE_DERIVE_DESERIALIZE]);
105107

106108
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
107-
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'_>) {
108-
if let hir::ItemKind::Impl {
109+
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
110+
if let ItemKind::Impl {
109111
of_trait: Some(ref trait_ref),
110112
..
111113
} = item.kind
@@ -128,7 +130,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
128130
fn check_hash_peq<'a, 'tcx>(
129131
cx: &LateContext<'a, 'tcx>,
130132
span: Span,
131-
trait_ref: &hir::TraitRef<'_>,
133+
trait_ref: &TraitRef<'_>,
132134
ty: Ty<'tcx>,
133135
hash_is_automatically_derived: bool,
134136
) {
@@ -175,12 +177,7 @@ fn check_hash_peq<'a, 'tcx>(
175177
}
176178

177179
/// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
178-
fn check_copy_clone<'a, 'tcx>(
179-
cx: &LateContext<'a, 'tcx>,
180-
item: &hir::Item<'_>,
181-
trait_ref: &hir::TraitRef<'_>,
182-
ty: Ty<'tcx>,
183-
) {
180+
fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item<'_>, trait_ref: &TraitRef<'_>, ty: Ty<'tcx>) {
184181
if match_path(&trait_ref.path, &paths::CLONE_TRAIT) {
185182
if !is_copy(cx, ty) {
186183
return;
@@ -223,16 +220,16 @@ fn check_copy_clone<'a, 'tcx>(
223220
/// Implementation of the `UNSAFE_DERIVE_DESERIALIZE` lint.
224221
fn check_unsafe_derive_deserialize<'a, 'tcx>(
225222
cx: &LateContext<'a, 'tcx>,
226-
item: &hir::Item<'_>,
227-
trait_ref: &hir::TraitRef<'_>,
223+
item: &Item<'_>,
224+
trait_ref: &TraitRef<'_>,
228225
ty: Ty<'tcx>,
229226
) {
230-
fn item_from_def_id<'tcx>(cx: &LateContext<'_, 'tcx>, def_id: DefId) -> &'tcx hir::Item<'tcx> {
227+
fn item_from_def_id<'tcx>(cx: &LateContext<'_, 'tcx>, def_id: DefId) -> &'tcx Item<'tcx> {
231228
let hir_id = cx.tcx.hir().as_local_hir_id(def_id).unwrap();
232229
cx.tcx.hir().expect_item(hir_id)
233230
}
234231

235-
fn has_unsafe<'tcx>(cx: &LateContext<'_, 'tcx>, item: &'tcx hir::Item<'_>) -> bool {
232+
fn has_unsafe<'tcx>(cx: &LateContext<'_, 'tcx>, item: &'tcx Item<'_>) -> bool {
236233
let mut visitor = UnsafeVisitor { cx, has_unsafe: false };
237234
walk_item(&mut visitor, item);
238235
visitor.has_unsafe
@@ -267,21 +264,14 @@ struct UnsafeVisitor<'a, 'tcx> {
267264
impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
268265
type Map = Map<'tcx>;
269266

270-
fn visit_fn(
271-
&mut self,
272-
kind: FnKind<'tcx>,
273-
decl: &'tcx hir::FnDecl<'_>,
274-
body_id: hir::BodyId,
275-
span: Span,
276-
id: hir::HirId,
277-
) {
267+
fn visit_fn(&mut self, kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body_id: BodyId, span: Span, id: HirId) {
278268
if self.has_unsafe {
279269
return;
280270
}
281271

282272
if_chain! {
283273
if let Some(header) = kind.header();
284-
if let hir::Unsafety::Unsafe = header.unsafety;
274+
if let Unsafety::Unsafe = header.unsafety;
285275
then {
286276
self.has_unsafe = true;
287277
}
@@ -290,14 +280,12 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
290280
walk_fn(self, kind, decl, body_id, span, id);
291281
}
292282

293-
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
283+
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
294284
if self.has_unsafe {
295285
return;
296286
}
297287

298-
if let hir::ExprKind::Block(block, _) = expr.kind {
299-
use hir::{BlockCheckMode, UnsafeSource};
300-
288+
if let ExprKind::Block(block, _) = expr.kind {
301289
match block.rules {
302290
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
303291
| BlockCheckMode::PushUnsafeBlock(UnsafeSource::UserProvided)

clippy_lints/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11061106
LintId::of(&default_trait_access::DEFAULT_TRAIT_ACCESS),
11071107
LintId::of(&dereference::EXPLICIT_DEREF_METHODS),
11081108
LintId::of(&derive::EXPL_IMPL_CLONE_ON_COPY),
1109+
LintId::of(&derive::UNSAFE_DERIVE_DESERIALIZE),
11091110
LintId::of(&doc::DOC_MARKDOWN),
11101111
LintId::of(&doc::MISSING_ERRORS_DOC),
11111112
LintId::of(&empty_enum::EMPTY_ENUM),
@@ -1197,7 +1198,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11971198
LintId::of(&copies::IFS_SAME_COND),
11981199
LintId::of(&copies::IF_SAME_THEN_ELSE),
11991200
LintId::of(&derive::DERIVE_HASH_XOR_EQ),
1200-
LintId::of(&derive::UNSAFE_DERIVE_DESERIALIZE),
12011201
LintId::of(&doc::MISSING_SAFETY_DOC),
12021202
LintId::of(&doc::NEEDLESS_DOCTEST_MAIN),
12031203
LintId::of(&double_comparison::DOUBLE_COMPARISONS),
@@ -1610,7 +1610,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16101610
LintId::of(&copies::IFS_SAME_COND),
16111611
LintId::of(&copies::IF_SAME_THEN_ELSE),
16121612
LintId::of(&derive::DERIVE_HASH_XOR_EQ),
1613-
LintId::of(&derive::UNSAFE_DERIVE_DESERIALIZE),
16141613
LintId::of(&drop_bounds::DROP_BOUNDS),
16151614
LintId::of(&drop_forget_ref::DROP_COPY),
16161615
LintId::of(&drop_forget_ref::DROP_REF),

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
23362336
},
23372337
Lint {
23382338
name: "unsafe_derive_deserialize",
2339-
group: "correctness",
2339+
group: "pedantic",
23402340
desc: "deriving `serde::Deserialize` on a type that has methods using `unsafe`",
23412341
deprecation: None,
23422342
module: "derive",

0 commit comments

Comments
 (0)