Skip to content

Commit dfb41f4

Browse files
committed
Separate out a hir::Impl struct
This makes it possible to pass the `Impl` directly to functions, instead of having to pass each of the many fields one at a time. It also simplifies matches in many cases.
1 parent 9e45a23 commit dfb41f4

24 files changed

+61
-69
lines changed

clippy_lints/src/copy_iterator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{is_copy, match_path, paths, span_lint_and_note};
2-
use rustc_hir::{Item, ItemKind};
2+
use rustc_hir::{Item, ItemKind, Impl};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
55

@@ -33,10 +33,10 @@ declare_lint_pass!(CopyIterator => [COPY_ITERATOR]);
3333

3434
impl<'tcx> LateLintPass<'tcx> for CopyIterator {
3535
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
36-
if let ItemKind::Impl {
36+
if let ItemKind::Impl(Impl {
3737
of_trait: Some(ref trait_ref),
3838
..
39-
} = item.kind
39+
}) = item.kind
4040
{
4141
let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id));
4242

clippy_lints/src/derive.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use if_chain::if_chain;
77
use rustc_hir::def_id::DefId;
88
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, NestedVisitorMap, Visitor};
99
use rustc_hir::{
10-
BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, TraitRef, UnsafeSource, Unsafety,
10+
BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, Impl, TraitRef, UnsafeSource, Unsafety,
1111
};
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_middle::hir::map::Map;
@@ -164,10 +164,10 @@ declare_lint_pass!(Derive => [
164164

165165
impl<'tcx> LateLintPass<'tcx> for Derive {
166166
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
167-
if let ItemKind::Impl {
167+
if let ItemKind::Impl(Impl {
168168
of_trait: Some(ref trait_ref),
169169
..
170-
} = item.kind
170+
}) = item.kind
171171
{
172172
let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id));
173173
let is_automatically_derived = is_automatically_derived(&*item.attrs);

clippy_lints/src/doc.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,8 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
182182
lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers, Some(body_id));
183183
}
184184
},
185-
hir::ItemKind::Impl {
186-
of_trait: ref trait_ref,
187-
..
188-
} => {
189-
self.in_trait_impl = trait_ref.is_some();
185+
hir::ItemKind::Impl(ref impl_) => {
186+
self.in_trait_impl = impl_.of_trait.is_some();
190187
},
191188
_ => {},
192189
}

clippy_lints/src/escape.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_hir::intravisit;
2-
use rustc_hir::{self, Body, FnDecl, HirId, HirIdSet, ItemKind, Node};
2+
use rustc_hir::{self, Body, FnDecl, HirId, HirIdSet, ItemKind, Impl, Node};
33
use rustc_infer::infer::TyCtxtInferExt;
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_middle::ty::{self, Ty};
@@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
7777
let parent_node = cx.tcx.hir().find(parent_id);
7878

7979
if let Some(Node::Item(item)) = parent_node {
80-
if let ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
80+
if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = item.kind {
8181
return;
8282
}
8383
}

clippy_lints/src/fallible_impl_from.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ impl<'tcx> LateLintPass<'tcx> for FallibleImplFrom {
5757
// check for `impl From<???> for ..`
5858
let impl_def_id = cx.tcx.hir().local_def_id(item.hir_id);
5959
if_chain! {
60-
if let hir::ItemKind::Impl{ items: impl_items, .. } = item.kind;
60+
if let hir::ItemKind::Impl(impl_) = &item.kind;
6161
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id);
6262
if match_def_path(cx, impl_trait_ref.def_id, &FROM_TRAIT);
6363
then {
64-
lint_impl_body(cx, item.span, impl_items);
64+
lint_impl_body(cx, item.span, impl_.items);
6565
}
6666
}
6767
}

clippy_lints/src/inherent_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::utils::{in_macro, span_lint_and_then};
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_hir::{def_id, Crate, Item, ItemKind};
5+
use rustc_hir::{def_id, Crate, Item, ItemKind, Impl};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_tool_lint, impl_lint_pass};
88
use rustc_span::Span;
@@ -49,11 +49,11 @@ impl_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]);
4949

