1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::in_macro;
3 use rustc_ast::ast::{AssocItemKind, Extern, FnKind, FnSig, ImplKind, Item, ItemKind, TraitKind, Ty, TyKind};
4 use rustc_lint::{EarlyContext, EarlyLintPass};
5 use rustc_session::{declare_tool_lint, impl_lint_pass};
6 use rustc_span::{sym, Span};
8 use std::convert::TryInto;
10 declare_clippy_lint! {
12 /// Checks for excessive
13 /// use of bools in structs.
15 /// ### Why is this bad?
16 /// Excessive bools in a struct
17 /// is often a sign that it's used as a state machine,
18 /// which is much better implemented as an enum.
19 /// If it's not the case, excessive bools usually benefit
20 /// from refactoring into two-variant enums for better
21 /// readability and API.
28 /// is_processing: bool,
29 /// is_finished: bool,
41 pub STRUCT_EXCESSIVE_BOOLS,
43 "using too many bools in a struct"
46 declare_clippy_lint! {
48 /// Checks for excessive use of
49 /// bools in function definitions.
51 /// ### Why is this bad?
52 /// Calls to such functions
53 /// are confusing and error prone, because it's
54 /// hard to remember argument order and you have
55 /// no type system support to back you up. Using
56 /// two-variant enums instead of bools often makes
57 /// API easier to use.
62 /// fn f(is_round: bool, is_hot: bool) { ... }
72 /// enum Temperature {
77 /// fn f(shape: Shape, temperature: Temperature) { ... }
79 pub FN_PARAMS_EXCESSIVE_BOOLS,
81 "using too many bools in function parameters"
84 pub struct ExcessiveBools {
85 max_struct_bools: u64,
86 max_fn_params_bools: u64,
91 pub fn new(max_struct_bools: u64, max_fn_params_bools: u64) -> Self {
98 fn check_fn_sig(&self, cx: &EarlyContext<'_>, fn_sig: &FnSig, span: Span) {
99 match fn_sig.header.ext {
100 Extern::Implicit | Extern::Explicit(_) => return,
104 let fn_sig_bools = fn_sig
108 .filter(|param| is_bool_ty(¶m.ty))
112 if self.max_fn_params_bools < fn_sig_bools {
115 FN_PARAMS_EXCESSIVE_BOOLS,
117 &format!("more than {} bools in function parameters", self.max_fn_params_bools),
119 "consider refactoring bools into two-variant enums",
125 impl_lint_pass!(ExcessiveBools => [STRUCT_EXCESSIVE_BOOLS, FN_PARAMS_EXCESSIVE_BOOLS]);
127 fn is_bool_ty(ty: &Ty) -> bool {
128 if let TyKind::Path(None, path) = &ty.kind {
129 if let [name] = path.segments.as_slice() {
130 return name.ident.name == sym::bool;
136 impl EarlyLintPass for ExcessiveBools {
137 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
138 if in_macro(item.span) {
142 ItemKind::Struct(variant_data, _) => {
143 if item.attrs.iter().any(|attr| attr.has_name(sym::repr)) {
147 let struct_bools = variant_data
150 .filter(|field| is_bool_ty(&field.ty))
154 if self.max_struct_bools < struct_bools {
157 STRUCT_EXCESSIVE_BOOLS,
159 &format!("more than {} bools in a struct", self.max_struct_bools),
161 "consider using a state machine or refactoring bools into two-variant enums",
165 ItemKind::Impl(box ImplKind {
166 of_trait: None, items, ..
168 | ItemKind::Trait(box TraitKind(.., items)) => {
170 if let AssocItemKind::Fn(box FnKind(_, fn_sig, _, _)) = &item.kind {
171 self.check_fn_sig(cx, fn_sig, item.span);
175 ItemKind::Fn(box FnKind(_, fn_sig, _, _)) => self.check_fn_sig(cx, fn_sig, item.span),