Skip to content

Commit 2c56b4f

Browse files
committed
Auto merge of rust-lang#11875 - samueltardieu:fix-11868, r=Jarcho
Fix `box_default` behaviour with empty `vec![]` coming from macro arg Fix rust-lang#11868 changelog: [`box_default`]: do not warn on `Box::new(vec![])` if the `vec![]` comes from a macro argument
2 parents 7af811b + 23d5332 commit 2c56b4f

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

clippy_lints/src/box_default.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl LateLintPass<'_> for BoxDefault {
4545
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_new.kind
4646
&& let ExprKind::Call(arg_path, ..) = arg.kind
4747
&& !in_external_macro(cx.sess(), expr.span)
48-
&& (expr.span.eq_ctxt(arg.span) || is_vec_expn(cx, arg))
48+
&& (expr.span.eq_ctxt(arg.span) || is_local_vec_expn(cx, arg, expr))
4949
&& seg.ident.name == sym::new
5050
&& path_def_id(cx, ty).map_or(false, |id| Some(id) == cx.tcx.lang_items().owned_box())
5151
&& is_default_equivalent(cx, arg)
@@ -81,10 +81,10 @@ fn is_plain_default(cx: &LateContext<'_>, arg_path: &Expr<'_>) -> bool {
8181
}
8282
}
8383

84-
fn is_vec_expn(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
85-
macro_backtrace(expr.span)
86-
.next()
87-
.map_or(false, |call| cx.tcx.is_diagnostic_item(sym::vec_macro, call.def_id))
84+
fn is_local_vec_expn(cx: &LateContext<'_>, expr: &Expr<'_>, ref_expr: &Expr<'_>) -> bool {
85+
macro_backtrace(expr.span).next().map_or(false, |call| {
86+
cx.tcx.is_diagnostic_item(sym::vec_macro, call.def_id) && call.span.eq_ctxt(ref_expr.span)
87+
})
8888
}
8989

9090
#[derive(Default)]

tests/ui/box_default.fixed

+14
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,17 @@ fn issue_10381() {
9090

9191
assert!(maybe_get_bar(2).is_some());
9292
}
93+
94+
#[allow(unused)]
95+
fn issue_11868() {
96+
fn foo(_: &mut Vec<usize>) {}
97+
98+
macro_rules! bar {
99+
($baz:expr) => {
100+
Box::leak(Box::new($baz))
101+
};
102+
}
103+
104+
foo(bar!(vec![]));
105+
foo(bar!(vec![1]));
106+
}

tests/ui/box_default.rs

+14
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,17 @@ fn issue_10381() {
9090

9191
assert!(maybe_get_bar(2).is_some());
9292
}
93+
94+
#[allow(unused)]
95+
fn issue_11868() {
96+
fn foo(_: &mut Vec<usize>) {}
97+
98+
macro_rules! bar {
99+
($baz:expr) => {
100+
Box::leak(Box::new($baz))
101+
};
102+
}
103+
104+
foo(bar!(vec![]));
105+
foo(bar!(vec![1]));
106+
}

0 commit comments

Comments
 (0)