Auto merge of #9511 - rust-lang:box-default, r=Alexendoo
add `box-default` lint This adds a `box-default` lint to suggest using `Box::default()` instead of `Box::new(Default::default())`, which offers less moving parts and potentially better performance according to [the perf book](https://nnethercote.github.io/perf-book/standard-library-types.html#box). --- changelog: add [`box_default`] lint
This commit is contained in:
commit
9aa85dc35b
11 changed files with 197 additions and 16 deletions
|
|
@ -15,7 +15,7 @@ macro_rules! boxit {
|
|||
}
|
||||
|
||||
fn test_macro() {
|
||||
boxit!(Vec::new(), Vec<u8>);
|
||||
boxit!(vec![1], Vec<u8>);
|
||||
}
|
||||
|
||||
fn test1(foo: Box<Vec<bool>>) {}
|
||||
|
|
@ -50,7 +50,7 @@ fn test_local_not_linted() {
|
|||
pub fn pub_test(foo: Box<Vec<bool>>) {}
|
||||
|
||||
pub fn pub_test_ret() -> Box<Vec<bool>> {
|
||||
Box::new(Vec::new())
|
||||
Box::default()
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
31
tests/ui/box_default.rs
Normal file
31
tests/ui/box_default.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#![warn(clippy::box_default)]
|
||||
|
||||
#[derive(Default)]
|
||||
struct ImplementsDefault;
|
||||
|
||||
struct OwnDefault;
|
||||
|
||||
impl OwnDefault {
|
||||
fn default() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! outer {
|
||||
($e: expr) => {
|
||||
$e
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _string: Box<String> = Box::new(Default::default());
|
||||
let _byte = Box::new(u8::default());
|
||||
let _vec = Box::new(Vec::<u8>::new());
|
||||
let _impl = Box::new(ImplementsDefault::default());
|
||||
let _impl2 = Box::new(<ImplementsDefault as Default>::default());
|
||||
let _impl3: Box<ImplementsDefault> = Box::new(Default::default());
|
||||
let _own = Box::new(OwnDefault::default()); // should not lint
|
||||
let _in_macro = outer!(Box::new(String::new()));
|
||||
// false negative: default is from different expansion
|
||||
let _vec2: Box<Vec<ImplementsDefault>> = Box::new(vec![]);
|
||||
}
|
||||
59
tests/ui/box_default.stderr
Normal file
59
tests/ui/box_default.stderr
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
error: `Box::new(_)` of default value
|
||||
--> $DIR/box_default.rs:21:32
|
||||
|
|
||||
LL | let _string: Box<String> = Box::new(Default::default());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::box-default` implied by `-D warnings`
|
||||
= help: use `Box::default()` instead
|
||||
|
||||
error: `Box::new(_)` of default value
|
||||
--> $DIR/box_default.rs:22:17
|
||||
|
|
||||
LL | let _byte = Box::new(u8::default());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use `Box::default()` instead
|
||||
|
||||
error: `Box::new(_)` of default value
|
||||
--> $DIR/box_default.rs:23:16
|
||||
|
|
||||
LL | let _vec = Box::new(Vec::<u8>::new());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use `Box::default()` instead
|
||||
|
||||
error: `Box::new(_)` of default value
|
||||
--> $DIR/box_default.rs:24:17
|
||||
|
|
||||
LL | let _impl = Box::new(ImplementsDefault::default());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use `Box::default()` instead
|
||||
|
||||
error: `Box::new(_)` of default value
|
||||
--> $DIR/box_default.rs:25:18
|
||||
|
|
||||
LL | let _impl2 = Box::new(<ImplementsDefault as Default>::default());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use `Box::default()` instead
|
||||
|
||||
error: `Box::new(_)` of default value
|
||||
--> $DIR/box_default.rs:26:42
|
||||
|
|
||||
LL | let _impl3: Box<ImplementsDefault> = Box::new(Default::default());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use `Box::default()` instead
|
||||
|
||||
error: `Box::new(_)` of default value
|
||||
--> $DIR/box_default.rs:28:28
|
||||
|
|
||||
LL | let _in_macro = outer!(Box::new(String::new()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use `Box::default()` instead
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue