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

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

View file

@ -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,

View file

@ -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,

View file

@ -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")]

View file

@ -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),

View file

@ -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};

View file

@ -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

View file

@ -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"))]

View file

@ -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
}

View file

@ -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)

View file

@ -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;

View file

@ -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

View file

@ -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