Skip to content

Commit 3d4b73c

Browse files
committed
[excessive_bools] lint trait functions even without bodies
1 parent 13b737d commit 3d4b73c

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

clippy_lints/src/excessive_bools.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::{get_parent_as_impl, has_repr_attr, is_bool};
33
use rustc_hir::intravisit::FnKind;
4-
use rustc_hir::{Body, FnDecl, HirId, Item, ItemKind, Ty};
4+
use rustc_hir::{Body, FnDecl, HirId, Item, ItemKind, TraitFn, TraitItem, TraitItemKind, Ty};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
77
use rustc_span::Span;
@@ -114,7 +114,7 @@ impl ExcessiveBools {
114114
}
115115

116116
fn check_fn_sig(&self, cx: &LateContext<'_>, fn_decl: &FnDecl<'_>, span: Span) {
117-
if self.too_many_bools(fn_decl.inputs.iter(), Kind::Fn) {
117+
if !span.from_expansion() && self.too_many_bools(fn_decl.inputs.iter(), Kind::Fn) {
118118
span_lint_and_help(
119119
cx,
120120
FN_PARAMS_EXCESSIVE_BOOLS,
@@ -152,6 +152,15 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
152152
}
153153
}
154154

155+
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx TraitItem<'tcx>) {
156+
// functions with a body are already checked by `check_fn`
157+
if let TraitItemKind::Fn(fn_sig, TraitFn::Required(_)) = &trait_item.kind
158+
&& fn_sig.header.abi == Abi::Rust
159+
{
160+
self.check_fn_sig(cx, fn_sig.decl, fn_sig.span);
161+
}
162+
}
163+
155164
fn check_fn(
156165
&mut self,
157166
cx: &LateContext<'tcx>,
@@ -163,7 +172,6 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
163172
) {
164173
if let Some(fn_header) = fn_kind.header()
165174
&& fn_header.abi == Abi::Rust
166-
&& !span.from_expansion()
167175
&& get_parent_as_impl(cx.tcx, hir_id)
168176
.map_or(true,
169177
|impl_item| impl_item.of_trait.is_none()

tests/ui/fn_params_excessive_bools.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ fn t(_: S, _: S, _: Box<S>, _: Vec<u32>, _: bool, _: bool, _: bool, _: bool) {}
2323

2424
struct S;
2525
trait Trait {
26+
// should warn for trait functions with and without body
2627
fn f(_: bool, _: bool, _: bool, _: bool);
2728
fn g(_: bool, _: bool, _: bool, _: Vec<u32>);
2829
#[allow(clippy::fn_params_excessive_bools)]
2930
fn h(_: bool, _: bool, _: bool, _: bool, _: bool, _: bool);
31+
fn i(_: bool, _: bool, _: bool, _: bool) {}
3032
}
3133

3234
impl S {

tests/ui/fn_params_excessive_bools.stderr

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,31 @@ LL | fn t(_: S, _: S, _: Box<S>, _: Vec<u32>, _: bool, _: bool, _: bool, _: bool
1616
= help: consider refactoring bools into two-variant enums
1717

1818
error: more than 3 bools in function parameters
19-
--> $DIR/fn_params_excessive_bools.rs:33:5
19+
--> $DIR/fn_params_excessive_bools.rs:27:5
20+
|
21+
LL | fn f(_: bool, _: bool, _: bool, _: bool);
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= help: consider refactoring bools into two-variant enums
25+
26+
error: more than 3 bools in function parameters
27+
--> $DIR/fn_params_excessive_bools.rs:31:5
28+
|
29+
LL | fn i(_: bool, _: bool, _: bool, _: bool) {}
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= help: consider refactoring bools into two-variant enums
33+
34+
error: more than 3 bools in function parameters
35+
--> $DIR/fn_params_excessive_bools.rs:35:5
2036
|
2137
LL | fn f(&self, _: bool, _: bool, _: bool, _: bool) {}
2238
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2339
|
2440
= help: consider refactoring bools into two-variant enums
2541

2642
error: more than 3 bools in function parameters
27-
--> $DIR/fn_params_excessive_bools.rs:48:5
43+
--> $DIR/fn_params_excessive_bools.rs:50:5
2844
|
2945
LL | / fn n(_: bool, _: u32, _: bool, _: Box<u32>, _: bool, _: bool) {
3046
LL | | fn nn(_: bool, _: bool, _: bool, _: bool) {}
@@ -34,12 +50,12 @@ LL | | }
3450
= help: consider refactoring bools into two-variant enums
3551

3652
error: more than 3 bools in function parameters
37-
--> $DIR/fn_params_excessive_bools.rs:49:9
53+
--> $DIR/fn_params_excessive_bools.rs:51:9
3854
|
3955
LL | fn nn(_: bool, _: bool, _: bool, _: bool) {}
4056
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4157
|
4258
= help: consider refactoring bools into two-variant enums
4359

44-
error: aborting due to 5 previous errors
60+
error: aborting due to 7 previous errors
4561

0 commit comments

Comments
 (0)