Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check if ty has_escaping_bound_vars in zero_sized_map_values lint #7470

Merged
merged 1 commit into from
Jul 19, 2021
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
5 changes: 4 additions & 1 deletion clippy_lints/src/zero_sized_map_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clippy_utils::ty::{is_normalizable, is_type_diagnostic_item, match_type};
use if_chain::if_chain;
use rustc_hir::{self as hir, HirId, ItemKind, Node};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{Adt, Ty};
use rustc_middle::ty::{Adt, Ty, TypeFoldable};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;
use rustc_target::abi::LayoutOf as _;
Expand Down Expand Up @@ -52,6 +52,9 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || match_type(cx, ty, &paths::BTREEMAP);
if let Adt(_, substs) = ty.kind();
let ty = substs.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
if !ty.has_escaping_bound_vars();
// Do this to prevent `layout_of` crashing, being unable to fully normalize `ty`.
if is_normalizable(cx, cx.param_env, ty);
if let Ok(layout) = cx.layout_of(ty);
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/issue-7447.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::{borrow::Cow, collections::BTreeMap, marker::PhantomData, sync::Arc};

fn byte_view<'a>(s: &'a ByteView<'_>) -> BTreeMap<&'a str, ByteView<'a>> {
panic!()
}

fn group_entries(s: &()) -> BTreeMap<Cow<'_, str>, Vec<Cow<'_, str>>> {
todo!()
}

struct Mmap;

enum ByteViewBacking<'a> {
Buf(Cow<'a, [u8]>),
Mmap(Mmap),
}

pub struct ByteView<'a> {
backing: Arc<ByteViewBacking<'a>>,
}

fn main() {
byte_view(panic!());
group_entries(panic!());
}