Skip to content

Commit 7112c38

Browse files
Improve excessive bools docs (#17080)
Improve the `fn_params_excessive_bools` docs to better explain why boolean parameters hurt call-site readability and to describe the main replacement patterns: enums for mode selection, separate named operations for distinct behaviors, and structs/options types for grouped flags. This also documents that `max-fn-params-bools = 0` is valid and lints any function with a bool parameter. changelog: [fn_params_excessive_bools]: improve documentation
2 parents aede4dc + d082244 commit 7112c38

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

book/src/lint_configuration.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,8 @@ be filtering for common types.
780780

781781

782782
## `max-fn-params-bools`
783-
The maximum number of bool parameters a function can have
783+
The maximum number of bool parameters a function can have.
784+
Use `0` to lint on any function with a bool parameter.
784785

785786
**Default Value:** `3`
786787

clippy_config/src/conf.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ define_Conf! {
713713
/// be filtering for common types.
714714
#[lints(manual_let_else)]
715715
matches_for_let_else: MatchLintBehaviour = MatchLintBehaviour::WellKnownTypes,
716-
/// The maximum number of bool parameters a function can have
716+
/// The maximum number of bool parameters a function can have.
717+
/// Use `0` to lint on any function with a bool parameter.
717718
#[lints(fn_params_excessive_bools)]
718719
max_fn_params_bools: u64 = 3,
719720
/// The maximum size of a file included via `include_bytes!()` or `include_str!()`, in bytes

clippy_lints/src/excessive_bools.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,51 @@ use rustc_span::def_id::LocalDefId;
1111

1212
declare_clippy_lint! {
1313
/// ### What it does
14-
/// Checks for excessive use of
15-
/// bools in function definitions.
14+
/// Checks for excessive use of bools in function declarations.
1615
///
1716
/// ### Why is this bad?
18-
/// Calls to such functions
19-
/// are confusing and error prone, because it's
20-
/// hard to remember argument order and you have
21-
/// no type system support to back you up. Using
22-
/// two-variant enums instead of bools often makes
23-
/// API easier to use.
17+
/// Boolean parameters obscure meaning at the call site. The reader has to
18+
/// remember what each `true` or `false` means and which position each flag
19+
/// belongs in, with no help from the type system.
20+
///
21+
/// The best replacement depends on what role the bool plays:
22+
///
23+
/// - Use a two-variant enum when one parameter selects a mode.
24+
/// - Split one function into two named operations when the choices are
25+
/// distinct actions.
26+
/// - Group multiple related flags into a struct or options type when they
27+
/// travel together.
28+
/// - Move the setting onto `self` or an existing config/context parameter
29+
/// when it is really part of the surrounding state.
30+
///
31+
/// This optimizes for readability of the calling code, which future readers
32+
/// encounter more often than the function declaration.
33+
///
34+
/// The `max-fn-params-bools` configuration accepts `0`, which lints on any
35+
/// function with a bool parameter.
2436
///
2537
/// ### Example
2638
/// ```rust,ignore
27-
/// fn f(is_round: bool, is_hot: bool) { ... }
39+
/// fn render_document(show_hidden: bool, is_draft: bool) { ... }
2840
/// ```
2941
///
3042
/// Use instead:
3143
/// ```rust,ignore
32-
/// enum Shape {
33-
/// Round,
34-
/// Spiky,
44+
/// enum Visibility {
45+
/// ShowHidden,
46+
/// HideHidden,
3547
/// }
3648
///
37-
/// enum Temperature {
38-
/// Hot,
39-
/// IceCold,
49+
/// enum DocumentKind {
50+
/// Draft,
51+
/// Final,
4052
/// }
4153
///
42-
/// fn f(shape: Shape, temperature: Temperature) { ... }
54+
/// fn render_document(visibility: Visibility, kind: DocumentKind) { ... }
55+
///
56+
/// // or split the operation when the choices are distinct
57+
/// fn render_draft() { ... }
58+
/// fn render_final() { ... }
4359
/// ```
4460
#[clippy::version = "1.43.0"]
4561
pub FN_PARAMS_EXCESSIVE_BOOLS,

0 commit comments

Comments
 (0)