]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/zero_sized_map_values.rs
Auto merge of #6915 - smoelius:docs-link, r=llogiq
[rust.git] / clippy_lints / src / zero_sized_map_values.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::{is_normalizable, is_type_diagnostic_item, match_type};
3 use if_chain::if_chain;
4 use rustc_hir::{self as hir, HirId, ItemKind, Node};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::ty::{Adt, Ty};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::sym;
9 use rustc_target::abi::LayoutOf as _;
10 use rustc_typeck::hir_ty_to_ty;
11
12 use crate::utils::paths;
13
14 declare_clippy_lint! {
15     /// **What it does:** Checks for maps with zero-sized value types anywhere in the code.
16     ///
17     /// **Why is this bad?** Since there is only a single value for a zero-sized type, a map
18     /// containing zero sized values is effectively a set. Using a set in that case improves
19     /// readability and communicates intent more clearly.
20     ///
21     /// **Known problems:**
22     /// * A zero-sized type cannot be recovered later if it contains private fields.
23     /// * This lints the signature of public items
24     ///
25     /// **Example:**
26     ///
27     /// ```rust
28     /// # use std::collections::HashMap;
29     /// fn unique_words(text: &str) -> HashMap<&str, ()> {
30     ///     todo!();
31     /// }
32     /// ```
33     /// Use instead:
34     /// ```rust
35     /// # use std::collections::HashSet;
36     /// fn unique_words(text: &str) -> HashSet<&str> {
37     ///     todo!();
38     /// }
39     /// ```
40     pub ZERO_SIZED_MAP_VALUES,
41     pedantic,
42     "usage of map with zero-sized value type"
43 }
44
45 declare_lint_pass!(ZeroSizedMapValues => [ZERO_SIZED_MAP_VALUES]);
46
47 impl LateLintPass<'_> for ZeroSizedMapValues {
48     fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
49         if_chain! {
50             if !hir_ty.span.from_expansion();
51             if !in_trait_impl(cx, hir_ty.hir_id);
52             let ty = ty_from_hir_ty(cx, hir_ty);
53             if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || match_type(cx, ty, &paths::BTREEMAP);
54             if let Adt(_, ref substs) = ty.kind();
55             let ty = substs.type_at(1);
56             // Do this to prevent `layout_of` crashing, being unable to fully normalize `ty`.
57             if is_normalizable(cx, cx.param_env, ty);
58             if let Ok(layout) = cx.layout_of(ty);
59             if layout.is_zst();
60             then {
61                 span_lint_and_help(cx, ZERO_SIZED_MAP_VALUES, hir_ty.span, "map with zero-sized value type", None, "consider using a set instead");
62             }
63         }
64     }
65 }
66
67 fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
68     let parent_id = cx.tcx.hir().get_parent_item(hir_id);
69     if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(parent_id)) {
70         if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
71             return true;
72         }
73     }
74     false
75 }
76
77 fn ty_from_hir_ty<'tcx>(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
78     cx.maybe_typeck_results()
79         .and_then(|results| {
80             if results.hir_owner == hir_ty.hir_id.owner {
81                 results.node_type_opt(hir_ty.hir_id)
82             } else {
83                 None
84             }
85         })
86         .unwrap_or_else(|| hir_ty_to_ty(cx.tcx, hir_ty))
87 }