about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-03-30 08:22:16 +0000
committerbors <bors@rust-lang.org>2023-03-30 08:22:16 +0000
commitef3867f3943a9180da87ac2290c49a9297198b1d (patch)
treef4fbedb14f22eebb3317ef2723c68a47809185b2 /tests
parentc5011e9d42b9bcedf0753a108018618568700a83 (diff)
parent76d13bb7fa20ba316c75023c66d74fed8e1881df (diff)
downloadrust-ef3867f3943a9180da87ac2290c49a9297198b1d.tar.gz
rust-ef3867f3943a9180da87ac2290c49a9297198b1d.zip
Auto merge of #9102 - botahamec:unused-box, r=xFrednet
Added the `[unnecessary_box_returns]` lint

fixes #5

I'm not confident in the name of this lint. Let me know if you can think of something better

---

changelog: New lint: ``[`unnecessary_box_returns`]``
[#9102](https://github.com/rust-lang/rust-clippy/pull/9102)
<!-- changelog_checked -->
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/unnecessary_box_returns.rs60
-rw-r--r--tests/ui/unnecessary_box_returns.stderr35
2 files changed, 95 insertions, 0 deletions
diff --git a/tests/ui/unnecessary_box_returns.rs b/tests/ui/unnecessary_box_returns.rs
new file mode 100644
index 00000000000..fe60d929759
--- /dev/null
+++ b/tests/ui/unnecessary_box_returns.rs
@@ -0,0 +1,60 @@
+#![warn(clippy::unnecessary_box_returns)]
+
+trait Bar {
+    // lint
+    fn baz(&self) -> Box<usize>;
+}
+
+pub struct Foo {}
+
+impl Bar for Foo {
+    // don't lint: this is a problem with the trait, not the implementation
+    fn baz(&self) -> Box<usize> {
+        Box::new(42)
+    }
+}
+
+impl Foo {
+    fn baz(&self) -> Box<usize> {
+        // lint
+        Box::new(13)
+    }
+}
+
+// lint
+fn bxed_usize() -> Box<usize> {
+    Box::new(5)
+}
+
+// lint
+fn _bxed_foo() -> Box<Foo> {
+    Box::new(Foo {})
+}
+
+// don't lint: this is exported
+pub fn bxed_foo() -> Box<Foo> {
+    Box::new(Foo {})
+}
+
+// don't lint: str is unsized
+fn bxed_str() -> Box<str> {
+    "Hello, world!".to_string().into_boxed_str()
+}
+
+// don't lint: function contains the word, "box"
+fn boxed_usize() -> Box<usize> {
+    Box::new(7)
+}
+
+// don't lint: this has an unspecified return type
+fn default() {}
+
+// don't lint: this doesn't return a Box
+fn string() -> String {
+    String::from("Hello, world")
+}
+
+fn main() {
+    // don't lint: this is a closure
+    let a = || -> Box<usize> { Box::new(5) };
+}
diff --git a/tests/ui/unnecessary_box_returns.stderr b/tests/ui/unnecessary_box_returns.stderr
new file mode 100644
index 00000000000..b17512c10a1
--- /dev/null
+++ b/tests/ui/unnecessary_box_returns.stderr
@@ -0,0 +1,35 @@
+error: boxed return of the sized type `usize`
+  --> $DIR/unnecessary_box_returns.rs:5:22
+   |
+LL |     fn baz(&self) -> Box<usize>;
+   |                      ^^^^^^^^^^ help: try: `usize`
+   |
+   = help: changing this also requires a change to the return expressions in this function
+   = note: `-D clippy::unnecessary-box-returns` implied by `-D warnings`
+
+error: boxed return of the sized type `usize`
+  --> $DIR/unnecessary_box_returns.rs:18:22
+   |
+LL |     fn baz(&self) -> Box<usize> {
+   |                      ^^^^^^^^^^ help: try: `usize`
+   |
+   = help: changing this also requires a change to the return expressions in this function
+
+error: boxed return of the sized type `usize`
+  --> $DIR/unnecessary_box_returns.rs:25:20
+   |
+LL | fn bxed_usize() -> Box<usize> {
+   |                    ^^^^^^^^^^ help: try: `usize`
+   |
+   = help: changing this also requires a change to the return expressions in this function
+
+error: boxed return of the sized type `Foo`
+  --> $DIR/unnecessary_box_returns.rs:30:19
+   |
+LL | fn _bxed_foo() -> Box<Foo> {
+   |                   ^^^^^^^^ help: try: `Foo`
+   |
+   = help: changing this also requires a change to the return expressions in this function
+
+error: aborting due to 4 previous errors
+