Skip to content

Commit a7f66ba

Browse files
committed
Auto merge of rust-lang#12687 - Alexendoo:box-default-generic-fn, r=xFrednet
Don't suggest `Box::default()` in functions with differing generics Fixes rust-lang#12684 changelog: none
2 parents d8e76ec + 66362ef commit a7f66ba

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

clippy_lints/src/box_default.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ fn given_type(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
121121
if let Some(index) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
122122
&& let Some(sig) = expr_sig(cx, path)
123123
&& let Some(input) = sig.input(index)
124-
&& !cx.typeck_results().expr_ty_adjusted(expr).boxed_ty().is_trait()
124+
&& let Some(input_ty) = input.no_bound_vars()
125125
{
126-
input.no_bound_vars().is_some()
126+
input_ty == cx.typeck_results().expr_ty_adjusted(expr)
127127
} else {
128128
false
129129
}

tests/ui/box_default.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ fn main() {
6565
// Would have a suggestion after https://github.com/rust-lang/rust/blob/fdd030127cc68afec44a8d3f6341525dd34e50ae/compiler/rustc_middle/src/ty/diagnostics.rs#L554-L563
6666
let mut unnameable = Box::new(Option::default());
6767
let _ = unnameable.insert(|| {});
68+
69+
let _ = Box::into_raw(Box::new(String::default()));
6870
}
6971

7072
fn ret_ty_fn() -> Box<bool> {
@@ -75,6 +77,16 @@ fn call_ty_fn(_b: Box<u8>) {
7577
issue_9621_dyn_trait();
7678
}
7779

80+
struct X<T>(T);
81+
82+
impl<T: Default> X<T> {
83+
fn x(_: Box<T>) {}
84+
85+
fn same_generic_param() {
86+
Self::x(Box::default());
87+
}
88+
}
89+
7890
use std::io::{Read, Result};
7991

8092
impl Read for ImplementsDefault {

tests/ui/box_default.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ fn main() {
6565
// Would have a suggestion after https://github.com/rust-lang/rust/blob/fdd030127cc68afec44a8d3f6341525dd34e50ae/compiler/rustc_middle/src/ty/diagnostics.rs#L554-L563
6666
let mut unnameable = Box::new(Option::default());
6767
let _ = unnameable.insert(|| {});
68+
69+
let _ = Box::into_raw(Box::new(String::default()));
6870
}
6971

7072
fn ret_ty_fn() -> Box<bool> {
@@ -75,6 +77,16 @@ fn call_ty_fn(_b: Box<u8>) {
7577
issue_9621_dyn_trait();
7678
}
7779

80+
struct X<T>(T);
81+
82+
impl<T: Default> X<T> {
83+
fn x(_: Box<T>) {}
84+
85+
fn same_generic_param() {
86+
Self::x(Box::new(T::default()));
87+
}
88+
}
89+
7890
use std::io::{Read, Result};
7991

8092
impl Read for ImplementsDefault {

tests/ui/box_default.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ error: `Box::new(_)` of default value
5555
LL | call_ty_fn(Box::new(u8::default()));
5656
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`
5757

58-
error: aborting due to 9 previous errors
58+
error: `Box::new(_)` of default value
59+
--> tests/ui/box_default.rs:86:17
60+
|
61+
LL | Self::x(Box::new(T::default()));
62+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`
63+
64+
error: aborting due to 10 previous errors
5965

0 commit comments

Comments
 (0)