Skip to content

Commit 574c8ae

Browse files
committed
Auto merge of rust-lang#10382 - samueltardieu:issue-10381, r=llogiq
Box::default(): do not omit the type of the removed trait object Within a larger expression, when the type of `Box::new(T::default())` is `Box<dyn Trait>`, the concrete type `T` cannot be omitted in the proposed replacement `Box::<T>::default()`. Fixes rust-lang#10381 changelog: [`box_default`]: in case of a trait object do not omit the concrete type name
2 parents 85d4b5a + 92c403c commit 574c8ae

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

clippy_lints/src/box_default.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ fn given_type(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
117117
) => {
118118
if let Some(index) = args.iter().position(|arg| arg.hir_id == expr.hir_id) &&
119119
let Some(sig) = expr_sig(cx, path) &&
120-
let Some(input) = sig.input(index)
120+
let Some(input) = sig.input(index) &&
121+
!cx.typeck_results().expr_ty_adjusted(expr).boxed_ty().is_trait()
121122
{
122123
input.no_bound_vars().is_some()
123124
} else {

tests/ui/box_default.fixed

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn main() {
3333
let _vec4: Box<_> = Box::<Vec<bool>>::default();
3434
let _more = ret_ty_fn();
3535
call_ty_fn(Box::default());
36+
issue_10381();
3637
}
3738

3839
fn ret_ty_fn() -> Box<bool> {
@@ -65,3 +66,20 @@ fn issue_10089() {
6566
let _ = Box::<WeirdPathed>::default();
6667
};
6768
}
69+
70+
fn issue_10381() {
71+
#[derive(Default)]
72+
pub struct Foo {}
73+
pub trait Bar {}
74+
impl Bar for Foo {}
75+
76+
fn maybe_get_bar(i: u32) -> Option<Box<dyn Bar>> {
77+
if i % 2 == 0 {
78+
Some(Box::<Foo>::default())
79+
} else {
80+
None
81+
}
82+
}
83+
84+
assert!(maybe_get_bar(2).is_some());
85+
}

tests/ui/box_default.rs

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn main() {
3333
let _vec4: Box<_> = Box::new(Vec::from([false; 0]));
3434
let _more = ret_ty_fn();
3535
call_ty_fn(Box::new(u8::default()));
36+
issue_10381();
3637
}
3738

3839
fn ret_ty_fn() -> Box<bool> {
@@ -65,3 +66,20 @@ fn issue_10089() {
6566
let _ = Box::new(WeirdPathed::default());
6667
};
6768
}
69+
70+
fn issue_10381() {
71+
#[derive(Default)]
72+
pub struct Foo {}
73+
pub trait Bar {}
74+
impl Bar for Foo {}
75+
76+
fn maybe_get_bar(i: u32) -> Option<Box<dyn Bar>> {
77+
if i % 2 == 0 {
78+
Some(Box::new(Foo::default()))
79+
} else {
80+
None
81+
}
82+
}
83+
84+
assert!(maybe_get_bar(2).is_some());
85+
}

tests/ui/box_default.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,28 @@ LL | call_ty_fn(Box::new(u8::default()));
7373
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`
7474

7575
error: `Box::new(_)` of default value
76-
--> $DIR/box_default.rs:39:5
76+
--> $DIR/box_default.rs:40:5
7777
|
7878
LL | Box::new(bool::default())
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<bool>::default()`
8080

8181
error: `Box::new(_)` of default value
82-
--> $DIR/box_default.rs:56:28
82+
--> $DIR/box_default.rs:57:28
8383
|
8484
LL | let _: Box<dyn Read> = Box::new(ImplementsDefault::default());
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<ImplementsDefault>::default()`
8686

8787
error: `Box::new(_)` of default value
88-
--> $DIR/box_default.rs:65:17
88+
--> $DIR/box_default.rs:66:17
8989
|
9090
LL | let _ = Box::new(WeirdPathed::default());
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<WeirdPathed>::default()`
9292

93-
error: aborting due to 15 previous errors
93+
error: `Box::new(_)` of default value
94+
--> $DIR/box_default.rs:78:18
95+
|
96+
LL | Some(Box::new(Foo::default()))
97+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<Foo>::default()`
98+
99+
error: aborting due to 16 previous errors
94100

0 commit comments

Comments
 (0)