-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #9585 - rust-lang:extend-box-default, r=Alexendoo
extend `box-default` lint, add suggestion This extends the recently added `box-default` lint to also cover `Box::new(vec![])`, `Box::new(String::from(""))` and `Box::new(Vec::from([]))`. Also the lint now suggests a suitable replacement. I did not find a simple way to check whether the type is fully determined by the outside, so I at least checked for some variations to remove the turbofish in those cases. --- changelog: none
- Loading branch information
Showing
6 changed files
with
212 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// run-rustfix | ||
#![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::default(); | ||
let _byte = Box::<u8>::default(); | ||
let _vec = Box::<std::vec::Vec<u8>>::default(); | ||
let _impl = Box::<ImplementsDefault>::default(); | ||
let _impl2 = Box::<ImplementsDefault>::default(); | ||
let _impl3: Box<ImplementsDefault> = Box::default(); | ||
let _own = Box::new(OwnDefault::default()); // should not lint | ||
let _in_macro = outer!(Box::<std::string::String>::default()); | ||
let _string_default = outer!(Box::<std::string::String>::default()); | ||
let _vec2: Box<Vec<ImplementsDefault>> = Box::default(); | ||
let _vec3: Box<Vec<bool>> = Box::default(); | ||
let _vec4: Box<_> = Box::<std::vec::Vec<bool>>::default(); | ||
let _more = ret_ty_fn(); | ||
call_ty_fn(Box::default()); | ||
} | ||
|
||
fn ret_ty_fn() -> Box<bool> { | ||
Box::<bool>::default() | ||
} | ||
|
||
#[allow(clippy::boxed_local)] | ||
fn call_ty_fn(_b: Box<u8>) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,82 @@ | ||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:21:32 | ||
--> $DIR/box_default.rs:22:32 | ||
| | ||
LL | let _string: Box<String> = Box::new(Default::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()` | ||
| | ||
= help: use `Box::default()` instead | ||
= note: `-D clippy::box-default` implied by `-D warnings` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:22:17 | ||
--> $DIR/box_default.rs:23:17 | ||
| | ||
LL | let _byte = Box::new(u8::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<u8>::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:23:16 | ||
--> $DIR/box_default.rs:24:16 | ||
| | ||
LL | let _vec = Box::new(Vec::<u8>::new()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<std::vec::Vec<u8>>::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:24:17 | ||
--> $DIR/box_default.rs:25:17 | ||
| | ||
LL | let _impl = Box::new(ImplementsDefault::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<ImplementsDefault>::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:25:18 | ||
--> $DIR/box_default.rs:26:18 | ||
| | ||
LL | let _impl2 = Box::new(<ImplementsDefault as Default>::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<ImplementsDefault>::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:26:42 | ||
--> $DIR/box_default.rs:27:42 | ||
| | ||
LL | let _impl3: Box<ImplementsDefault> = Box::new(Default::default()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use `Box::default()` instead | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:28:28 | ||
--> $DIR/box_default.rs:29:28 | ||
| | ||
LL | let _in_macro = outer!(Box::new(String::new())); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<std::string::String>::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:30:34 | ||
| | ||
LL | let _string_default = outer!(Box::new(String::from(""))); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<std::string::String>::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:31:46 | ||
| | ||
LL | let _vec2: Box<Vec<ImplementsDefault>> = Box::new(vec![]); | ||
| ^^^^^^^^^^^^^^^^ help: try: `Box::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:32:33 | ||
| | ||
LL | let _vec3: Box<Vec<bool>> = Box::new(Vec::from([])); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:33:25 | ||
| | ||
LL | let _vec4: Box<_> = Box::new(Vec::from([false; 0])); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<std::vec::Vec<bool>>::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:35:16 | ||
| | ||
LL | call_ty_fn(Box::new(u8::default())); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()` | ||
|
||
error: `Box::new(_)` of default value | ||
--> $DIR/box_default.rs:39:5 | ||
| | ||
= help: use `Box::default()` instead | ||
LL | Box::new(bool::default()) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::<bool>::default()` | ||
|
||
error: aborting due to 7 previous errors | ||
error: aborting due to 13 previous errors | ||
|