Skip to content

Commit 4f142aa

Browse files
committed
Auto merge of rust-lang#9622 - llogiq:box-dyn-default, r=Alexendoo
fix `box-default` ignoring trait objects' types This avoids removing the turbofish when the `Box` type is a `dyn` or `impl _`. This fixes rust-lang#9621. --- changelog: none
2 parents 28cd1ec + bd61fdb commit 4f142aa

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

clippy_lints/src/box_default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct InferVisitor(bool);
8888

8989
impl<'tcx> Visitor<'tcx> for InferVisitor {
9090
fn visit_ty(&mut self, t: &rustc_hir::Ty<'_>) {
91-
self.0 |= matches!(t.kind, TyKind::Infer);
91+
self.0 |= matches!(t.kind, TyKind::Infer | TyKind::OpaqueDef(..) | TyKind::TraitObject(..));
9292
if !self.0 {
9393
walk_ty(self, t);
9494
}

tests/ui/box_default.fixed

+15-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,18 @@ fn ret_ty_fn() -> Box<bool> {
4040
}
4141

4242
#[allow(clippy::boxed_local)]
43-
fn call_ty_fn(_b: Box<u8>) {}
43+
fn call_ty_fn(_b: Box<u8>) {
44+
issue_9621_dyn_trait();
45+
}
46+
47+
use std::io::{Read, Result};
48+
49+
impl Read for ImplementsDefault {
50+
fn read(&mut self, _: &mut [u8]) -> Result<usize> {
51+
Ok(0)
52+
}
53+
}
54+
55+
fn issue_9621_dyn_trait() {
56+
let _: Box<dyn Read> = Box::<ImplementsDefault>::default();
57+
}

tests/ui/box_default.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,18 @@ fn ret_ty_fn() -> Box<bool> {
4040
}
4141

4242
#[allow(clippy::boxed_local)]
43-
fn call_ty_fn(_b: Box<u8>) {}
43+
fn call_ty_fn(_b: Box<u8>) {
44+
issue_9621_dyn_trait();
45+
}
46+
47+
use std::io::{Read, Result};
48+
49+
impl Read for ImplementsDefault {
50+
fn read(&mut self, _: &mut [u8]) -> Result<usize> {
51+
Ok(0)
52+
}
53+
}
54+
55+
fn issue_9621_dyn_trait() {
56+
let _: Box<dyn Read> = Box::new(ImplementsDefault::default());
57+
}

tests/ui/box_default.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,11 @@ error: `Box::new(_)` of default value
7878
LL | Box::new(bool::default())
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<bool>::default()`
8080

81-
error: aborting due to 13 previous errors
81+
error: `Box::new(_)` of default value
82+
--> $DIR/box_default.rs:56:28
83+
|
84+
LL | let _: Box<dyn Read> = Box::new(ImplementsDefault::default());
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<ImplementsDefault>::default()`
86+
87+
error: aborting due to 14 previous errors
8288

0 commit comments

Comments
 (0)