Skip to content

Commit 3a91a11

Browse files
committed
add logic to check allocator matching
1 parent 73b9841 commit 3a91a11

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

clippy_lints/src/types/vec_box.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,32 @@ pub(super) fn check(
2323
if let Some(last) = last_path_segment(qpath).args
2424
// Get the _ part of Vec<_>
2525
&& let Some(GenericArg::Type(ty)) = last.args.first()
26+
// extract allocator from the Vec for later
27+
&& let vec_alloc_ty = last.args.get(1)
2628
// ty is now _ at this point
2729
&& let TyKind::Path(ref ty_qpath) = ty.kind
2830
&& let res = cx.qpath_res(ty_qpath, ty.hir_id)
2931
&& let Some(def_id) = res.opt_def_id()
3032
&& Some(def_id) == cx.tcx.lang_items().owned_box()
3133
// At this point, we know ty is Box<T>, now get T
3234
&& let Some(last) = last_path_segment(ty_qpath).args
35+
// extract allocator from thr Box for later
3336
&& let Some(GenericArg::Type(boxed_ty)) = last.args.first()
37+
&& let boxed_alloc_ty = last.args.get(1)
3438
&& let ty_ty = hir_ty_to_ty(cx.tcx, boxed_ty)
3539
&& !ty_ty.has_escaping_bound_vars()
3640
&& ty_ty.is_sized(cx.tcx, cx.param_env)
3741
&& let Ok(ty_ty_size) = cx.layout_of(ty_ty).map(|l| l.size.bytes())
3842
&& ty_ty_size < box_size_threshold
43+
// https://github.com/rust-lang/rust-clippy/issues/7114
44+
// this code also does not consider that Global can be explicitly
45+
// defined (yet) as in Vec<_, Global> so a very slight false negative edge case
46+
&& match (vec_alloc_ty, boxed_alloc_ty) {
47+
(None, None) => true,
48+
(Some(GenericArg::Type(l)), Some(GenericArg::Type(r))) =>
49+
hir_ty_to_ty(cx.tcx, l) == hir_ty_to_ty(cx.tcx, r),
50+
_ => false
51+
}
3952
{
4053
span_lint_and_sugg(
4154
cx,
@@ -53,4 +66,4 @@ pub(super) fn check(
5366
} else {
5467
false
5568
}
56-
}
69+
}

0 commit comments

Comments
 (0)