5050
impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
5151
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) {
52-
if let ItemKind::Impl {
52+
if let ItemKind::Impl(Impl {
5353
ref generics,
5454
of_trait: None,
5555
..
56-
} = item.kind
56+
}) = item.kind
5757
{
5858
// Remember for each inherent implementation encountered its span and generics
5959
// but filter out implementations that have generic params (type or lifetime)

clippy_lints/src/len_zero.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::ast::LitKind;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_errors::Applicability;
55
use rustc_hir::def_id::DefId;
6-
use rustc_hir::{AssocItemKind, BinOpKind, Expr, ExprKind, ImplItemRef, Item, ItemKind, TraitItemRef};
6+
use rustc_hir::{AssocItemKind, BinOpKind, Expr, ExprKind, ImplItemRef, Item, ItemKind, Impl, TraitItemRef};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty;
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -115,11 +115,11 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
115115

116116
match item.kind {
117117
ItemKind::Trait(_, _, _, _, ref trait_items) => check_trait_items(cx, item, trait_items),
118-
ItemKind::Impl {
118+
ItemKind::Impl(Impl {
119119
of_trait: None,
120120
items: ref impl_items,
121121
..
122-
} => check_impl_items(cx, item, impl_items),
122+
}) => check_impl_items(cx, item, impl_items),
123123
_ => (),
124124
}
125125
}

clippy_lints/src/methods/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16261626
let self_ty = cx.tcx.type_of(def_id);
16271627

16281628
// if this impl block implements a trait, lint in trait definition instead
1629-
if let hir::ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
1629+
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
16301630
return;
16311631
}
16321632

clippy_lints/src/needless_pass_by_value.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::ast::Attribute;
88
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
99
use rustc_errors::{Applicability, DiagnosticBuilder};
1010
use rustc_hir::intravisit::FnKind;
11-
use rustc_hir::{BindingAnnotation, Body, FnDecl, GenericArg, HirId, ItemKind, Node, PatKind, QPath, TyKind};
11+
use rustc_hir::{BindingAnnotation, Body, FnDecl, GenericArg, HirId, ItemKind, Impl, Node, PatKind, QPath, TyKind};
1212
use rustc_infer::infer::TyCtxtInferExt;
1313
use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_middle::ty::{self, TypeFoldable};
@@ -92,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
9292
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
9393
if matches!(
9494
item.kind,
95-
ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
95+
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)
9696
) {
9797
return;
9898
}

