From 9e1c7febe261b834d2c787aca2fcd9de577d76d8 Mon Sep 17 00:00:00 2001 From: kraktus Date: Sat, 29 Oct 2022 16:12:41 +0200 Subject: [PATCH] `excessive_bools`, do not lint in trait impls Should not lint because the trait might not be changeable by the user We only lint in the trait definition --- clippy_lints/src/excessive_bools.rs | 11 ++++++++--- tests/ui/fn_params_excessive_bools.rs | 2 ++ tests/ui/fn_params_excessive_bools.stderr | 14 +++----------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/excessive_bools.rs b/clippy_lints/src/excessive_bools.rs index 281d0631d06a..68c5e5ab1f1e 100644 --- a/clippy_lints/src/excessive_bools.rs +++ b/clippy_lints/src/excessive_bools.rs @@ -1,5 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::{has_repr_attr, is_bool}; +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_lint::{LateContext, LateLintPass}; @@ -159,11 +159,16 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools { fn_decl: &'tcx FnDecl<'tcx>, _: &'tcx Body<'tcx>, span: Span, - _: HirId, + hir_id: HirId, ) { if let Some(fn_header) = fn_kind.header() && fn_header.abi == Abi::Rust - && !span.from_expansion() { + && !span.from_expansion() + && get_parent_as_impl(cx.tcx, hir_id) + .map_or(true, + |impl_item| impl_item.of_trait.is_none() + ) + { self.check_fn_sig(cx, fn_decl, span); } } diff --git a/tests/ui/fn_params_excessive_bools.rs b/tests/ui/fn_params_excessive_bools.rs index 4fcd4d54ce62..2635e433a319 100644 --- a/tests/ui/fn_params_excessive_bools.rs +++ b/tests/ui/fn_params_excessive_bools.rs @@ -35,6 +35,8 @@ impl S { } impl Trait for S { + // Should not lint because the trait might not be changeable by the user + // We only lint in the trait definition fn f(_: bool, _: bool, _: bool, _: bool) {} fn g(_: bool, _: bool, _: bool, _: Vec) {} } diff --git a/tests/ui/fn_params_excessive_bools.stderr b/tests/ui/fn_params_excessive_bools.stderr index 9d22d3851d2f..7edfd07b9db3 100644 --- a/tests/ui/fn_params_excessive_bools.stderr +++ b/tests/ui/fn_params_excessive_bools.stderr @@ -24,15 +24,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:38: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:43:5 + --> $DIR/fn_params_excessive_bools.rs:45:5 | LL | / fn n(_: bool, _: u32, _: bool, _: Box, _: bool, _: bool) { LL | | fn nn(_: bool, _: bool, _: bool, _: bool) {} @@ -42,12 +34,12 @@ LL | | } = help: consider refactoring bools into two-variant enums error: more than 3 bools in function parameters - --> $DIR/fn_params_excessive_bools.rs:44:9 + --> $DIR/fn_params_excessive_bools.rs:46:9 | LL | fn nn(_: bool, _: bool, _: bool, _: bool) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider refactoring bools into two-variant enums -error: aborting due to 6 previous errors +error: aborting due to 5 previous errors