Disallow rule names starting with do-not-*

This commit is contained in:
Martin Fischer 2023-02-10 06:17:57 +01:00 committed by Charlie Marsh
parent cba91b758b
commit bfbde537af
14 changed files with 45 additions and 47 deletions

View File

@ -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` | 🛠 | | E713 | not-in-test | Test for membership should be `not in` | 🛠 |
| E714 | not-is-test | Test for object identity should be `is not` | 🛠 | | E714 | not-is-test | Test for object identity should be `is not` | 🛠 |
| E721 | type-comparison | Do not compare types, use `isinstance()` | | | E721 | type-comparison | Do not compare types, use `isinstance()` | |
| E722 | do-not-use-bare-except | Do not use bare `except` | | | E722 | bare-except | Do not use bare `except` | |
| E731 | do-not-assign-lambda | Do not assign a `lambda` expression, use a `def` | 🛠 | | E731 | lambda-assignment | Do not assign a `lambda` expression, use a `def` | 🛠 |
| E741 | ambiguous-variable-name | Ambiguous variable name: `{name}` | | | E741 | ambiguous-variable-name | Ambiguous variable name: `{name}` | |
| E742 | ambiguous-class-name | Ambiguous class name: `{name}` | | | E742 | ambiguous-class-name | Ambiguous class name: `{name}` | |
| E743 | ambiguous-function-name | Ambiguous function 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 | | | 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. | 🛠 | | 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. | 🛠 | | 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 | | | 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},)`. | 🛠 | | 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}` | 🛠 | | B014 | duplicate-handler-exception | Exception handler with duplicate exception: `{name}` | 🛠 |

View File

@ -0,0 +1 @@
do-not-*

View File

@ -1528,7 +1528,7 @@ where
if self.settings.rules.enabled(&Rule::AssertTuple) { if self.settings.rules.enabled(&Rule::AssertTuple) {
pyflakes::rules::assert_tuple(self, stmt, test); 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( flake8_bugbear::rules::assert_false(
self, self,
stmt, stmt,
@ -1695,9 +1695,9 @@ where
} }
} }
StmtKind::Assign { targets, value, .. } => { StmtKind::Assign { targets, value, .. } => {
if self.settings.rules.enabled(&Rule::DoNotAssignLambda) { if self.settings.rules.enabled(&Rule::LambdaAssignment) {
if let [target] = &targets[..] { 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, .. } => { StmtKind::AnnAssign { target, value, .. } => {
if self.settings.rules.enabled(&Rule::DoNotAssignLambda) { if self.settings.rules.enabled(&Rule::LambdaAssignment) {
if let Some(value) = value { 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 { ExcepthandlerKind::ExceptHandler {
type_, name, body, .. type_, name, body, ..
} => { } => {
if self.settings.rules.enabled(&Rule::DoNotUseBareExcept) { if self.settings.rules.enabled(&Rule::BareExcept) {
if let Some(diagnostic) = pycodestyle::rules::do_not_use_bare_except( if let Some(diagnostic) = pycodestyle::rules::bare_except(
type_.as_deref(), type_.as_deref(),
body, body,
excepthandler, excepthandler,

View File

@ -69,8 +69,8 @@ ruff_macros::define_rule_mapping!(
E713 => rules::pycodestyle::rules::NotInTest, E713 => rules::pycodestyle::rules::NotInTest,
E714 => rules::pycodestyle::rules::NotIsTest, E714 => rules::pycodestyle::rules::NotIsTest,
E721 => rules::pycodestyle::rules::TypeComparison, E721 => rules::pycodestyle::rules::TypeComparison,
E722 => rules::pycodestyle::rules::DoNotUseBareExcept, E722 => rules::pycodestyle::rules::BareExcept,
E731 => rules::pycodestyle::rules::DoNotAssignLambda, E731 => rules::pycodestyle::rules::LambdaAssignment,
E741 => rules::pycodestyle::rules::AmbiguousVariableName, E741 => rules::pycodestyle::rules::AmbiguousVariableName,
E742 => rules::pycodestyle::rules::AmbiguousClassName, E742 => rules::pycodestyle::rules::AmbiguousClassName,
E743 => rules::pycodestyle::rules::AmbiguousFunctionName, E743 => rules::pycodestyle::rules::AmbiguousFunctionName,
@ -161,7 +161,7 @@ ruff_macros::define_rule_mapping!(
B008 => rules::flake8_bugbear::rules::FunctionCallArgumentDefault, B008 => rules::flake8_bugbear::rules::FunctionCallArgumentDefault,
B009 => rules::flake8_bugbear::rules::GetAttrWithConstant, B009 => rules::flake8_bugbear::rules::GetAttrWithConstant,
B010 => rules::flake8_bugbear::rules::SetAttrWithConstant, B010 => rules::flake8_bugbear::rules::SetAttrWithConstant,
B011 => rules::flake8_bugbear::rules::DoNotAssertFalse, B011 => rules::flake8_bugbear::rules::AssertFalse,
B012 => rules::flake8_bugbear::rules::JumpStatementInFinally, B012 => rules::flake8_bugbear::rules::JumpStatementInFinally,
B013 => rules::flake8_bugbear::rules::RedundantTupleInExceptionHandler, B013 => rules::flake8_bugbear::rules::RedundantTupleInExceptionHandler,
B014 => rules::flake8_bugbear::rules::DuplicateHandlerException, B014 => rules::flake8_bugbear::rules::DuplicateHandlerException,

View File

@ -23,7 +23,7 @@ mod tests {
#[test_case(Rule::FunctionCallArgumentDefault, Path::new("B006_B008.py"); "B008")] #[test_case(Rule::FunctionCallArgumentDefault, Path::new("B006_B008.py"); "B008")]
#[test_case(Rule::GetAttrWithConstant, Path::new("B009_B010.py"); "B009")] #[test_case(Rule::GetAttrWithConstant, Path::new("B009_B010.py"); "B009")]
#[test_case(Rule::SetAttrWithConstant, Path::new("B009_B010.py"); "B010")] #[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::JumpStatementInFinally, Path::new("B012.py"); "B012")]
#[test_case(Rule::RedundantTupleInExceptionHandler, Path::new("B013.py"); "B013")] #[test_case(Rule::RedundantTupleInExceptionHandler, Path::new("B013.py"); "B013")]
#[test_case(Rule::DuplicateHandlerException, Path::new("B014.py"); "B014")] #[test_case(Rule::DuplicateHandlerException, Path::new("B014.py"); "B014")]

View File

@ -9,9 +9,9 @@ use crate::registry::Diagnostic;
use crate::violation::AlwaysAutofixableViolation; use crate::violation::AlwaysAutofixableViolation;
define_violation!( define_violation!(
pub struct DoNotAssertFalse; pub struct AssertFalse;
); );
impl AlwaysAutofixableViolation for DoNotAssertFalse { impl AlwaysAutofixableViolation for AssertFalse {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
format!("Do not `assert False` (`python -O` removes these calls), raise `AssertionError()`") 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; 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()) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
unparse_stmt(&assertion_error(msg), checker.stylist), unparse_stmt(&assertion_error(msg), checker.stylist),

View File

@ -2,7 +2,7 @@ pub use abstract_base_class::{
abstract_base_class, AbstractBaseClassWithoutAbstractMethod, abstract_base_class, AbstractBaseClassWithoutAbstractMethod,
EmptyMethodWithoutAbstractDecorator, 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 assert_raises_exception::{assert_raises_exception, AssertRaisesException};
pub use assignment_to_os_environ::{assignment_to_os_environ, AssignmentToOsEnviron}; pub use assignment_to_os_environ::{assignment_to_os_environ, AssignmentToOsEnviron};
pub use cached_instance_method::{cached_instance_method, CachedInstanceMethod}; pub use cached_instance_method::{cached_instance_method, CachedInstanceMethod};

View File

@ -1,9 +1,9 @@
--- ---
source: src/rules/flake8_bugbear/mod.rs source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics expression: diagnostics
--- ---
- kind: - kind:
DoNotAssertFalse: ~ AssertFalse: ~
location: location:
row: 8 row: 8
column: 7 column: 7
@ -21,7 +21,7 @@ expression: diagnostics
column: 12 column: 12
parent: ~ parent: ~
- kind: - kind:
DoNotAssertFalse: ~ AssertFalse: ~
location: location:
row: 10 row: 10
column: 7 column: 7

View File

@ -20,8 +20,8 @@ mod tests {
#[test_case(Rule::AmbiguousClassName, Path::new("E742.py"))] #[test_case(Rule::AmbiguousClassName, Path::new("E742.py"))]
#[test_case(Rule::AmbiguousFunctionName, Path::new("E743.py"))] #[test_case(Rule::AmbiguousFunctionName, Path::new("E743.py"))]
#[test_case(Rule::AmbiguousVariableName, Path::new("E741.py"))] #[test_case(Rule::AmbiguousVariableName, Path::new("E741.py"))]
#[test_case(Rule::DoNotAssignLambda, Path::new("E731.py"))] #[test_case(Rule::LambdaAssignment, Path::new("E731.py"))]
#[test_case(Rule::DoNotUseBareExcept, Path::new("E722.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_0.py"))]
#[test_case(Rule::InvalidEscapeSequence, Path::new("W605_1.py"))] #[test_case(Rule::InvalidEscapeSequence, Path::new("W605_1.py"))]
#[test_case(Rule::LineTooLong, Path::new("E501.py"))] #[test_case(Rule::LineTooLong, Path::new("E501.py"))]

View File

@ -7,9 +7,9 @@ use crate::source_code::Locator;
use crate::violation::Violation; use crate::violation::Violation;
define_violation!( define_violation!(
pub struct DoNotUseBareExcept; pub struct BareExcept;
); );
impl Violation for DoNotUseBareExcept { impl Violation for BareExcept {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
format!("Do not use bare `except`") format!("Do not use bare `except`")
@ -17,7 +17,7 @@ impl Violation for DoNotUseBareExcept {
} }
/// E722 /// E722
pub fn do_not_use_bare_except( pub fn bare_except(
type_: Option<&Expr>, type_: Option<&Expr>,
body: &[Stmt], body: &[Stmt],
handler: &Excepthandler, handler: &Excepthandler,
@ -28,10 +28,7 @@ pub fn do_not_use_bare_except(
.iter() .iter()
.any(|stmt| matches!(stmt.node, StmtKind::Raise { exc: None, .. })) .any(|stmt| matches!(stmt.node, StmtKind::Raise { exc: None, .. }))
{ {
Some(Diagnostic::new( Some(Diagnostic::new(BareExcept, except_range(handler, locator)))
DoNotUseBareExcept,
except_range(handler, locator),
))
} else { } else {
None None
} }

View File

@ -11,26 +11,26 @@ use crate::source_code::Stylist;
use crate::violation::AlwaysAutofixableViolation; use crate::violation::AlwaysAutofixableViolation;
define_violation!( define_violation!(
pub struct DoNotAssignLambda(pub String); pub struct LambdaAssignment(pub String);
); );
impl AlwaysAutofixableViolation for DoNotAssignLambda { impl AlwaysAutofixableViolation for LambdaAssignment {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
format!("Do not assign a `lambda` expression, use a `def`") format!("Do not assign a `lambda` expression, use a `def`")
} }
fn autofix_title(&self) -> String { fn autofix_title(&self) -> String {
let DoNotAssignLambda(name) = self; let LambdaAssignment(name) = self;
format!("Rewrite `{name}` as a `def`") format!("Rewrite `{name}` as a `def`")
} }
} }
/// E731 /// 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::Name { id, .. } = &target.node {
if let ExprKind::Lambda { args, body } = &value.node { if let ExprKind::Lambda { args, body } = &value.node {
let mut diagnostic = 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 checker.patch(diagnostic.kind.rule()) {
if !match_leading_content(stmt, checker.locator) if !match_leading_content(stmt, checker.locator)
&& !match_trailing_content(stmt, checker.locator) && !match_trailing_content(stmt, checker.locator)

View File

@ -1,12 +1,11 @@
pub use ambiguous_class_name::{ambiguous_class_name, AmbiguousClassName}; pub use ambiguous_class_name::{ambiguous_class_name, AmbiguousClassName};
pub use ambiguous_function_name::{ambiguous_function_name, AmbiguousFunctionName}; pub use ambiguous_function_name::{ambiguous_function_name, AmbiguousFunctionName};
pub use ambiguous_variable_name::{ambiguous_variable_name, AmbiguousVariableName}; pub use ambiguous_variable_name::{ambiguous_variable_name, AmbiguousVariableName};
pub use bare_except::{bare_except, BareExcept};
pub use compound_statements::{ pub use compound_statements::{
compound_statements, MultipleStatementsOnOneLineColon, MultipleStatementsOnOneLineDef, compound_statements, MultipleStatementsOnOneLineColon, MultipleStatementsOnOneLineDef,
MultipleStatementsOnOneLineSemicolon, UselessSemicolon, 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 doc_line_too_long::{doc_line_too_long, DocLineTooLong};
pub use errors::{syntax_error, IOError, SyntaxError}; pub use errors::{syntax_error, IOError, SyntaxError};
pub use extraneous_whitespace::{ pub use extraneous_whitespace::{
@ -23,6 +22,7 @@ pub use indentation::{
UnexpectedIndentationComment, UnexpectedIndentationComment,
}; };
pub use invalid_escape_sequence::{invalid_escape_sequence, InvalidEscapeSequence}; 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 line_too_long::{line_too_long, LineTooLong};
pub use literal_comparisons::{literal_comparisons, NoneComparison, TrueFalseComparison}; pub use literal_comparisons::{literal_comparisons, NoneComparison, TrueFalseComparison};
pub use mixed_spaces_and_tabs::{mixed_spaces_and_tabs, MixedSpacesAndTabs}; 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_class_name;
mod ambiguous_function_name; mod ambiguous_function_name;
mod ambiguous_variable_name; mod ambiguous_variable_name;
mod bare_except;
mod compound_statements; mod compound_statements;
mod do_not_assign_lambda;
mod do_not_use_bare_except;
mod doc_line_too_long; mod doc_line_too_long;
mod errors; mod errors;
mod extraneous_whitespace; mod extraneous_whitespace;
mod imports; mod imports;
mod indentation; mod indentation;
mod invalid_escape_sequence; mod invalid_escape_sequence;
mod lambda_assignment;
mod line_too_long; mod line_too_long;
mod literal_comparisons; mod literal_comparisons;
mod mixed_spaces_and_tabs; mod mixed_spaces_and_tabs;

View File

@ -1,9 +1,9 @@
--- ---
source: src/rules/pycodestyle/mod.rs source: crates/ruff/src/rules/pycodestyle/mod.rs
expression: diagnostics expression: diagnostics
--- ---
- kind: - kind:
DoNotUseBareExcept: ~ BareExcept: ~
location: location:
row: 4 row: 4
column: 0 column: 0
@ -13,7 +13,7 @@ expression: diagnostics
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
DoNotUseBareExcept: ~ BareExcept: ~
location: location:
row: 11 row: 11
column: 0 column: 0
@ -23,7 +23,7 @@ expression: diagnostics
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
DoNotUseBareExcept: ~ BareExcept: ~
location: location:
row: 16 row: 16
column: 0 column: 0

View File

@ -1,9 +1,9 @@
--- ---
source: src/rules/pycodestyle/mod.rs source: crates/ruff/src/rules/pycodestyle/mod.rs
expression: diagnostics expression: diagnostics
--- ---
- kind: - kind:
DoNotAssignLambda: f LambdaAssignment: f
location: location:
row: 2 row: 2
column: 0 column: 0
@ -22,7 +22,7 @@ expression: diagnostics
column: 19 column: 19
parent: ~ parent: ~
- kind: - kind:
DoNotAssignLambda: f LambdaAssignment: f
location: location:
row: 4 row: 4
column: 0 column: 0
@ -41,7 +41,7 @@ expression: diagnostics
column: 19 column: 19
parent: ~ parent: ~
- kind: - kind:
DoNotAssignLambda: this LambdaAssignment: this
location: location:
row: 7 row: 7
column: 4 column: 4