Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clippy_lints/src/zero_sized_map_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
&& (is_type_diagnostic_item(cx, ty, sym::HashMap) || is_type_diagnostic_item(cx, ty, sym::BTreeMap))
&& let ty::Adt(_, args) = ty.kind()
&& let ty = args.type_at(1)
// 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
&& !ty.has_escaping_bound_vars()
// Ensure that no type information is missing, to avoid a delayed bug in the compiler if this is not the case.
// This might happen when computing a reference/pointer metadata on a type for which we
// cannot check if it is `Sized` or not, such as an incomplete associated type in a
// type alias. See an example in `issue14822()` of `tests/ui/zero_sized_hashmap_values.rs`.
&& !ty.has_non_region_param()
&& let Ok(layout) = cx.layout_of(ty)
&& layout.is_zst()
{
Expand Down
1 change: 1 addition & 0 deletions tests/ui/crashes/ice-6840.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! This is a reproducer for the ICE 6840: https://github.com/rust-lang/rust-clippy/issues/6840.
//! The ICE is caused by `TyCtxt::layout_of` and `is_normalizable` not being strict enough
#![allow(dead_code)]
#![deny(clippy::zero_sized_map_values)] // For ICE 14822
use std::collections::HashMap;

pub trait Rule {
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/zero_sized_hashmap_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ fn test2(map: HashMap<String, usize>, key: &str) -> HashMap<String, usize> {
todo!();
}

fn issue14822() {
trait Trait {
type T;
}
struct S<T: Trait>(T::T);

// The `delay_bug` happens when evaluating the pointer metadata of `S<T>` which depends on
// whether `T::T` is `Sized`. Since the type alias doesn't have a trait bound of `T: Trait`
// evaluating `T::T: Sized` ultimately fails with `NoSolution`.
type A<T> = HashMap<u32, *const S<T>>;
type B<T> = HashMap<u32, S<T>>;

enum E {}
impl Trait for E {
type T = ();
}
type C = HashMap<u32, *const S<E>>;
type D = HashMap<u32, S<E>>;
//~^ zero_sized_map_values
}

fn main() {
let _: HashMap<String, ()> = HashMap::new();
//~^ zero_sized_map_values
Expand Down
16 changes: 12 additions & 4 deletions tests/ui/zero_sized_hashmap_values.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,36 @@ LL | fn test(map: HashMap<String, ()>, key: &str) -> HashMap<String, ()> {
= help: consider using a set instead

error: map with zero-sized value type
--> tests/ui/zero_sized_hashmap_values.rs:75:34
--> tests/ui/zero_sized_hashmap_values.rs:91:14
|
LL | type D = HashMap<u32, S<E>>;
| ^^^^^^^^^^^^^^^^^^
|
= help: consider using a set instead

error: map with zero-sized value type
--> tests/ui/zero_sized_hashmap_values.rs:96:34
|
LL | let _: HashMap<String, ()> = HashMap::new();
| ^^^^^^^
|
= help: consider using a set instead

error: map with zero-sized value type
--> tests/ui/zero_sized_hashmap_values.rs:75:12
--> tests/ui/zero_sized_hashmap_values.rs:96:12
|
LL | let _: HashMap<String, ()> = HashMap::new();
| ^^^^^^^^^^^^^^^^^^^
|
= help: consider using a set instead

error: map with zero-sized value type
--> tests/ui/zero_sized_hashmap_values.rs:81:12
--> tests/ui/zero_sized_hashmap_values.rs:102:12
|
LL | let _: HashMap<_, _> = std::iter::empty::<(String, ())>().collect();
| ^^^^^^^^^^^^^
|
= help: consider using a set instead

error: aborting due to 13 previous errors
error: aborting due to 14 previous errors