diff options
| author | bors <bors@rust-lang.org> | 2022-09-27 18:14:24 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-09-27 18:14:24 +0000 |
| commit | 9aa85dc35b1692aaf907c5ca921913e2be7253f7 (patch) | |
| tree | 3cb73a0e23531ac332fff8fe6b6fb3fa6787dc9a /src | |
| parent | 257fb4b458d9fc86530899705fa14c68a5c69d07 (diff) | |
| parent | 63f441ec855f0de342ccc2b9ff90776c633ca9e6 (diff) | |
| download | rust-9aa85dc35b1692aaf907c5ca921913e2be7253f7.tar.gz rust-9aa85dc35b1692aaf907c5ca921913e2be7253f7.zip | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/docs.rs | 1 | ||||
| -rw-r--r-- | src/docs/box_default.txt | 23 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/docs.rs b/src/docs.rs index a501b56ffbe..166be0618ff 100644 --- a/src/docs.rs +++ b/src/docs.rs @@ -48,6 +48,7 @@ docs! { "borrow_interior_mutable_const", "borrowed_box", "box_collection", + "box_default", "boxed_local", "branches_sharing_code", "builtin_type_shadow", diff --git a/src/docs/box_default.txt b/src/docs/box_default.txt new file mode 100644 index 00000000000..ffac894d0c5 --- /dev/null +++ b/src/docs/box_default.txt @@ -0,0 +1,23 @@ +### What it does +checks for `Box::new(T::default())`, which is better written as +`Box::<T>::default()`. + +### Why is this bad? +First, it's more complex, involving two calls instead of one. +Second, `Box::default()` can be faster +[in certain cases](https://nnethercote.github.io/perf-book/standard-library-types.html#box). + +### Known problems +The lint may miss some cases (e.g. Box::new(String::from(""))). +On the other hand, it will trigger on cases where the `default` +code comes from a macro that does something different based on +e.g. target operating system. + +### Example +``` +let x: Box<String> = Box::new(Default::default()); +``` +Use instead: +``` +let x: Box<String> = Box::default(); +``` \ No newline at end of file |
