Skip to content

Commit

Permalink
Don't lint functions with "box" in their name
Browse files Browse the repository at this point in the history
  • Loading branch information
botahamec committed Mar 26, 2023
1 parent a143fb7 commit 76d13bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
14 changes: 10 additions & 4 deletions clippy_lints/src/unnecessary_box_returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<'_>) {
Expand All @@ -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);
}
}
13 changes: 9 additions & 4 deletions tests/ui/unnecessary_box_returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,30 @@ impl Foo {
}

// lint
fn boxed_usize() -> Box<usize> {
fn bxed_usize() -> Box<usize> {
Box::new(5)
}

// lint
fn _boxed_foo() -> Box<Foo> {
fn _bxed_foo() -> Box<Foo> {
Box::new(Foo {})
}

// don't lint: this is exported
pub fn boxed_foo() -> Box<Foo> {
pub fn bxed_foo() -> Box<Foo> {
Box::new(Foo {})
}

// don't lint: str is unsized
fn boxed_str() -> Box<str> {
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() {}

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/unnecessary_box_returns.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ LL | fn baz(&self) -> Box<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:21
--> $DIR/unnecessary_box_returns.rs:25:20
|
LL | fn boxed_usize() -> Box<usize> {
| ^^^^^^^^^^ help: try: `usize`
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:20
--> $DIR/unnecessary_box_returns.rs:30:19
|
LL | fn _boxed_foo() -> Box<Foo> {
| ^^^^^^^^ help: try: `Foo`
LL | fn _bxed_foo() -> Box<Foo> {
| ^^^^^^^^ help: try: `Foo`
|
= help: changing this also requires a change to the return expressions in this function

Expand Down

0 comments on commit 76d13bb

Please sign in to comment.