Add Checker::any_enabled shortcut (#4630)

Add Checker::any_enabled shortcut

 ## Summary

 Akin to #4625, This is a refactoring that shortens a bunch of code by replacing `checker.settings.rules.any_enabled` with `checker.any_enabled`.

 ## Test Plan

 `cargo clippy`
This commit is contained in:
konstin 2023-05-24 16:32:55 +02:00 committed by GitHub
parent 5cedf0f724
commit 17d938f078
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 47 deletions

View file

@ -174,10 +174,17 @@ impl<'a> Checker<'a> {
&self.semantic_model &self.semantic_model
} }
/// Returns whether the given rule should be checked.
#[inline] #[inline]
pub(crate) const fn enabled(&self, rule: Rule) -> bool { pub(crate) const fn enabled(&self, rule: Rule) -> bool {
self.settings.rules.enabled(rule) self.settings.rules.enabled(rule)
} }
/// Returns whether any of the given rules should be checked.
#[inline]
pub(crate) const fn any_enabled(&self, rules: &[Rule]) -> bool {
self.settings.rules.any_enabled(rules)
}
} }
impl<'a, 'b> Visitor<'b> for Checker<'a> impl<'a, 'b> Visitor<'b> for Checker<'a>
@ -450,7 +457,7 @@ where
flake8_bugbear::rules::cached_instance_method(self, decorator_list); flake8_bugbear::rules::cached_instance_method(self, decorator_list);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::UnnecessaryReturnNone, Rule::UnnecessaryReturnNone,
Rule::ImplicitReturnValue, Rule::ImplicitReturnValue,
Rule::ImplicitReturn, Rule::ImplicitReturn,
@ -534,7 +541,7 @@ where
} }
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::PytestFixtureIncorrectParenthesesStyle, Rule::PytestFixtureIncorrectParenthesesStyle,
Rule::PytestFixturePositionalArgs, Rule::PytestFixturePositionalArgs,
Rule::PytestExtraneousScopeFunction, Rule::PytestExtraneousScopeFunction,
@ -557,14 +564,14 @@ where
); );
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::PytestParametrizeNamesWrongType, Rule::PytestParametrizeNamesWrongType,
Rule::PytestParametrizeValuesWrongType, Rule::PytestParametrizeValuesWrongType,
]) { ]) {
flake8_pytest_style::rules::parametrize(self, decorator_list); flake8_pytest_style::rules::parametrize(self, decorator_list);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::PytestIncorrectMarkParenthesesStyle, Rule::PytestIncorrectMarkParenthesesStyle,
Rule::PytestUseFixturesWithoutParameters, Rule::PytestUseFixturesWithoutParameters,
]) { ]) {
@ -708,7 +715,7 @@ where
} }
if !self.is_stub { if !self.is_stub {
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::AbstractBaseClassWithoutAbstractMethod, Rule::AbstractBaseClassWithoutAbstractMethod,
Rule::EmptyMethodWithoutAbstractDecorator, Rule::EmptyMethodWithoutAbstractDecorator,
]) { ]) {
@ -741,7 +748,7 @@ where
flake8_pie::rules::non_unique_enums(self, stmt, body); flake8_pie::rules::non_unique_enums(self, stmt, body);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::MutableDataclassDefault, Rule::MutableDataclassDefault,
Rule::FunctionCallInDataclassDefaultArgument, Rule::FunctionCallInDataclassDefaultArgument,
]) && ruff::rules::is_dataclass(&self.semantic_model, decorator_list) ]) && ruff::rules::is_dataclass(&self.semantic_model, decorator_list)
@ -1331,7 +1338,7 @@ where
flake8_bugbear::rules::cannot_raise_literal(self, exc); flake8_bugbear::rules::cannot_raise_literal(self, exc);
} }
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::RawStringInException, Rule::RawStringInException,
Rule::FStringInException, Rule::FStringInException,
Rule::DotFormatInException, Rule::DotFormatInException,
@ -1574,7 +1581,7 @@ where
self.diagnostics.push(diagnostic); self.diagnostics.push(diagnostic);
} }
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::DuplicateHandlerException, Rule::DuplicateHandlerException,
Rule::DuplicateTryBlockException, Rule::DuplicateTryBlockException,
]) { ]) {
@ -1671,7 +1678,7 @@ where
} }
if self.is_stub { if self.is_stub {
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::UnprefixedTypeParam, Rule::UnprefixedTypeParam,
Rule::AssignmentDefaultInStub, Rule::AssignmentDefaultInStub,
Rule::UnannotatedAssignmentInStub, Rule::UnannotatedAssignmentInStub,
@ -2219,7 +2226,7 @@ where
match expr { match expr {
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => { Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
// Ex) Optional[...], Union[...] // Ex) Optional[...], Union[...]
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::MissingFutureAnnotationsImport, Rule::MissingFutureAnnotationsImport,
Rule::NonPEP604Annotation, Rule::NonPEP604Annotation,
]) { ]) {
@ -2255,7 +2262,7 @@ where
self.semantic_model.flags |= SemanticModelFlags::LITERAL; self.semantic_model.flags |= SemanticModelFlags::LITERAL;
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::SysVersionSlice3, Rule::SysVersionSlice3,
Rule::SysVersion2, Rule::SysVersion2,
Rule::SysVersion0, Rule::SysVersion0,
@ -2306,7 +2313,7 @@ where
} }
// Ex) List[...] // Ex) List[...]
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::MissingFutureAnnotationsImport, Rule::MissingFutureAnnotationsImport,
Rule::NonPEP585Annotation, Rule::NonPEP585Annotation,
]) { ]) {
@ -2384,7 +2391,7 @@ where
} }
Expr::Attribute(ast::ExprAttribute { attr, value, .. }) => { Expr::Attribute(ast::ExprAttribute { attr, value, .. }) => {
// Ex) typing.List[...] // Ex) typing.List[...]
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::MissingFutureAnnotationsImport, Rule::MissingFutureAnnotationsImport,
Rule::NonPEP585Annotation, Rule::NonPEP585Annotation,
]) { ]) {
@ -2444,7 +2451,7 @@ where
keywords, keywords,
range: _, range: _,
}) => { }) => {
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
// pyflakes // pyflakes
Rule::StringDotFormatInvalidFormat, Rule::StringDotFormatInvalidFormat,
Rule::StringDotFormatExtraNamedArguments, Rule::StringDotFormatExtraNamedArguments,
@ -2574,16 +2581,12 @@ where
} }
// flake8-print // flake8-print
if self if self.any_enabled(&[Rule::Print, Rule::PPrint]) {
.settings
.rules
.any_enabled(&[Rule::Print, Rule::PPrint])
{
flake8_print::rules::print_call(self, func, keywords); flake8_print::rules::print_call(self, func, keywords);
} }
// flake8-bandit // flake8-bandit
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::SuspiciousPickleUsage, Rule::SuspiciousPickleUsage,
Rule::SuspiciousMarshalUsage, Rule::SuspiciousMarshalUsage,
Rule::SuspiciousInsecureHashUsage, Rule::SuspiciousInsecureHashUsage,
@ -2693,7 +2696,7 @@ where
self, func, args, keywords, self, func, args, keywords,
); );
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::SubprocessWithoutShellEqualsTrue, Rule::SubprocessWithoutShellEqualsTrue,
Rule::SubprocessPopenWithShellEqualsTrue, Rule::SubprocessPopenWithShellEqualsTrue,
Rule::CallWithShellEqualsTrue, Rule::CallWithShellEqualsTrue,
@ -2941,7 +2944,7 @@ where
} }
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::PytestRaisesWithoutException, Rule::PytestRaisesWithoutException,
Rule::PytestRaisesTooBroad, Rule::PytestRaisesTooBroad,
]) { ]) {
@ -2959,7 +2962,7 @@ where
} }
// flake8-gettext // flake8-gettext
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::FStringInGetTextFuncCall, Rule::FStringInGetTextFuncCall,
Rule::FormatInGetTextFuncCall, Rule::FormatInGetTextFuncCall,
Rule::PrintfInGetTextFuncCall, Rule::PrintfInGetTextFuncCall,
@ -2995,7 +2998,7 @@ where
} }
// flake8-use-pathlib // flake8-use-pathlib
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::OsPathAbspath, Rule::OsPathAbspath,
Rule::OsChmod, Rule::OsChmod,
Rule::OsMkdir, Rule::OsMkdir,
@ -3030,7 +3033,7 @@ where
} }
// flake8-logging-format // flake8-logging-format
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::LoggingStringFormat, Rule::LoggingStringFormat,
Rule::LoggingPercentFormat, Rule::LoggingPercentFormat,
Rule::LoggingStringConcat, Rule::LoggingStringConcat,
@ -3044,11 +3047,7 @@ where
} }
// pylint logging checker // pylint logging checker
if self if self.any_enabled(&[Rule::LoggingTooFewArgs, Rule::LoggingTooManyArgs]) {
.settings
.rules
.any_enabled(&[Rule::LoggingTooFewArgs, Rule::LoggingTooManyArgs])
{
pylint::rules::logging_call(self, func, args, keywords); pylint::rules::logging_call(self, func, args, keywords);
} }
@ -3062,7 +3061,7 @@ where
values, values,
range: _, range: _,
}) => { }) => {
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::MultiValueRepeatedKeyLiteral, Rule::MultiValueRepeatedKeyLiteral,
Rule::MultiValueRepeatedKeyVariable, Rule::MultiValueRepeatedKeyVariable,
]) { ]) {
@ -3133,7 +3132,7 @@ where
.. ..
}) = left.as_ref() }) = left.as_ref()
{ {
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::PercentFormatInvalidFormat, Rule::PercentFormatInvalidFormat,
Rule::PercentFormatExpectedMapping, Rule::PercentFormatExpectedMapping,
Rule::PercentFormatExpectedSequence, Rule::PercentFormatExpectedSequence,
@ -3322,7 +3321,7 @@ where
pycodestyle::rules::type_comparison(self, expr, ops, comparators); pycodestyle::rules::type_comparison(self, expr, ops, comparators);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::SysVersionCmpStr3, Rule::SysVersionCmpStr3,
Rule::SysVersionInfo0Eq3, Rule::SysVersionInfo0Eq3,
Rule::SysVersionInfo1CmpInt, Rule::SysVersionInfo1CmpInt,
@ -3362,7 +3361,7 @@ where
} }
if self.is_stub { if self.is_stub {
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::UnrecognizedPlatformCheck, Rule::UnrecognizedPlatformCheck,
Rule::UnrecognizedPlatformName, Rule::UnrecognizedPlatformName,
]) { ]) {
@ -4862,7 +4861,7 @@ impl<'a> Checker<'a> {
if !self.is_stub { if !self.is_stub {
// flake8-unused-arguments // flake8-unused-arguments
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::UnusedFunctionArgument, Rule::UnusedFunctionArgument,
Rule::UnusedMethodArgument, Rule::UnusedMethodArgument,
Rule::UnusedClassMethodArgument, Rule::UnusedClassMethodArgument,
@ -4907,7 +4906,7 @@ impl<'a> Checker<'a> {
fn check_dead_scopes(&mut self) { fn check_dead_scopes(&mut self) {
let enforce_typing_imports = !self.is_stub let enforce_typing_imports = !self.is_stub
&& self.settings.rules.any_enabled(&[ && self.any_enabled(&[
Rule::GlobalVariableNotAssigned, Rule::GlobalVariableNotAssigned,
Rule::RuntimeImportInTypeCheckingBlock, Rule::RuntimeImportInTypeCheckingBlock,
Rule::TypingOnlyFirstPartyImport, Rule::TypingOnlyFirstPartyImport,
@ -4916,7 +4915,7 @@ impl<'a> Checker<'a> {
]); ]);
if !(enforce_typing_imports if !(enforce_typing_imports
|| self.settings.rules.any_enabled(&[ || self.any_enabled(&[
Rule::UnusedImport, Rule::UnusedImport,
Rule::UndefinedLocalWithImportStarUsage, Rule::UndefinedLocalWithImportStarUsage,
Rule::RedefinedWhileUnused, Rule::RedefinedWhileUnused,
@ -5322,7 +5321,7 @@ impl<'a> Checker<'a> {
/// it is expected that all [`Definition`] nodes have been visited by the time, and that this /// it is expected that all [`Definition`] nodes have been visited by the time, and that this
/// method will not recurse into any other nodes. /// method will not recurse into any other nodes.
fn check_definitions(&mut self) { fn check_definitions(&mut self) {
let enforce_annotations = self.settings.rules.any_enabled(&[ let enforce_annotations = self.any_enabled(&[
Rule::MissingTypeFunctionArgument, Rule::MissingTypeFunctionArgument,
Rule::MissingTypeArgs, Rule::MissingTypeArgs,
Rule::MissingTypeKwargs, Rule::MissingTypeKwargs,
@ -5335,9 +5334,8 @@ impl<'a> Checker<'a> {
Rule::MissingReturnTypeClassMethod, Rule::MissingReturnTypeClassMethod,
Rule::AnyType, Rule::AnyType,
]); ]);
let enforce_stubs = let enforce_stubs = self.is_stub && self.any_enabled(&[Rule::DocstringInStub]);
self.is_stub && self.settings.rules.any_enabled(&[Rule::DocstringInStub]); let enforce_docstrings = self.any_enabled(&[
let enforce_docstrings = self.settings.rules.any_enabled(&[
Rule::UndocumentedPublicModule, Rule::UndocumentedPublicModule,
Rule::UndocumentedPublicClass, Rule::UndocumentedPublicClass,
Rule::UndocumentedPublicMethod, Rule::UndocumentedPublicMethod,
@ -5495,13 +5493,13 @@ impl<'a> Checker<'a> {
if self.enabled(Rule::FitsOnOneLine) { if self.enabled(Rule::FitsOnOneLine) {
pydocstyle::rules::one_liner(self, &docstring); pydocstyle::rules::one_liner(self, &docstring);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::NoBlankLineBeforeFunction, Rule::NoBlankLineBeforeFunction,
Rule::NoBlankLineAfterFunction, Rule::NoBlankLineAfterFunction,
]) { ]) {
pydocstyle::rules::blank_before_after_function(self, &docstring); pydocstyle::rules::blank_before_after_function(self, &docstring);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::OneBlankLineBeforeClass, Rule::OneBlankLineBeforeClass,
Rule::OneBlankLineAfterClass, Rule::OneBlankLineAfterClass,
Rule::BlankLineBeforeClass, Rule::BlankLineBeforeClass,
@ -5511,7 +5509,7 @@ impl<'a> Checker<'a> {
if self.enabled(Rule::BlankLineAfterSummary) { if self.enabled(Rule::BlankLineAfterSummary) {
pydocstyle::rules::blank_after_summary(self, &docstring); pydocstyle::rules::blank_after_summary(self, &docstring);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::IndentWithSpaces, Rule::IndentWithSpaces,
Rule::UnderIndentation, Rule::UnderIndentation,
Rule::OverIndentation, Rule::OverIndentation,
@ -5524,7 +5522,7 @@ impl<'a> Checker<'a> {
if self.enabled(Rule::SurroundingWhitespace) { if self.enabled(Rule::SurroundingWhitespace) {
pydocstyle::rules::no_surrounding_whitespace(self, &docstring); pydocstyle::rules::no_surrounding_whitespace(self, &docstring);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::MultiLineSummaryFirstLine, Rule::MultiLineSummaryFirstLine,
Rule::MultiLineSummarySecondLine, Rule::MultiLineSummarySecondLine,
]) { ]) {
@ -5561,7 +5559,7 @@ impl<'a> Checker<'a> {
if self.enabled(Rule::OverloadWithDocstring) { if self.enabled(Rule::OverloadWithDocstring) {
pydocstyle::rules::if_needed(self, &docstring); pydocstyle::rules::if_needed(self, &docstring);
} }
if self.settings.rules.any_enabled(&[ if self.any_enabled(&[
Rule::MultiLineSummaryFirstLine, Rule::MultiLineSummaryFirstLine,
Rule::SectionNotOverIndented, Rule::SectionNotOverIndented,
Rule::SectionUnderlineNotOverIndented, Rule::SectionUnderlineNotOverIndented,

View file

@ -710,7 +710,7 @@ pub(crate) fn function(checker: &mut Checker, body: &[Stmt], returns: Option<&Ex
return; return;
} }
if checker.settings.rules.any_enabled(&[ if checker.any_enabled(&[
Rule::SuperfluousElseReturn, Rule::SuperfluousElseReturn,
Rule::SuperfluousElseRaise, Rule::SuperfluousElseRaise,
Rule::SuperfluousElseContinue, Rule::SuperfluousElseContinue,