Skip to content

Commit 3f5a2d9

Browse files
committed
Consider traits in lint
1 parent 5488f13 commit 3f5a2d9

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

clippy_lints/src/zero_sized_map_values.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,44 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
4646
self.check_ty(cx, ty, local.pat.span);
4747
}
4848

49-
fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, _: HirId) {
50-
for ty in decl.inputs {
51-
self.check_hir_ty(cx, ty);
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)) {
51+
if let ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
52+
return;
53+
}
5254
}
5355

54-
if let FnRetTy::Return(ty) = decl.output {
55-
self.check_hir_ty(cx, ty);
56-
}
56+
self.check_fn_decl(cx, decl);
5757
}
5858

5959
fn check_struct_field(&mut self, cx: &LateContext<'_>, field: &StructField<'_>) {
6060
self.check_hir_ty(cx, field.ty);
6161
}
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+
}
6274
}
6375

6476
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+
6587
fn check_hir_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
6688
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
6789
self.check_ty(cx, ty, hir_ty.span);

tests/ui/zero_sized_map_values.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ struct Test {
88
also_not_ok: Vec<HashMap<usize, ()>>,
99
}
1010

11+
trait TestTrait {
12+
type Output;
13+
14+
fn produce_output() -> Self::Output;
15+
16+
fn weird_map(&self, map: HashMap<usize, ()>);
17+
}
18+
19+
impl TestTrait for Test {
20+
type Output = HashMap<String, ()>;
21+
22+
fn produce_output() -> Self::Output {
23+
todo!();
24+
}
25+
26+
fn weird_map(&self, map: HashMap<usize, ()>) {
27+
todo!();
28+
}
29+
}
30+
1131
fn test(map: HashMap<String, ()>, key: &str) -> HashMap<String, ()> {
1232
todo!();
1333
}

tests/ui/zero_sized_map_values.stderr

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,44 @@ LL | also_not_ok: Vec<HashMap<usize, ()>>,
1616
= help: consider using a set instead
1717

1818
error: map with zero-sized value type
19-
--> $DIR/zero_sized_map_values.rs:11:14
19+
--> $DIR/zero_sized_map_values.rs:16:30
20+
|
21+
LL | fn weird_map(&self, map: HashMap<usize, ()>);
22+
| ^^^^^^^^^^^^^^^^^^
23+
|
24+
= help: consider using a set instead
25+
26+
error: map with zero-sized value type
27+
--> $DIR/zero_sized_map_values.rs:31:14
2028
|
2129
LL | fn test(map: HashMap<String, ()>, key: &str) -> HashMap<String, ()> {
2230
| ^^^^^^^^^^^^^^^^^^^
2331
|
2432
= help: consider using a set instead
2533

2634
error: map with zero-sized value type
27-
--> $DIR/zero_sized_map_values.rs:11:49
35+
--> $DIR/zero_sized_map_values.rs:31:49
2836
|
2937
LL | fn test(map: HashMap<String, ()>, key: &str) -> HashMap<String, ()> {
3038
| ^^^^^^^^^^^^^^^^^^^
3139
|
3240
= help: consider using a set instead
3341

3442
error: map with zero-sized value type
35-
--> $DIR/zero_sized_map_values.rs:21:9
43+
--> $DIR/zero_sized_map_values.rs:41:9
3644
|
3745
LL | let _: HashMap<String, ()> = HashMap::new();
3846
| ^
3947
|
4048
= help: consider using a set instead
4149

4250
error: map with zero-sized value type
43-
--> $DIR/zero_sized_map_values.rs:24:9
51+
--> $DIR/zero_sized_map_values.rs:44:9
4452
|
4553
LL | let _: HashMap<_, _> = std::iter::empty::<(String, ())>().collect();
4654
| ^
4755
|
4856
= help: consider using a set instead
4957

50-
error: aborting due to 6 previous errors
58+
error: aborting due to 7 previous errors
5159

0 commit comments

Comments
 (0)