-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Port mitsuhiko's excessive bools lints #5125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
use crate::utils::{attr_by_name, in_macro, match_path_ast, span_lint_and_help}; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_span::Span; | ||
use syntax::ast::{AssocItemKind, Extern, FnSig, Item, ItemKind, Ty, TyKind}; | ||
|
||
use std::convert::TryInto; | ||
|
||
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 it's used as a state machine, | ||
/// which is much better implemented as an enum. | ||
/// If it's not the case, excessive bools usually benefit | ||
/// from refactoring into two-variant enums for better | ||
/// readability and API. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// Bad: | ||
/// ```rust | ||
/// struct S { | ||
/// is_pending: bool, | ||
/// is_processing: bool, | ||
/// is_finished: bool, | ||
/// } | ||
/// ``` | ||
/// | ||
/// Good: | ||
/// ```rust | ||
/// enum S { | ||
/// Pending, | ||
/// Processing, | ||
/// Finished, | ||
/// } | ||
/// ``` | ||
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. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// Bad: | ||
/// ```rust,ignore | ||
/// fn f(is_round: bool, is_hot: bool) { ... } | ||
/// ``` | ||
/// | ||
/// Good: | ||
/// ```rust,ignore | ||
/// enum Shape { | ||
/// Round, | ||
/// Spiky, | ||
/// } | ||
/// | ||
/// enum Temperature { | ||
/// Hot, | ||
/// IceCold, | ||
/// } | ||
/// | ||
/// fn f(shape: Shape, temperature: Temperature) { ... } | ||
/// ``` | ||
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 { | ||
#[must_use] | ||
pub fn new(max_struct_bools: u64, max_fn_params_bools: u64) -> Self { | ||
Self { | ||
max_struct_bools, | ||
max_fn_params_bools, | ||
} | ||
} | ||
|
||
fn check_fn_sig(&self, cx: &EarlyContext<'_>, fn_sig: &FnSig, span: Span) { | ||
match fn_sig.header.ext { | ||
Extern::Implicit | Extern::Explicit(_) => return, | ||
Extern::None => (), | ||
} | ||
|
||
let fn_sig_bools = fn_sig | ||
.decl | ||
.inputs | ||
.iter() | ||
.filter(|param| is_bool_ty(¶m.ty)) | ||
.count() | ||
.try_into() | ||
.unwrap(); | ||
if self.max_fn_params_bools < fn_sig_bools { | ||
span_lint_and_help( | ||
cx, | ||
FN_PARAMS_EXCESSIVE_BOOLS, | ||
span, | ||
&format!("more than {} bools in function parameters", self.max_fn_params_bools), | ||
"consider refactoring bools into two-variant enums", | ||
); | ||
} | ||
} | ||
} | ||
|
||
impl_lint_pass!(ExcessiveBools => [STRUCT_EXCESSIVE_BOOLS, FN_PARAMS_EXCESSIVE_BOOLS]); | ||
|
||
fn is_bool_ty(ty: &Ty) -> bool { | ||
if let TyKind::Path(None, path) = &ty.kind { | ||
return match_path_ast(path, &["bool"]); | ||
} | ||
false | ||
} | ||
|
||
impl EarlyLintPass for ExcessiveBools { | ||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { | ||
if in_macro(item.span) { | ||
return; | ||
} | ||
match &item.kind { | ||
ItemKind::Struct(variant_data, _) => { | ||
if attr_by_name(&item.attrs, "repr").is_some() { | ||
return; | ||
} | ||
|
||
let struct_bools = variant_data | ||
.fields() | ||
.iter() | ||
.filter(|field| is_bool_ty(&field.ty)) | ||
.count() | ||
.try_into() | ||
.unwrap(); | ||
if self.max_struct_bools < struct_bools { | ||
span_lint_and_help( | ||
cx, | ||
STRUCT_EXCESSIVE_BOOLS, | ||
item.span, | ||
&format!("more than {} bools in a struct", self.max_struct_bools), | ||
"consider using a state machine or refactoring bools into two-variant enums", | ||
); | ||
} | ||
}, | ||
ItemKind::Impl { | ||
of_trait: None, items, .. | ||
} | ||
| ItemKind::Trait(_, _, _, _, items) => { | ||
for item in items { | ||
if let AssocItemKind::Fn(fn_sig, _) = &item.kind { | ||
self.check_fn_sig(cx, fn_sig, item.span); | ||
} | ||
} | ||
}, | ||
ItemKind::Fn(fn_sig, _, _) => self.check_fn_sig(cx, fn_sig, item.span), | ||
_ => (), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
max-fn-params-bools = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#![warn(clippy::fn_params_excessive_bools)] | ||
|
||
fn f(_: bool) {} | ||
fn g(_: bool, _: bool) {} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: more than 1 bools in function parameters | ||
--> $DIR/test.rs:4:1 | ||
| | ||
LL | fn g(_: bool, _: bool) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::fn-params-excessive-bools` implied by `-D warnings` | ||
= help: consider refactoring bools into two-variant enums | ||
|
||
error: aborting due to previous error | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
max-struct-bools = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![warn(clippy::struct_excessive_bools)] | ||
|
||
struct S { | ||
a: bool, | ||
} | ||
|
||
struct Foo {} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error: more than 0 bools in a struct | ||
--> $DIR/test.rs:3:1 | ||
| | ||
LL | / struct S { | ||
LL | | a: bool, | ||
LL | | } | ||
| |_^ | ||
| | ||
= note: `-D clippy::struct-excessive-bools` implied by `-D warnings` | ||
= help: consider using a state machine or refactoring bools into two-variant enums | ||
|
||
error: aborting due to previous error | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `third-party` at line 5 column 1 | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-struct-bools`, `max-fn-params-bools`, `third-party` at line 5 column 1 | ||
|
||
error: aborting due to previous error | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.