diff --git a/clippy_lints/src/unnecessary_box_returns.rs b/clippy_lints/src/unnecessary_box_returns.rs index 10aee89606ddc..912bcda630b83 100644 --- a/clippy_lints/src/unnecessary_box_returns.rs +++ b/clippy_lints/src/unnecessary_box_returns.rs @@ -3,6 +3,7 @@ use rustc_errors::Applicability; use rustc_hir::{def_id::LocalDefId, FnDecl, FnRetTy, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::Symbol; declare_clippy_lint! { /// ### What it does @@ -46,12 +47,17 @@ impl UnnecessaryBoxReturns { } } - fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, def_id: LocalDefId) { + fn check_fn_item(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, def_id: LocalDefId, name: Symbol) { // we don't want to tell someone to break an exported function if they ask us not to if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) { return; } + // functions which contain the word "box" are exempt from this lint + if name.as_str().contains("box") { + return; + } + let FnRetTy::Return(return_ty_hir) = &decl.output else { return }; let return_ty = cx @@ -91,7 +97,7 @@ impl UnnecessaryBoxReturns { impl LateLintPass<'_> for UnnecessaryBoxReturns { fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) { let TraitItemKind::Fn(signature, _) = &item.kind else { return }; - self.check_fn_decl(cx, signature.decl, item.owner_id.def_id); + self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name); } fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ImplItem<'_>) { @@ -104,11 +110,11 @@ impl LateLintPass<'_> for UnnecessaryBoxReturns { } let ImplItemKind::Fn(signature, ..) = &item.kind else { return }; - self.check_fn_decl(cx, signature.decl, item.owner_id.def_id); + self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name); } fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { let ItemKind::Fn(signature, ..) = &item.kind else { return }; - self.check_fn_decl(cx, signature.decl, item.owner_id.def_id); + self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name); } } diff --git a/tests/ui/unnecessary_box_returns.rs b/tests/ui/unnecessary_box_returns.rs index f2f6aae380e93..fe60d929759ba 100644 --- a/tests/ui/unnecessary_box_returns.rs +++ b/tests/ui/unnecessary_box_returns.rs @@ -22,25 +22,30 @@ impl Foo { } // lint -fn boxed_usize() -> Box { +fn bxed_usize() -> Box { Box::new(5) } // lint -fn _boxed_foo() -> Box { +fn _bxed_foo() -> Box { Box::new(Foo {}) } // don't lint: this is exported -pub fn boxed_foo() -> Box { +pub fn bxed_foo() -> Box { Box::new(Foo {}) } // don't lint: str is unsized -fn boxed_str() -> Box { +fn bxed_str() -> Box { "Hello, world!".to_string().into_boxed_str() } +// don't lint: function contains the word, "box" +fn boxed_usize() -> Box { + Box::new(7) +} + // don't lint: this has an unspecified return type fn default() {} diff --git a/tests/ui/unnecessary_box_returns.stderr b/tests/ui/unnecessary_box_returns.stderr index 0870af5f28f7b..b17512c10a177 100644 --- a/tests/ui/unnecessary_box_returns.stderr +++ b/tests/ui/unnecessary_box_returns.stderr @@ -16,18 +16,18 @@ LL | fn baz(&self) -> Box { = 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:21 + --> $DIR/unnecessary_box_returns.rs:25:20 | -LL | fn boxed_usize() -> Box { - | ^^^^^^^^^^ help: try: `usize` +LL | fn bxed_usize() -> Box { + | ^^^^^^^^^^ 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:20 + --> $DIR/unnecessary_box_returns.rs:30:19 | -LL | fn _boxed_foo() -> Box { - | ^^^^^^^^ help: try: `Foo` +LL | fn _bxed_foo() -> Box { + | ^^^^^^^^ help: try: `Foo` | = help: changing this also requires a change to the return expressions in this function