about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAndre Bogus <bogusandre@gmail.com>2022-09-21 23:43:49 +0200
committerAndre Bogus <bogusandre@gmail.com>2022-09-27 13:26:23 +0200
commit63f441ec855f0de342ccc2b9ff90776c633ca9e6 (patch)
treefbe233938f4ab9c9ae3ea1b19ed78e7ba31a4a8e /src
parent7248d06384c6a90de58c04c1f46be88821278d8b (diff)
add `box-default` lint
Diffstat (limited to 'src')
-rw-r--r--src/docs.rs1
-rw-r--r--src/docs/box_default.txt23
2 files changed, 24 insertions, 0 deletions
diff --git a/src/docs.rs b/src/docs.rs
index 6c89b4dde37..8ffe1281f65 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