Skip to content

Commit 3320a70

Browse files
committed
Move most of implementation into check_ty
1 parent e5d8eb2 commit 3320a70

File tree

1 file changed

+18
-89
lines changed

1 file changed

+18
-89
lines changed

clippy_lints/src/zero_sized_map_values.rs

Lines changed: 18 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ declare_clippy_lint! {
1616
/// containing zero sized values is effectively a set. Using a set in that case improves
1717
/// readability and communicates intent more clearly.
1818
///
19-
/// **Known problems:** A zero-sized type cannot be recovered later if it contains private
20-
/// fields.
19+
/// **Known problems:**
20+
/// * A zero-sized type cannot be recovered later if it contains private fields.
21+
/// * This lints the signature of public items
2122
///
2223
/// **Example:**
2324
///
@@ -33,7 +34,7 @@ declare_clippy_lint! {
3334
/// }
3435
/// ```
3536
pub ZERO_SIZED_MAP_VALUES,
36-
nursery,
37+
pedantic,
3738
"usage of map with zero-sized value type"
3839
}
3940

@@ -46,107 +47,35 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
4647
self.check_ty(cx, ty, local.pat.span);
4748
}
4849

49-
fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) {
50-
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(id)) {
50+
fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
51+
let parent_id = cx.tcx.hir().get_parent_item(hir_ty.hir_id);
52+
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(parent_id)) {
5153
if let ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
5254
return;
5355
}
5456
}
5557

56-
self.check_fn_decl(cx, decl);
57-
}
58-
59-
fn check_struct_field(&mut self, cx: &LateContext<'_>, field: &StructField<'_>) {
60-
self.check_hir_ty(cx, field.ty);
61-
}
62-
63-
fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) {
64-
match item.kind {
65-
TraitItemKind::Const(ref ty, _) | TraitItemKind::Type(_, Some(ref ty)) => {
66-
self.check_hir_ty(cx, ty);
67-
},
68-
TraitItemKind::Fn(ref sig, _) => {
69-
self.check_fn_decl(cx, &sig.decl);
70-
},
71-
_ => (),
72-
}
73-
}
74-
}
75-
76-
impl ZeroSizedMapValues {
77-
fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>) {
78-
for ty in decl.inputs {
79-
self.check_hir_ty(cx, ty);
80-
}
81-
82-
if let FnRetTy::Return(ty) = decl.output {
83-
self.check_hir_ty(cx, ty);
84-
}
85-
}
86-
87-
fn check_hir_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
58+
// TODO this causes errors in some cases
8859
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
8960
self.check_ty(cx, ty, hir_ty.span);
90-
91-
match hir_ty.kind {
92-
TyKind::Path(ref qpath) => match *qpath {
93-
QPath::Resolved(ty, ref p) => {
94-
if let Some(ty) = ty {
95-
self.check_hir_ty(cx, ty);
96-
}
97-
98-
for ty in p.segments.iter().flat_map(|seg| {
99-
seg.args
100-
.as_ref()
101-
.map_or_else(|| [].iter(), |params| params.args.iter())
102-
.filter_map(|arg| match arg {
103-
GenericArg::Type(ty) => Some(ty),
104-
_ => None,
105-
})
106-
}) {
107-
self.check_hir_ty(cx, ty);
108-
}
109-
},
110-
QPath::TypeRelative(ty, ref seg) => {
111-
self.check_hir_ty(cx, ty);
112-
113-
if let Some(ref params) = seg.args {
114-
for ty in params.args.iter().filter_map(|arg| match arg {
115-
GenericArg::Type(ty) => Some(ty),
116-
_ => None,
117-
}) {
118-
self.check_hir_ty(cx, ty);
119-
}
120-
}
121-
},
122-
QPath::LangItem(..) => {},
123-
},
124-
TyKind::Slice(ref ty) | TyKind::Array(ref ty, _) | TyKind::Ptr(MutTy { ref ty, .. }) => {
125-
self.check_hir_ty(cx, ty);
126-
},
127-
TyKind::Tup(tys) => {
128-
for ty in tys {
129-
self.check_hir_ty(cx, ty);
130-
}
131-
},
132-
_ => {},
133-
}
13461
}
62+
}
13563

64+
impl ZeroSizedMapValues {
13665
fn check_ty<'tcx>(&mut self, cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span) {
13766
if span.from_expansion() {
13867
return;
13968
}
14069

14170
if_chain! {
142-
if match_type(cx, ty, &paths::HASHMAP) || match_type(cx, ty, &paths::BTREEMAP);
143-
if let Adt(_, ref substs) = ty.kind();
144-
let ty = substs.type_at(1);
145-
if let Ok(layout) = cx.layout_of(ty);
146-
if layout.is_zst();
147-
then {
148-
span_lint_and_help(cx, ZERO_SIZED_MAP_VALUES, span, "map with zero-sized value type", None, "consider using a set instead");
149-
}
71+
if match_type(cx, ty, &paths::HASHMAP) || match_type(cx, ty, &paths::BTREEMAP);
72+
if let Adt(_, ref substs) = ty.kind();
73+
let ty = substs.type_at(1);
74+
if let Ok(layout) = cx.layout_of(ty);
75+
if layout.is_zst();
76+
then {
77+
span_lint_and_help(cx, ZERO_SIZED_MAP_VALUES, span, "map with zero-sized value type", None, "consider using a set instead");
78+
}
15079
}
15180
}
15281
}

0 commit comments

Comments
 (0)