1 use crate::utils::{attr_by_name, in_macro, match_path_ast, span_lint_and_help};
2 use rustc_ast::ast::{AssocItemKind, Extern, FnSig, Item, ItemKind, Ty, TyKind};
3 use rustc_lint::{EarlyContext, EarlyLintPass};
4 use rustc_session::{declare_tool_lint, impl_lint_pass};
7 use std::convert::TryInto;
10 /// **What it does:** Checks for excessive
11 /// use of bools in structs.
13 /// **Why is this bad?** Excessive bools in a struct
14 /// is often a sign that it's used as a state machine,
15 /// which is much better implemented as an enum.
16 /// If it's not the case, excessive bools usually benefit
17 /// from refactoring into two-variant enums for better
18 /// readability and API.
20 /// **Known problems:** None.
27 /// is_processing: bool,
28 /// is_finished: bool,
40 pub STRUCT_EXCESSIVE_BOOLS,
42 "using too many bools in a struct"
45 declare_clippy_lint! {
46 /// **What it does:** Checks for excessive use of
47 /// bools in function definitions.
49 /// **Why is this bad?** Calls to such functions
50 /// are confusing and error prone, because it's
51 /// hard to remember argument order and you have
52 /// no type system support to back you up. Using
53 /// two-variant enums instead of bools often makes
54 /// API easier to use.
56 /// **Known problems:** None.
61 /// fn f(is_round: bool, is_hot: bool) { ... }
71 /// enum Temperature {
76 /// fn f(shape: Shape, temperature: Temperature) { ... }
78 pub FN_PARAMS_EXCESSIVE_BOOLS,
80 "using too many bools in function parameters"
83 pub struct ExcessiveBools {
84 max_struct_bools: u64,
85 max_fn_params_bools: u64,
90 pub fn new(max_struct_bools: u64, max_fn_params_bools: u64) -> Self {
97 fn check_fn_sig(&self, cx: &EarlyContext<'_>, fn_sig: &FnSig, span: Span) {
98 match fn_sig.header.ext {
99 Extern::Implicit | Extern::Explicit(_) => return,
103 let fn_sig_bools = fn_sig
107 .filter(|param| is_bool_ty(¶m.ty))
111 if self.max_fn_params_bools < fn_sig_bools {
114 FN_PARAMS_EXCESSIVE_BOOLS,
116 &format!("more than {} bools in function parameters", self.max_fn_params_bools),
118 "consider refactoring bools into two-variant enums",
124 impl_lint_pass!(ExcessiveBools => [STRUCT_EXCESSIVE_BOOLS, FN_PARAMS_EXCESSIVE_BOOLS]);
126 fn is_bool_ty(ty: &Ty) -> bool {
127 if let TyKind::Path(None, path) = &ty.kind {
128 return match_path_ast(path, &["bool"]);
133 impl EarlyLintPass for ExcessiveBools {
134 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
135 if in_macro(item.span) {
139 ItemKind::Struct(variant_data, _) => {
140 if attr_by_name(&item.attrs, "repr").is_some() {
144 let struct_bools = variant_data
147 .filter(|field| is_bool_ty(&field.ty))
151 if self.max_struct_bools < struct_bools {
154 STRUCT_EXCESSIVE_BOOLS,
156 &format!("more than {} bools in a struct", self.max_struct_bools),
158 "consider using a state machine or refactoring bools into two-variant enums",
163 of_trait: None, items, ..
165 | ItemKind::Trait(_, _, _, _, items) => {
167 if let AssocItemKind::Fn(_, fn_sig, _, _) = &item.kind {
168 self.check_fn_sig(cx, fn_sig, item.span);
172 ItemKind::Fn(_, fn_sig, _, _) => self.check_fn_sig(cx, fn_sig, item.span),