Skip to content

Commit f7cb937

Browse files
committed
Lint on non-trait implementations
1 parent 0567b1f commit f7cb937

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

clippy_lints/src/unnecessary_box_returns.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::implements_trait};
22
use rustc_errors::Applicability;
3-
use rustc_hir::{def_id::LocalDefId, FnDecl, FnRetTy, Item, ItemKind, TraitItem, TraitItemKind};
3+
use rustc_hir::{def_id::LocalDefId, FnDecl, FnRetTy, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind};
44
use rustc_hir_analysis::hir_ty_to_ty;
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -87,6 +87,19 @@ impl LateLintPass<'_> for UnnecessaryBoxReturns {
8787
self.check_fn_decl(cx, signature.decl, item.owner_id.def_id);
8888
}
8989

90+
fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ImplItem<'_>) {
91+
// Ignore implementations of traits, because the lint should be on the
92+
// trait, not on the implmentation of it.
93+
let Node::Item(parent) = cx.tcx.hir().get_parent(item.hir_id()) else { return };
94+
let ItemKind::Impl(parent) = parent.kind else { return };
95+
if parent.of_trait.is_some() {
96+
return;
97+
}
98+
99+
let ImplItemKind::Fn(signature, ..) = &item.kind else { return };
100+
self.check_fn_decl(cx, signature.decl, item.owner_id.def_id);
101+
}
102+
90103
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
91104
let ItemKind::Fn(signature, ..) = &item.kind else { return };
92105
self.check_fn_decl(cx, signature.decl, item.owner_id.def_id);

tests/ui/unnecessary_box_returns.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ impl Bar for Foo {
1414
}
1515
}
1616

17+
impl Foo {
18+
fn baz(&self) -> Box<usize> {
19+
Box::new(13)
20+
}
21+
}
22+
1723
// lint
1824
fn boxed_usize() -> Box<usize> {
1925
Box::new(5)

tests/ui/unnecessary_box_returns.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@ LL | fn baz(&self) -> Box<usize>;
77
= note: `-D clippy::unnecessary-box-returns` implied by `-D warnings`
88

99
error: boxed return of the sized type `usize`
10-
--> $DIR/unnecessary_box_returns.rs:18:21
10+
--> $DIR/unnecessary_box_returns.rs:18:22
11+
|
12+
LL | fn baz(&self) -> Box<usize> {
13+
| ^^^^^^^^^^ help: try: `usize`
14+
15+
error: boxed return of the sized type `usize`
16+
--> $DIR/unnecessary_box_returns.rs:24:21
1117
|
1218
LL | fn boxed_usize() -> Box<usize> {
1319
| ^^^^^^^^^^ help: try: `usize`
1420

1521
error: boxed return of the sized type `Foo`
16-
--> $DIR/unnecessary_box_returns.rs:23:20
22+
--> $DIR/unnecessary_box_returns.rs:29:20
1723
|
1824
LL | fn _boxed_foo() -> Box<Foo> {
1925
| ^^^^^^^^ help: try: `Foo`
2026

21-
error: aborting due to 3 previous errors
27+
error: aborting due to 4 previous errors
2228

0 commit comments

Comments
 (0)