clippy_lints/src/new_without_default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ impl_lint_pass!(NewWithoutDefault => [NEW_WITHOUT_DEFAULT]);
6060
impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
6161
#[allow(clippy::too_many_lines)]
6262
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
63-
if let hir::ItemKind::Impl {
63+
if let hir::ItemKind::Impl(hir::Impl {
6464
of_trait: None, items, ..
65-
} = item.kind
65+
}) = item.kind
6666
{
6767
for assoc_item in items {
6868
if let hir::AssocItemKind::Fn { has_self: false } = assoc_item.kind {

clippy_lints/src/non_copy_const.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ptr;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::DefId;
99
use rustc_hir::{
10-
BodyId, Expr, ExprKind, HirId, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind, UnOp,
10+
BodyId, Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind, UnOp,
1111
};
1212
use rustc_infer::traits::specialization_graph;
1313
use rustc_lint::{LateContext, LateLintPass, Lint};
@@ -275,10 +275,10 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
275275
let item = cx.tcx.hir().expect_item(item_hir_id);
276276

277277
match &item.kind {
278-
ItemKind::Impl {
278+
ItemKind::Impl(Impl {
279279
of_trait: Some(of_trait_ref),
280280
..
281-
} => {
281+
}) => {
282282
if_chain! {
283283
// Lint a trait impl item only when the definition is a generic type,
284284
// assuming a assoc const is not meant to be a interior mutable type.
@@ -317,7 +317,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
317317
}
318318
}
319319
},
320-
ItemKind::Impl { of_trait: None, .. } => {
320+
ItemKind::Impl(Impl { of_trait: None, .. }) => {
321321
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
322322
// Normalize assoc types originated from generic params.
323323
let normalized = cx.tcx.normalize_erasing_regions(cx.param_env, ty);

clippy_lints/src/partialeq_ne_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{is_automatically_derived, span_lint_hir};
22
use if_chain::if_chain;
3-
use rustc_hir::{Item, ItemKind};
3+
use rustc_hir::{Item, ItemKind, Impl};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
66
use rustc_span::sym;
@@ -34,7 +34,7 @@ declare_lint_pass!(PartialEqNeImpl => [PARTIALEQ_NE_IMPL]);
3434
impl<'tcx> LateLintPass<'tcx> for PartialEqNeImpl {
3535
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
3636
if_chain! {
37-
if let ItemKind::Impl{ of_trait: Some(ref trait_ref), items: impl_items, .. } = item.kind;
37+
if let ItemKind::Impl(Impl { of_trait: Some(ref trait_ref), items: impl_items, .. }) = item.kind;
3838
if !is_automatically_derived(&*item.attrs);
3939
if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
4040
if trait_ref.path.res.def_id() == eq_trait;

clippy_lints/src/pass_by_ref_or_value.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::attr;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
88
use rustc_hir::intravisit::FnKind;
9-
use rustc_hir::{BindingAnnotation, Body, FnDecl, HirId, ItemKind, MutTy, Mutability, Node, PatKind};
9+
use rustc_hir::{BindingAnnotation, Body, FnDecl, HirId, ItemKind, MutTy, Mutability, Node, PatKind, Impl};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::ty;
1212
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -246,7 +246,7 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
246246
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
247247
if matches!(
248248
item.kind,
249-
ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
249+
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)
250250
) {
251251
return;
252252
}

clippy_lints/src/ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::utils::{
88
use if_chain::if_chain;
99
use rustc_errors::Applicability;
1010
use rustc_hir::{
11-
BinOpKind, BodyId, Expr, ExprKind, FnDecl, FnRetTy, GenericArg, HirId, ImplItem, ImplItemKind, Item, ItemKind,
11+
BinOpKind, BodyId, Expr, ExprKind, FnDecl, FnRetTy, GenericArg, HirId, ImplItem, ImplItemKind, Item, ItemKind, Impl,
1212
Lifetime, MutTy, Mutability, Node, PathSegment, QPath, TraitFn, TraitItem, TraitItemKind, Ty, TyKind,
1313
};
1414
use rustc_lint::{LateContext, LateLintPass};
@@ -132,7 +132,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
132132
if let ImplItemKind::Fn(ref sig, body_id) = item.kind {
133133
let parent_item = cx.tcx.hir().get_parent_item(item.hir_id);
134134
if let Some(Node::Item(it)) = cx.tcx.hir().find(parent_item) {
135-
if let ItemKind::Impl { of_trait: Some(_), .. } = it.kind {
135+
if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = it.kind {
136136
return; // ignore trait impls
137137
}
138138
}

clippy_lints/src/serde_api.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{get_trait_def_id, paths, span_lint};
2-
use rustc_hir::{Item, ItemKind};
2+
use rustc_hir::{Item, ItemKind, Impl};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
55

@@ -22,11 +22,11 @@ declare_lint_pass!(SerdeAPI => [SERDE_API_MISUSE]);
2222

2323
impl<'tcx> LateLintPass<'tcx> for SerdeAPI {
2424
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
25-
if let ItemKind::Impl {
25+
if let ItemKind::Impl(Impl {
2626
of_trait: Some(ref trait_ref),
2727
items,
2828
..
29-
} = item.kind
29+
}) = item.kind
3030
{
3131
let did = trait_ref.path.res.def_id();
3232
if let Some(visit_did) = get_trait_def_id(cx, &paths::SERDE_DE_VISITOR) {

clippy_lints/src/to_string_in_display.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::{match_def_path, match_trait_method, paths, qpath_res, span_lint};
22
use if_chain::if_chain;
33
use rustc_hir::def::Res;
4-
use rustc_hir::{Expr, ExprKind, HirId, ImplItem, ImplItemKind, Item, ItemKind};
4+
use rustc_hir::{Expr, ExprKind, HirId, ImplItem, ImplItemKind, Item, ItemKind, Impl};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
77

@@ -111,7 +111,7 @@ impl LateLintPass<'_> for ToStringInDisplay {
111111

112112
fn is_display_impl(cx: &LateContext<'_>, item: &Item<'_>) -> bool {
113113
if_chain! {
114-
if let ItemKind::Impl { of_trait: Some(trait_ref), .. } = &item.kind;
114+
if let ItemKind::Impl(Impl { of_trait: Some(trait_ref), .. }) = &item.kind;
115115
if let Some(did) = trait_ref.trait_def_id();
116116
then {
117117
match_def_path(cx, did, &paths::DISPLAY_TRAIT)

clippy_lints/src/types.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
258258
fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) {
259259
// Skip trait implementations; see issue #605.
260260
if let Some(hir::Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(id)) {
261-
if let ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
261+
if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
262262
return;
263263
}
264264
}
@@ -2558,21 +2558,16 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
25582558
}
25592559

25602560
match item.kind {
2561-
ItemKind::Impl {
2562-
ref generics,
2563-
self_ty: ref ty,
2564-
ref items,
2565-
..
2566-
} => {
2561+
ItemKind::Impl(ref impl_) => {
25672562
let mut vis = ImplicitHasherTypeVisitor::new(cx);
2568-
vis.visit_ty(ty);
2563+
vis.visit_ty(impl_.self_ty);
25692564

25702565
for target in &vis.found {
25712566
if differing_macro_contexts(item.span, target.span()) {
25722567
return;
25732568
}
25742569

2575-
let generics_suggestion_span = generics.span.substitute_dummy({
2570+
let generics_suggestion_span = impl_.generics.span.substitute_dummy({
25762571
let pos = snippet_opt(cx, item.span.until(target.span()))
25772572
.and_then(|snip| Some(item.span.lo() + BytePos(snip.find("impl")? as u32 + 4)));
25782573
if let Some(pos) = pos {
@@ -2583,7 +2578,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
25832578
});
25842579

25852580
let mut ctr_vis = ImplicitHasherConstructorVisitor::new(cx, target);
2586-
for item in items.iter().map(|item| cx.tcx.hir().impl_item(item.id)) {
2581+
for item in impl_.items.iter().map(|item| cx.tcx.hir().impl_item(item.id)) {
25872582
ctr_vis.visit_impl_item(item);
25882583
}
25892584

@@ -2596,7 +2591,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
25962591
target.type_name()
25972592
),
25982593
move |diag| {
2599-
suggestion(cx, diag, generics.span, generics_suggestion_span, target, ctr_vis);
2594+
suggestion(cx, diag, impl_.generics.span, generics_suggestion_span, target, ctr_vis);
26002595
},
26012596
);
26022597
}

clippy_lints/src/unnecessary_wraps.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::utils::{
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::intravisit::FnKind;
8-
use rustc_hir::{Body, ExprKind, FnDecl, HirId, ItemKind, Node};
8+
use rustc_hir::{Body, ExprKind, FnDecl, HirId, ItemKind, Impl, Node};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::subst::GenericArgKind;
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
7777
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
7878
if matches!(
7979
item.kind,
80-
ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
80+
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)
8181
) {
8282
return;
8383
}

clippy_lints/src/unused_self.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use if_chain::if_chain;
22
use rustc_hir::def::Res;
33
use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
4-
use rustc_hir::{HirId, ImplItem, ImplItemKind, ItemKind, Path};
4+
use rustc_hir::{HirId, ImplItem, ImplItemKind, ItemKind, Impl, Path};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::hir::map::Map;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -49,7 +49,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
4949
let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
5050
let assoc_item = cx.tcx.associated_item(def_id);
5151
if_chain! {
52-
if let ItemKind::Impl { of_trait: None, .. } = parent_item.kind;
52+
if let ItemKind::Impl(Impl { of_trait: None, .. }) = parent_item.kind;
5353
if assoc_item.fn_has_self_parameter;
5454
if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
5555
let body = cx.tcx.hir().body(*body_id);

0 commit comments

Comments
 (0)