Skip to content

Commit 6fa0fdc

Browse files
korratebroto
andcommitted
Apply suggestions from code review
Co-authored-by: Eduardo Broto <[email protected]>
1 parent 381985e commit 6fa0fdc

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use if_chain::if_chain;
2-
use rustc_hir::{self as hir, intravisit::FnKind, *};
2+
use rustc_hir::{self as hir, *};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_middle::ty::{Adt, Ty};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
6-
use rustc_span::Span;
76
use rustc_target::abi::LayoutOf as _;
87
use rustc_typeck::hir_ty_to_ty;
98

10-
use crate::utils::{match_type, paths, span_lint_and_help};
9+
use crate::utils::{is_type_diagnostic_item, match_type, paths, span_lint_and_help};
1110

1211
declare_clippy_lint! {
1312
/// **What it does:** Checks for maps with zero-sized value types anywhere in the code.
@@ -41,41 +40,41 @@ declare_clippy_lint! {
4140
declare_lint_pass!(ZeroSizedMapValues => [ZERO_SIZED_MAP_VALUES]);
4241

4342
impl LateLintPass<'_> for ZeroSizedMapValues {
44-
fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
45-
let ty = cx.typeck_results().pat_ty(local.pat);
46-
47-
self.check_ty(cx, ty, local.pat.span);
48-
}
49-
5043
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)) {
53-
if let ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
54-
return;
55-
}
56-
}
57-
58-
// TODO this causes errors in some cases
59-
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
60-
self.check_ty(cx, ty, hir_ty.span);
61-
}
62-
}
63-
64-
impl ZeroSizedMapValues {
65-
fn check_ty<'tcx>(&mut self, cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span) {
66-
if span.from_expansion() {
67-
return;
68-
}
69-
7044
if_chain! {
71-
if match_type(cx, ty, &paths::HASHMAP) || match_type(cx, ty, &paths::BTREEMAP);
45+
if !hir_ty.span.from_expansion();
46+
if !in_trait_impl(cx, hir_ty.hir_id);
47+
let ty = ty_from_hir_ty(cx, hir_ty);
48+
if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) || match_type(cx, ty, &paths::BTREEMAP);
7249
if let Adt(_, ref substs) = ty.kind();
7350
let ty = substs.type_at(1);
7451
if let Ok(layout) = cx.layout_of(ty);
7552
if layout.is_zst();
7653
then {
77-
span_lint_and_help(cx, ZERO_SIZED_MAP_VALUES, span, "map with zero-sized value type", None, "consider using a set instead");
54+
span_lint_and_help(cx, ZERO_SIZED_MAP_VALUES, hir_ty.span, "map with zero-sized value type", None, "consider using a set instead");
7855
}
7956
}
8057
}
8158
}
59+
60+
fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
61+
let parent_id = cx.tcx.hir().get_parent_item(hir_id);
62+
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(parent_id)) {
63+
if let ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
64+
return true;
65+
}
66+
}
67+
false
68+
}
69+
70+
fn ty_from_hir_ty<'tcx>(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
71+
cx.maybe_typeck_results()
72+
.and_then(|results| {
73+
if results.hir_owner == hir_ty.hir_id.owner {
74+
results.node_type_opt(hir_ty.hir_id)
75+
} else {
76+
None
77+
}
78+
})
79+
.unwrap_or_else(|| hir_ty_to_ty(cx.tcx, hir_ty))
80+
}

tests/ui/zero_sized_map_values.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ LL | fn test(map: HashMap<String, ()>, key: &str) -> HashMap<String, ()> {
8080
= help: consider using a set instead
8181

8282
error: map with zero-sized value type
83-
--> $DIR/zero_sized_map_values.rs:65:9
83+
--> $DIR/zero_sized_map_values.rs:65:34
8484
|
8585
LL | let _: HashMap<String, ()> = HashMap::new();
86-
| ^
86+
| ^^^^^^^^^^^^
8787
|
8888
= help: consider using a set instead
8989

@@ -96,10 +96,10 @@ LL | let _: HashMap<String, ()> = HashMap::new();
9696
= help: consider using a set instead
9797

9898
error: map with zero-sized value type
99-
--> $DIR/zero_sized_map_values.rs:68:9
99+
--> $DIR/zero_sized_map_values.rs:68:12
100100
|
101101
LL | let _: HashMap<_, _> = std::iter::empty::<(String, ())>().collect();
102-
| ^
102+
| ^^^^^^^^^^^^^
103103
|
104104
= help: consider using a set instead
105105

0 commit comments

Comments
 (0)