-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathexcessive_bools.rs
178 lines (168 loc) · 5.37 KB
/
excessive_bools.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{get_parent_as_impl, has_repr_attr, is_bool};
use rustc_abi::ExternAbi;
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, FnDecl, Item, ItemKind, TraitFn, TraitItem, TraitItemKind, Ty};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;
use rustc_span::Span;
use rustc_span::def_id::LocalDefId;
declare_clippy_lint! {
/// ### What it does
/// Checks for excessive
/// use of bools in structs.
///
/// ### Why is this bad?
/// Excessive bools in a struct is often a sign that
/// the type is being used to represent a state
/// machine, which is much better implemented as an
/// enum.
///
/// The reason an enum is better for state machines
/// over structs is that enums more easily forbid
/// invalid states.
///
/// Structs with too many booleans may benefit from refactoring
/// into multi variant enums for better readability and API.
///
/// ### Example
/// ```no_run
/// struct S {
/// is_pending: bool,
/// is_processing: bool,
/// is_finished: bool,
/// }
/// ```
///
/// Use instead:
/// ```no_run
/// enum S {
/// Pending,
/// Processing,
/// Finished,
/// }
/// ```
#[clippy::version = "1.43.0"]
pub STRUCT_EXCESSIVE_BOOLS,
pedantic,
"using too many bools in a struct"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for excessive use of
/// bools in function definitions.
///
/// ### Why is this bad?
/// Calls to such functions
/// are confusing and error prone, because it's
/// hard to remember argument order and you have
/// no type system support to back you up. Using
/// two-variant enums instead of bools often makes
/// API easier to use.
///
/// ### Example
/// ```rust,ignore
/// fn f(is_round: bool, is_hot: bool) { ... }
/// ```
///
/// Use instead:
/// ```rust,ignore
/// enum Shape {
/// Round,
/// Spiky,
/// }
///
/// enum Temperature {
/// Hot,
/// IceCold,
/// }
///
/// fn f(shape: Shape, temperature: Temperature) { ... }
/// ```
#[clippy::version = "1.43.0"]
pub FN_PARAMS_EXCESSIVE_BOOLS,
pedantic,
"using too many bools in function parameters"
}
pub struct ExcessiveBools {
max_struct_bools: u64,
max_fn_params_bools: u64,
}
impl ExcessiveBools {
pub fn new(conf: &'static Conf) -> Self {
Self {
max_struct_bools: conf.max_struct_bools,
max_fn_params_bools: conf.max_fn_params_bools,
}
}
}
impl_lint_pass!(ExcessiveBools => [STRUCT_EXCESSIVE_BOOLS, FN_PARAMS_EXCESSIVE_BOOLS]);
fn has_n_bools<'tcx>(iter: impl Iterator<Item = &'tcx Ty<'tcx>>, mut count: u64) -> bool {
iter.filter(|ty| is_bool(ty)).any(|_| {
let (x, overflow) = count.overflowing_sub(1);
count = x;
overflow
})
}
fn check_fn_decl(cx: &LateContext<'_>, decl: &FnDecl<'_>, sp: Span, max: u64) {
if has_n_bools(decl.inputs.iter(), max) && !sp.from_expansion() {
span_lint_and_help(
cx,
FN_PARAMS_EXCESSIVE_BOOLS,
sp,
format!("more than {max} bools in function parameters"),
None,
"consider refactoring bools into two-variant enums",
);
}
}
impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
if let ItemKind::Struct(_, variant_data, _) = &item.kind
&& variant_data.fields().len() as u64 > self.max_struct_bools
&& has_n_bools(
variant_data.fields().iter().map(|field| field.ty),
self.max_struct_bools,
)
&& !has_repr_attr(cx, item.hir_id())
&& !item.span.from_expansion()
{
span_lint_and_help(
cx,
STRUCT_EXCESSIVE_BOOLS,
item.span,
format!("more than {} bools in a struct", self.max_struct_bools),
None,
"consider using a state machine or refactoring bools into two-variant enums",
);
}
}
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 == ExternAbi::Rust
&& fn_sig.decl.inputs.len() as u64 > self.max_fn_params_bools
{
check_fn_decl(cx, fn_sig.decl, fn_sig.span, self.max_fn_params_bools);
}
}
fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
fn_kind: FnKind<'tcx>,
fn_decl: &'tcx FnDecl<'tcx>,
_: &'tcx Body<'tcx>,
span: Span,
def_id: LocalDefId,
) {
if let Some(fn_header) = fn_kind.header()
&& fn_header.abi == ExternAbi::Rust
&& fn_decl.inputs.len() as u64 > self.max_fn_params_bools
&& get_parent_as_impl(cx.tcx, cx.tcx.local_def_id_to_hir_id(def_id))
.is_none_or(|impl_item| impl_item.of_trait.is_none())
{
check_fn_decl(cx, fn_decl, span, self.max_fn_params_bools);
}
}
}