2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-07-29 05:16:06 -05:00
|
|
|
use clippy_utils::ty::{is_normalizable, is_type_diagnostic_item};
|
2020-12-20 10:19:49 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_hir::{self as hir, HirId, ItemKind, Node};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-08-30 09:38:27 -05:00
|
|
|
use rustc_middle::ty::layout::LayoutOf as _;
|
2021-07-19 04:52:05 -05:00
|
|
|
use rustc_middle::ty::{Adt, Ty, TypeFoldable};
|
2020-12-20 10:19:49 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_span::sym;
|
2020-12-20 10:19:49 -06:00
|
|
|
use rustc_typeck::hir_ty_to_ty;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for maps with zero-sized value types anywhere in the code.
|
2020-12-20 10:19:49 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Since there is only a single value for a zero-sized type, a map
|
2020-12-20 10:19:49 -06:00
|
|
|
/// containing zero sized values is effectively a set. Using a set in that case improves
|
|
|
|
/// readability and communicates intent more clearly.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
2020-12-20 10:19:49 -06:00
|
|
|
/// * A zero-sized type cannot be recovered later if it contains private fields.
|
|
|
|
/// * This lints the signature of public items
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2020-12-20 10:19:49 -06:00
|
|
|
/// ```rust
|
|
|
|
/// # use std::collections::HashMap;
|
|
|
|
/// fn unique_words(text: &str) -> HashMap<&str, ()> {
|
|
|
|
/// todo!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// # use std::collections::HashSet;
|
|
|
|
/// fn unique_words(text: &str) -> HashSet<&str> {
|
|
|
|
/// todo!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.50.0"]
|
2020-12-20 10:19:49 -06:00
|
|
|
pub ZERO_SIZED_MAP_VALUES,
|
|
|
|
pedantic,
|
|
|
|
"usage of map with zero-sized value type"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(ZeroSizedMapValues => [ZERO_SIZED_MAP_VALUES]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for ZeroSizedMapValues {
|
|
|
|
fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
if !hir_ty.span.from_expansion();
|
|
|
|
if !in_trait_impl(cx, hir_ty.hir_id);
|
|
|
|
let ty = ty_from_hir_ty(cx, hir_ty);
|
2021-10-02 18:51:01 -05:00
|
|
|
if is_type_diagnostic_item(cx, ty, sym::HashMap) || is_type_diagnostic_item(cx, ty, sym::BTreeMap);
|
2021-04-08 10:50:13 -05:00
|
|
|
if let Adt(_, substs) = ty.kind();
|
2020-12-20 10:19:49 -06:00
|
|
|
let ty = substs.type_at(1);
|
2021-07-19 04:52:05 -05:00
|
|
|
// Fixes https://github.com/rust-lang/rust-clippy/issues/7447 because of
|
|
|
|
// https://github.com/rust-lang/rust/blob/master/compiler/rustc_middle/src/ty/sty.rs#L968
|
|
|
|
if !ty.has_escaping_bound_vars();
|
2021-01-30 11:06:34 -06:00
|
|
|
// Do this to prevent `layout_of` crashing, being unable to fully normalize `ty`.
|
|
|
|
if is_normalizable(cx, cx.param_env, ty);
|
2020-12-20 10:19:49 -06:00
|
|
|
if let Ok(layout) = cx.layout_of(ty);
|
|
|
|
if layout.is_zst();
|
|
|
|
then {
|
|
|
|
span_lint_and_help(cx, ZERO_SIZED_MAP_VALUES, hir_ty.span, "map with zero-sized value type", None, "consider using a set instead");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
|
|
|
|
let parent_id = cx.tcx.hir().get_parent_item(hir_id);
|
|
|
|
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(parent_id)) {
|
2020-11-22 16:46:21 -06:00
|
|
|
if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
|
2020-12-20 10:19:49 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_from_hir_ty<'tcx>(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
|
|
|
|
cx.maybe_typeck_results()
|
|
|
|
.and_then(|results| {
|
|
|
|
if results.hir_owner == hir_ty.hir_id.owner {
|
|
|
|
results.node_type_opt(hir_ty.hir_id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| hir_ty_to_ty(cx.tcx, hir_ty))
|
|
|
|
}
|