Skip to content

Commit 76d13bb

Browse files
committed
Don't lint functions with "box" in their name
1 parent a143fb7 commit 76d13bb

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

clippy_lints/src/unnecessary_box_returns.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_errors::Applicability;
33
use rustc_hir::{def_id::LocalDefId, FnDecl, FnRetTy, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::{declare_tool_lint, impl_lint_pass};
6+
use rustc_span::Symbol;
67

78
declare_clippy_lint! {
89
/// ### What it does
@@ -46,12 +47,17 @@ impl UnnecessaryBoxReturns {
4647
}
4748
}
4849

49-
fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, def_id: LocalDefId) {
50+
fn check_fn_item(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, def_id: LocalDefId, name: Symbol) {
5051
// we don't want to tell someone to break an exported function if they ask us not to
5152
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) {
5253
return;
5354
}
5455

56+
// functions which contain the word "box" are exempt from this lint
57+
if name.as_str().contains("box") {
58+
return;
59+
}
60+
5561
let FnRetTy::Return(return_ty_hir) = &decl.output else { return };
5662

5763
let return_ty = cx
@@ -91,7 +97,7 @@ impl UnnecessaryBoxReturns {
9197
impl LateLintPass<'_> for UnnecessaryBoxReturns {
9298
fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) {
9399
let TraitItemKind::Fn(signature, _) = &item.kind else { return };
94-
self.check_fn_decl(cx, signature.decl, item.owner_id.def_id);
100+
self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name);
95101
}
96102

97103
fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ImplItem<'_>) {
@@ -104,11 +110,11 @@ impl LateLintPass<'_> for UnnecessaryBoxReturns {
104110
}
105111

106112
let ImplItemKind::Fn(signature, ..) = &item.kind else { return };
107-
self.check_fn_decl(cx, signature.decl, item.owner_id.def_id);
113+
self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name);
108114
}
109115

110116
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
111117
let ItemKind::Fn(signature, ..) = &item.kind else { return };
112-
self.check_fn_decl(cx, signature.decl, item.owner_id.def_id);
118+
self.check_fn_item(cx, signature.decl, item.owner_id.def_id, item.ident.name);
113119
}
114120
}

tests/ui/unnecessary_box_returns.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,30 @@ impl Foo {
2222
}
2323

2424
// lint
25-
fn boxed_usize() -> Box<usize> {
25+
fn bxed_usize() -> Box<usize> {
2626
Box::new(5)
2727
}
2828

2929
// lint
30-
fn _boxed_foo() -> Box<Foo> {
30+
fn _bxed_foo() -> Box<Foo> {
3131
Box::new(Foo {})
3232
}
3333

3434
// don't lint: this is exported
35-
pub fn boxed_foo() -> Box<Foo> {
35+
pub fn bxed_foo() -> Box<Foo> {
3636
Box::new(Foo {})
3737
}
3838

3939
// don't lint: str is unsized
40-
fn boxed_str() -> Box<str> {
40+
fn bxed_str() -> Box<str> {
4141
"Hello, world!".to_string().into_boxed_str()
4242
}
4343

44+
// don't lint: function contains the word, "box"
45+
fn boxed_usize() -> Box<usize> {
46+
Box::new(7)
47+
}
48+
4449
// don't lint: this has an unspecified return type
4550
fn default() {}
4651

tests/ui/unnecessary_box_returns.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ LL | fn baz(&self) -> Box<usize> {
1616
= help: changing this also requires a change to the return expressions in this function
1717

1818
error: boxed return of the sized type `usize`
19-
--> $DIR/unnecessary_box_returns.rs:25:21
19+
--> $DIR/unnecessary_box_returns.rs:25:20
2020
|
21-
LL | fn boxed_usize() -> Box<usize> {
22-
| ^^^^^^^^^^ help: try: `usize`
21+
LL | fn bxed_usize() -> Box<usize> {
22+
| ^^^^^^^^^^ help: try: `usize`
2323
|
2424
= help: changing this also requires a change to the return expressions in this function
2525

2626
error: boxed return of the sized type `Foo`
27-
--> $DIR/unnecessary_box_returns.rs:30:20
27+
--> $DIR/unnecessary_box_returns.rs:30:19
2828
|
29-
LL | fn _boxed_foo() -> Box<Foo> {
30-
| ^^^^^^^^ help: try: `Foo`
29+
LL | fn _bxed_foo() -> Box<Foo> {
30+
| ^^^^^^^^ help: try: `Foo`
3131
|
3232
= help: changing this also requires a change to the return expressions in this function
3333

0 commit comments

Comments
 (0)