RS.CLIPPY.UNNECESSARY_BOX_RETURNS
Needlessly returning a Box
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: unnecessary_box_returns. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for a return type containing a Box<T> where T implements Sized
The lint ignores Box<T> where T is larger than unnecessary_box_size,
as returning a large T directly may be detrimental to performance.
Why is this bad?
It's better to just return T in these cases. The caller may not need
the value to be boxed, and it's expensive to free the memory once the
Box<T> been dropped.
Example
fn foo() -> Box<String> {
Box::new(String::from("Hello, world!"))
}
Use instead:
fn foo() -> String {
String::from("Hello, world!")
}
Configuration
-
avoid-breaking-exported-api: Suppress lints whenever the suggested change would cause breakage for other crates.(default:
true) -
unnecessary-box-size: The byte size aTinBox<T>can have, below which it triggers theclippy::unnecessary_boxlint(default:
128)