From bfbde537af9d221f637bf10557eb24ed64ed8e07 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Fri, 10 Feb 2023 06:17:57 +0100 Subject: [PATCH] Disallow rule names starting with do-not-* --- README.md | 6 +++--- .../ruff/resources/test/disallowed_rule_names.txt | 1 + crates/ruff/src/checkers/ast.rs | 14 +++++++------- crates/ruff/src/registry.rs | 6 +++--- crates/ruff/src/rules/flake8_bugbear/mod.rs | 2 +- .../src/rules/flake8_bugbear/rules/assert_false.rs | 6 +++--- crates/ruff/src/rules/flake8_bugbear/rules/mod.rs | 2 +- ...rules__flake8_bugbear__tests__B011_B011.py.snap | 6 +++--- crates/ruff/src/rules/pycodestyle/mod.rs | 4 ++-- .../{do_not_use_bare_except.rs => bare_except.rs} | 11 ++++------- ...o_not_assign_lambda.rs => lambda_assignment.rs} | 10 +++++----- crates/ruff/src/rules/pycodestyle/rules/mod.rs | 8 ++++---- ...f__rules__pycodestyle__tests__E722_E722.py.snap | 8 ++++---- ...f__rules__pycodestyle__tests__E731_E731.py.snap | 8 ++++---- 14 files changed, 45 insertions(+), 47 deletions(-) rename crates/ruff/src/rules/pycodestyle/rules/{do_not_use_bare_except.rs => bare_except.rs} (76%) rename crates/ruff/src/rules/pycodestyle/rules/{do_not_assign_lambda.rs => lambda_assignment.rs} (89%) diff --git a/README.md b/README.md index 45a02fff61..ac953ec508 100644 --- a/README.md +++ b/README.md @@ -727,8 +727,8 @@ For more, see [pycodestyle](https://pypi.org/project/pycodestyle/) on PyPI. | E713 | not-in-test | Test for membership should be `not in` | 🛠 | | E714 | not-is-test | Test for object identity should be `is not` | 🛠 | | E721 | type-comparison | Do not compare types, use `isinstance()` | | -| E722 | do-not-use-bare-except | Do not use bare `except` | | -| E731 | do-not-assign-lambda | Do not assign a `lambda` expression, use a `def` | 🛠 | +| E722 | bare-except | Do not use bare `except` | | +| E731 | lambda-assignment | Do not assign a `lambda` expression, use a `def` | 🛠 | | E741 | ambiguous-variable-name | Ambiguous variable name: `{name}` | | | E742 | ambiguous-class-name | Ambiguous class name: `{name}` | | | E743 | ambiguous-function-name | Ambiguous function name: `{name}` | | @@ -971,7 +971,7 @@ For more, see [flake8-bugbear](https://pypi.org/project/flake8-bugbear/) on PyPI | B008 | function-call-argument-default | Do not perform function call `{name}` in argument defaults | | | B009 | get-attr-with-constant | Do not call `getattr` with a constant attribute value. It is not any safer than normal property access. | 🛠 | | B010 | set-attr-with-constant | Do not call `setattr` with a constant attribute value. It is not any safer than normal property access. | 🛠 | -| B011 | do-not-assert-false | Do not `assert False` (`python -O` removes these calls), raise `AssertionError()` | 🛠 | +| B011 | assert-false | Do not `assert False` (`python -O` removes these calls), raise `AssertionError()` | 🛠 | | B012 | jump-statement-in-finally | `{name}` inside `finally` blocks cause exceptions to be silenced | | | B013 | redundant-tuple-in-exception-handler | A length-one tuple literal is redundant. Write `except {name}` instead of `except ({name},)`. | 🛠 | | B014 | duplicate-handler-exception | Exception handler with duplicate exception: `{name}` | 🛠 | diff --git a/crates/ruff/resources/test/disallowed_rule_names.txt b/crates/ruff/resources/test/disallowed_rule_names.txt index e69de29bb2..bdcddd375c 100644 --- a/crates/ruff/resources/test/disallowed_rule_names.txt +++ b/crates/ruff/resources/test/disallowed_rule_names.txt @@ -0,0 +1 @@ +do-not-* diff --git a/crates/ruff/src/checkers/ast.rs b/crates/ruff/src/checkers/ast.rs index fb3c39e3a8..fadd3e6a03 100644 --- a/crates/ruff/src/checkers/ast.rs +++ b/crates/ruff/src/checkers/ast.rs @@ -1528,7 +1528,7 @@ where if self.settings.rules.enabled(&Rule::AssertTuple) { pyflakes::rules::assert_tuple(self, stmt, test); } - if self.settings.rules.enabled(&Rule::DoNotAssertFalse) { + if self.settings.rules.enabled(&Rule::AssertFalse) { flake8_bugbear::rules::assert_false( self, stmt, @@ -1695,9 +1695,9 @@ where } } StmtKind::Assign { targets, value, .. } => { - if self.settings.rules.enabled(&Rule::DoNotAssignLambda) { + if self.settings.rules.enabled(&Rule::LambdaAssignment) { if let [target] = &targets[..] { - pycodestyle::rules::do_not_assign_lambda(self, target, value, stmt); + pycodestyle::rules::lambda_assignment(self, target, value, stmt); } } @@ -1751,9 +1751,9 @@ where } } StmtKind::AnnAssign { target, value, .. } => { - if self.settings.rules.enabled(&Rule::DoNotAssignLambda) { + if self.settings.rules.enabled(&Rule::LambdaAssignment) { if let Some(value) = value { - pycodestyle::rules::do_not_assign_lambda(self, target, value, stmt); + pycodestyle::rules::lambda_assignment(self, target, value, stmt); } } } @@ -3533,8 +3533,8 @@ where ExcepthandlerKind::ExceptHandler { type_, name, body, .. } => { - if self.settings.rules.enabled(&Rule::DoNotUseBareExcept) { - if let Some(diagnostic) = pycodestyle::rules::do_not_use_bare_except( + if self.settings.rules.enabled(&Rule::BareExcept) { + if let Some(diagnostic) = pycodestyle::rules::bare_except( type_.as_deref(), body, excepthandler, diff --git a/crates/ruff/src/registry.rs b/crates/ruff/src/registry.rs index 17d948d70d..31a842b672 100644 --- a/crates/ruff/src/registry.rs +++ b/crates/ruff/src/registry.rs @@ -69,8 +69,8 @@ ruff_macros::define_rule_mapping!( E713 => rules::pycodestyle::rules::NotInTest, E714 => rules::pycodestyle::rules::NotIsTest, E721 => rules::pycodestyle::rules::TypeComparison, - E722 => rules::pycodestyle::rules::DoNotUseBareExcept, - E731 => rules::pycodestyle::rules::DoNotAssignLambda, + E722 => rules::pycodestyle::rules::BareExcept, + E731 => rules::pycodestyle::rules::LambdaAssignment, E741 => rules::pycodestyle::rules::AmbiguousVariableName, E742 => rules::pycodestyle::rules::AmbiguousClassName, E743 => rules::pycodestyle::rules::AmbiguousFunctionName, @@ -161,7 +161,7 @@ ruff_macros::define_rule_mapping!( B008 => rules::flake8_bugbear::rules::FunctionCallArgumentDefault, B009 => rules::flake8_bugbear::rules::GetAttrWithConstant, B010 => rules::flake8_bugbear::rules::SetAttrWithConstant, - B011 => rules::flake8_bugbear::rules::DoNotAssertFalse, + B011 => rules::flake8_bugbear::rules::AssertFalse, B012 => rules::flake8_bugbear::rules::JumpStatementInFinally, B013 => rules::flake8_bugbear::rules::RedundantTupleInExceptionHandler, B014 => rules::flake8_bugbear::rules::DuplicateHandlerException, diff --git a/crates/ruff/src/rules/flake8_bugbear/mod.rs b/crates/ruff/src/rules/flake8_bugbear/mod.rs index 4d9a182d04..512fa97f49 100644 --- a/crates/ruff/src/rules/flake8_bugbear/mod.rs +++ b/crates/ruff/src/rules/flake8_bugbear/mod.rs @@ -23,7 +23,7 @@ mod tests { #[test_case(Rule::FunctionCallArgumentDefault, Path::new("B006_B008.py"); "B008")] #[test_case(Rule::GetAttrWithConstant, Path::new("B009_B010.py"); "B009")] #[test_case(Rule::SetAttrWithConstant, Path::new("B009_B010.py"); "B010")] - #[test_case(Rule::DoNotAssertFalse, Path::new("B011.py"); "B011")] + #[test_case(Rule::AssertFalse, Path::new("B011.py"); "B011")] #[test_case(Rule::JumpStatementInFinally, Path::new("B012.py"); "B012")] #[test_case(Rule::RedundantTupleInExceptionHandler, Path::new("B013.py"); "B013")] #[test_case(Rule::DuplicateHandlerException, Path::new("B014.py"); "B014")] diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs b/crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs index 496c61269d..440b4df157 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs @@ -9,9 +9,9 @@ use crate::registry::Diagnostic; use crate::violation::AlwaysAutofixableViolation; define_violation!( - pub struct DoNotAssertFalse; + pub struct AssertFalse; ); -impl AlwaysAutofixableViolation for DoNotAssertFalse { +impl AlwaysAutofixableViolation for AssertFalse { #[derive_message_formats] fn message(&self) -> String { format!("Do not `assert False` (`python -O` removes these calls), raise `AssertionError()`") @@ -61,7 +61,7 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option return; }; - let mut diagnostic = Diagnostic::new(DoNotAssertFalse, Range::from_located(test)); + let mut diagnostic = Diagnostic::new(AssertFalse, Range::from_located(test)); if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( unparse_stmt(&assertion_error(msg), checker.stylist), diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/mod.rs b/crates/ruff/src/rules/flake8_bugbear/rules/mod.rs index d59c8ea853..a9e936115c 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/mod.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/mod.rs @@ -2,7 +2,7 @@ pub use abstract_base_class::{ abstract_base_class, AbstractBaseClassWithoutAbstractMethod, EmptyMethodWithoutAbstractDecorator, }; -pub use assert_false::{assert_false, DoNotAssertFalse}; +pub use assert_false::{assert_false, AssertFalse}; pub use assert_raises_exception::{assert_raises_exception, AssertRaisesException}; pub use assignment_to_os_environ::{assignment_to_os_environ, AssignmentToOsEnviron}; pub use cached_instance_method::{cached_instance_method, CachedInstanceMethod}; diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B011_B011.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B011_B011.py.snap index 3c5eed7a00..29801ba200 100644 --- a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B011_B011.py.snap +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B011_B011.py.snap @@ -1,9 +1,9 @@ --- -source: src/rules/flake8_bugbear/mod.rs +source: crates/ruff/src/rules/flake8_bugbear/mod.rs expression: diagnostics --- - kind: - DoNotAssertFalse: ~ + AssertFalse: ~ location: row: 8 column: 7 @@ -21,7 +21,7 @@ expression: diagnostics column: 12 parent: ~ - kind: - DoNotAssertFalse: ~ + AssertFalse: ~ location: row: 10 column: 7 diff --git a/crates/ruff/src/rules/pycodestyle/mod.rs b/crates/ruff/src/rules/pycodestyle/mod.rs index e8c4448281..b327146363 100644 --- a/crates/ruff/src/rules/pycodestyle/mod.rs +++ b/crates/ruff/src/rules/pycodestyle/mod.rs @@ -20,8 +20,8 @@ mod tests { #[test_case(Rule::AmbiguousClassName, Path::new("E742.py"))] #[test_case(Rule::AmbiguousFunctionName, Path::new("E743.py"))] #[test_case(Rule::AmbiguousVariableName, Path::new("E741.py"))] - #[test_case(Rule::DoNotAssignLambda, Path::new("E731.py"))] - #[test_case(Rule::DoNotUseBareExcept, Path::new("E722.py"))] + #[test_case(Rule::LambdaAssignment, Path::new("E731.py"))] + #[test_case(Rule::BareExcept, Path::new("E722.py"))] #[test_case(Rule::InvalidEscapeSequence, Path::new("W605_0.py"))] #[test_case(Rule::InvalidEscapeSequence, Path::new("W605_1.py"))] #[test_case(Rule::LineTooLong, Path::new("E501.py"))] diff --git a/crates/ruff/src/rules/pycodestyle/rules/do_not_use_bare_except.rs b/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs similarity index 76% rename from crates/ruff/src/rules/pycodestyle/rules/do_not_use_bare_except.rs rename to crates/ruff/src/rules/pycodestyle/rules/bare_except.rs index f0c8c37e29..8049f608f0 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/do_not_use_bare_except.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs @@ -7,9 +7,9 @@ use crate::source_code::Locator; use crate::violation::Violation; define_violation!( - pub struct DoNotUseBareExcept; + pub struct BareExcept; ); -impl Violation for DoNotUseBareExcept { +impl Violation for BareExcept { #[derive_message_formats] fn message(&self) -> String { format!("Do not use bare `except`") @@ -17,7 +17,7 @@ impl Violation for DoNotUseBareExcept { } /// E722 -pub fn do_not_use_bare_except( +pub fn bare_except( type_: Option<&Expr>, body: &[Stmt], handler: &Excepthandler, @@ -28,10 +28,7 @@ pub fn do_not_use_bare_except( .iter() .any(|stmt| matches!(stmt.node, StmtKind::Raise { exc: None, .. })) { - Some(Diagnostic::new( - DoNotUseBareExcept, - except_range(handler, locator), - )) + Some(Diagnostic::new(BareExcept, except_range(handler, locator))) } else { None } diff --git a/crates/ruff/src/rules/pycodestyle/rules/do_not_assign_lambda.rs b/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs similarity index 89% rename from crates/ruff/src/rules/pycodestyle/rules/do_not_assign_lambda.rs rename to crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs index 22ec95c1db..7fd1f51d94 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/do_not_assign_lambda.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs @@ -11,26 +11,26 @@ use crate::source_code::Stylist; use crate::violation::AlwaysAutofixableViolation; define_violation!( - pub struct DoNotAssignLambda(pub String); + pub struct LambdaAssignment(pub String); ); -impl AlwaysAutofixableViolation for DoNotAssignLambda { +impl AlwaysAutofixableViolation for LambdaAssignment { #[derive_message_formats] fn message(&self) -> String { format!("Do not assign a `lambda` expression, use a `def`") } fn autofix_title(&self) -> String { - let DoNotAssignLambda(name) = self; + let LambdaAssignment(name) = self; format!("Rewrite `{name}` as a `def`") } } /// E731 -pub fn do_not_assign_lambda(checker: &mut Checker, target: &Expr, value: &Expr, stmt: &Stmt) { +pub fn lambda_assignment(checker: &mut Checker, target: &Expr, value: &Expr, stmt: &Stmt) { if let ExprKind::Name { id, .. } = &target.node { if let ExprKind::Lambda { args, body } = &value.node { let mut diagnostic = - Diagnostic::new(DoNotAssignLambda(id.to_string()), Range::from_located(stmt)); + Diagnostic::new(LambdaAssignment(id.to_string()), Range::from_located(stmt)); if checker.patch(diagnostic.kind.rule()) { if !match_leading_content(stmt, checker.locator) && !match_trailing_content(stmt, checker.locator) diff --git a/crates/ruff/src/rules/pycodestyle/rules/mod.rs b/crates/ruff/src/rules/pycodestyle/rules/mod.rs index 16ee4d9a11..7508295813 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/mod.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/mod.rs @@ -1,12 +1,11 @@ pub use ambiguous_class_name::{ambiguous_class_name, AmbiguousClassName}; pub use ambiguous_function_name::{ambiguous_function_name, AmbiguousFunctionName}; pub use ambiguous_variable_name::{ambiguous_variable_name, AmbiguousVariableName}; +pub use bare_except::{bare_except, BareExcept}; pub use compound_statements::{ compound_statements, MultipleStatementsOnOneLineColon, MultipleStatementsOnOneLineDef, MultipleStatementsOnOneLineSemicolon, UselessSemicolon, }; -pub use do_not_assign_lambda::{do_not_assign_lambda, DoNotAssignLambda}; -pub use do_not_use_bare_except::{do_not_use_bare_except, DoNotUseBareExcept}; pub use doc_line_too_long::{doc_line_too_long, DocLineTooLong}; pub use errors::{syntax_error, IOError, SyntaxError}; pub use extraneous_whitespace::{ @@ -23,6 +22,7 @@ pub use indentation::{ UnexpectedIndentationComment, }; pub use invalid_escape_sequence::{invalid_escape_sequence, InvalidEscapeSequence}; +pub use lambda_assignment::{lambda_assignment, LambdaAssignment}; pub use line_too_long::{line_too_long, LineTooLong}; pub use literal_comparisons::{literal_comparisons, NoneComparison, TrueFalseComparison}; pub use mixed_spaces_and_tabs::{mixed_spaces_and_tabs, MixedSpacesAndTabs}; @@ -45,15 +45,15 @@ pub use whitespace_before_comment::{ mod ambiguous_class_name; mod ambiguous_function_name; mod ambiguous_variable_name; +mod bare_except; mod compound_statements; -mod do_not_assign_lambda; -mod do_not_use_bare_except; mod doc_line_too_long; mod errors; mod extraneous_whitespace; mod imports; mod indentation; mod invalid_escape_sequence; +mod lambda_assignment; mod line_too_long; mod literal_comparisons; mod mixed_spaces_and_tabs; diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E722_E722.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E722_E722.py.snap index 392f38abf2..93699e485a 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E722_E722.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E722_E722.py.snap @@ -1,9 +1,9 @@ --- -source: src/rules/pycodestyle/mod.rs +source: crates/ruff/src/rules/pycodestyle/mod.rs expression: diagnostics --- - kind: - DoNotUseBareExcept: ~ + BareExcept: ~ location: row: 4 column: 0 @@ -13,7 +13,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - DoNotUseBareExcept: ~ + BareExcept: ~ location: row: 11 column: 0 @@ -23,7 +23,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - DoNotUseBareExcept: ~ + BareExcept: ~ location: row: 16 column: 0 diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E731_E731.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E731_E731.py.snap index fb425b5648..6fbd2c96d2 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E731_E731.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E731_E731.py.snap @@ -1,9 +1,9 @@ --- -source: src/rules/pycodestyle/mod.rs +source: crates/ruff/src/rules/pycodestyle/mod.rs expression: diagnostics --- - kind: - DoNotAssignLambda: f + LambdaAssignment: f location: row: 2 column: 0 @@ -22,7 +22,7 @@ expression: diagnostics column: 19 parent: ~ - kind: - DoNotAssignLambda: f + LambdaAssignment: f location: row: 4 column: 0 @@ -41,7 +41,7 @@ expression: diagnostics column: 19 parent: ~ - kind: - DoNotAssignLambda: this + LambdaAssignment: this location: row: 7 column: 4