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

View file

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