[excessive_bools] lint trait functions even without bodies

This commit is contained in:
kraktus 2022-11-07 21:00:37 +01:00
parent 13b737d5b8
commit 3d4b73c26d
3 changed files with 33 additions and 7 deletions

View File

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{get_parent_as_impl, has_repr_attr, is_bool};
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, FnDecl, HirId, Item, ItemKind, Ty};
use rustc_hir::{Body, FnDecl, HirId, Item, ItemKind, TraitFn, TraitItem, TraitItemKind, Ty};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span;
@ -114,7 +114,7 @@ impl ExcessiveBools {
}
fn check_fn_sig(&self, cx: &LateContext<'_>, fn_decl: &FnDecl<'_>, span: Span) {
if self.too_many_bools(fn_decl.inputs.iter(), Kind::Fn) {
if !span.from_expansion() && self.too_many_bools(fn_decl.inputs.iter(), Kind::Fn) {
span_lint_and_help(
cx,
FN_PARAMS_EXCESSIVE_BOOLS,
@ -152,6 +152,15 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
}
}
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx TraitItem<'tcx>) {
// functions with a body are already checked by `check_fn`
if let TraitItemKind::Fn(fn_sig, TraitFn::Required(_)) = &trait_item.kind
&& fn_sig.header.abi == Abi::Rust
{
self.check_fn_sig(cx, fn_sig.decl, fn_sig.span);
}
}
fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
@ -163,7 +172,6 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
) {
if let Some(fn_header) = fn_kind.header()
&& fn_header.abi == Abi::Rust
&& !span.from_expansion()
&& get_parent_as_impl(cx.tcx, hir_id)
.map_or(true,
|impl_item| impl_item.of_trait.is_none()

View File

@ -23,10 +23,12 @@ fn t(_: S, _: S, _: Box<S>, _: Vec<u32>, _: bool, _: bool, _: bool, _: bool) {}
struct S;
trait Trait {
// should warn for trait functions with and without body
fn f(_: bool, _: bool, _: bool, _: bool);
fn g(_: bool, _: bool, _: bool, _: Vec<u32>);
#[allow(clippy::fn_params_excessive_bools)]
fn h(_: bool, _: bool, _: bool, _: bool, _: bool, _: bool);
fn i(_: bool, _: bool, _: bool, _: bool) {}
}
impl S {

View File

@ -16,7 +16,23 @@ LL | fn t(_: S, _: S, _: Box<S>, _: Vec<u32>, _: bool, _: bool, _: bool, _: bool
= help: consider refactoring bools into two-variant enums
error: more than 3 bools in function parameters
--> $DIR/fn_params_excessive_bools.rs:33:5
--> $DIR/fn_params_excessive_bools.rs:27:5
|
LL | fn f(_: bool, _: bool, _: bool, _: bool);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider refactoring bools into two-variant enums
error: more than 3 bools in function parameters
--> $DIR/fn_params_excessive_bools.rs:31:5
|
LL | fn i(_: bool, _: bool, _: bool, _: bool) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider refactoring bools into two-variant enums
error: more than 3 bools in function parameters
--> $DIR/fn_params_excessive_bools.rs:35:5
|
LL | fn f(&self, _: bool, _: bool, _: bool, _: bool) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -24,7 +40,7 @@ LL | fn f(&self, _: bool, _: bool, _: bool, _: bool) {}
= help: consider refactoring bools into two-variant enums
error: more than 3 bools in function parameters
--> $DIR/fn_params_excessive_bools.rs:48:5
--> $DIR/fn_params_excessive_bools.rs:50:5
|
LL | / fn n(_: bool, _: u32, _: bool, _: Box<u32>, _: bool, _: bool) {
LL | | fn nn(_: bool, _: bool, _: bool, _: bool) {}
@ -34,12 +50,12 @@ LL | | }
= help: consider refactoring bools into two-variant enums
error: more than 3 bools in function parameters
--> $DIR/fn_params_excessive_bools.rs:49:9
--> $DIR/fn_params_excessive_bools.rs:51:9
|
LL | fn nn(_: bool, _: bool, _: bool, _: bool) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider refactoring bools into two-variant enums
error: aborting due to 5 previous errors
error: aborting due to 7 previous errors