Skip to content

Commit

Permalink
Rename some flake8-simplify rules (#2915)
Browse files Browse the repository at this point in the history
Renames the following rules that stood out to me at a glance as needing better names:

- `or-true` to `expr-or-true`
- `and-false` to `expr-and-false`
- `a-or-not-a` to `expr-or-not-expr`
- `a-and-not-a` to `expr-and-not-expr`

Related to #2902.
  • Loading branch information
ngnpope authored Feb 26, 2023
1 parent bc79f54 commit 994e2e0
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 55 deletions.
18 changes: 9 additions & 9 deletions crates/ruff/src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ where
);
}
if self.settings.rules.enabled(&Rule::NeedlessBool) {
flake8_simplify::rules::return_bool_condition_directly(self, stmt);
flake8_simplify::rules::needless_bool(self, stmt);
}
if self.settings.rules.enabled(&Rule::ManualDictLookup) {
flake8_simplify::rules::manual_dict_lookup(self, stmt, test, body, orelse);
Expand Down Expand Up @@ -3454,17 +3454,17 @@ where
if self.settings.rules.enabled(&Rule::CompareWithTuple) {
flake8_simplify::rules::compare_with_tuple(self, expr);
}
if self.settings.rules.enabled(&Rule::AAndNotA) {
flake8_simplify::rules::a_and_not_a(self, expr);
if self.settings.rules.enabled(&Rule::ExprAndNotExpr) {
flake8_simplify::rules::expr_and_not_expr(self, expr);
}
if self.settings.rules.enabled(&Rule::AOrNotA) {
flake8_simplify::rules::a_or_not_a(self, expr);
if self.settings.rules.enabled(&Rule::ExprOrNotExpr) {
flake8_simplify::rules::expr_or_not_expr(self, expr);
}
if self.settings.rules.enabled(&Rule::OrTrue) {
flake8_simplify::rules::or_true(self, expr);
if self.settings.rules.enabled(&Rule::ExprOrTrue) {
flake8_simplify::rules::expr_or_true(self, expr);
}
if self.settings.rules.enabled(&Rule::AndFalse) {
flake8_simplify::rules::and_false(self, expr);
if self.settings.rules.enabled(&Rule::ExprAndFalse) {
flake8_simplify::rules::expr_and_false(self, expr);
}
}
_ => {}
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,10 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<Rule> {
(Flake8Simplify, "210") => Rule::IfExprWithTrueFalse,
(Flake8Simplify, "211") => Rule::IfExprWithFalseTrue,
(Flake8Simplify, "212") => Rule::IfExprWithTwistedArms,
(Flake8Simplify, "220") => Rule::AAndNotA,
(Flake8Simplify, "221") => Rule::AOrNotA,
(Flake8Simplify, "222") => Rule::OrTrue,
(Flake8Simplify, "223") => Rule::AndFalse,
(Flake8Simplify, "220") => Rule::ExprAndNotExpr,
(Flake8Simplify, "221") => Rule::ExprOrNotExpr,
(Flake8Simplify, "222") => Rule::ExprOrTrue,
(Flake8Simplify, "223") => Rule::ExprAndFalse,
(Flake8Simplify, "300") => Rule::YodaConditions,
(Flake8Simplify, "401") => Rule::DictGetWithDefault,

Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ ruff_macros::register_rules!(
rules::flake8_simplify::rules::IfExprWithTrueFalse,
rules::flake8_simplify::rules::IfExprWithFalseTrue,
rules::flake8_simplify::rules::IfExprWithTwistedArms,
rules::flake8_simplify::rules::AAndNotA,
rules::flake8_simplify::rules::AOrNotA,
rules::flake8_simplify::rules::OrTrue,
rules::flake8_simplify::rules::AndFalse,
rules::flake8_simplify::rules::ExprAndNotExpr,
rules::flake8_simplify::rules::ExprOrNotExpr,
rules::flake8_simplify::rules::ExprOrTrue,
rules::flake8_simplify::rules::ExprAndFalse,
rules::flake8_simplify::rules::YodaConditions,
rules::flake8_simplify::rules::DictGetWithDefault,
// pyupgrade
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/rules/flake8_simplify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ mod tests {
#[test_case(Rule::IfExprWithTrueFalse, Path::new("SIM210.py"); "SIM210")]
#[test_case(Rule::IfExprWithFalseTrue, Path::new("SIM211.py"); "SIM211")]
#[test_case(Rule::IfExprWithTwistedArms, Path::new("SIM212.py"); "SIM212")]
#[test_case(Rule::AAndNotA, Path::new("SIM220.py"); "SIM220")]
#[test_case(Rule::AOrNotA, Path::new("SIM221.py"); "SIM221")]
#[test_case(Rule::OrTrue, Path::new("SIM222.py"); "SIM222")]
#[test_case(Rule::AndFalse, Path::new("SIM223.py"); "SIM223")]
#[test_case(Rule::ExprAndNotExpr, Path::new("SIM220.py"); "SIM220")]
#[test_case(Rule::ExprOrNotExpr, Path::new("SIM221.py"); "SIM221")]
#[test_case(Rule::ExprOrTrue, Path::new("SIM222.py"); "SIM222")]
#[test_case(Rule::ExprAndFalse, Path::new("SIM223.py"); "SIM223")]
#[test_case(Rule::YodaConditions, Path::new("SIM300.py"); "SIM300")]
#[test_case(Rule::DictGetWithDefault, Path::new("SIM401.py"); "SIM401")]
#[test_case(Rule::ManualDictLookup, Path::new("SIM116.py"); "SIM116")]
Expand Down
36 changes: 18 additions & 18 deletions crates/ruff/src/rules/flake8_simplify/rules/ast_bool_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ impl AlwaysAutofixableViolation for CompareWithTuple {
}

define_violation!(
pub struct AAndNotA {
pub struct ExprAndNotExpr {
pub name: String,
}
);
impl AlwaysAutofixableViolation for AAndNotA {
impl AlwaysAutofixableViolation for ExprAndNotExpr {
#[derive_message_formats]
fn message(&self) -> String {
let AAndNotA { name } = self;
let ExprAndNotExpr { name } = self;
format!("Use `False` instead of `{name} and not {name}`")
}

Expand All @@ -93,14 +93,14 @@ impl AlwaysAutofixableViolation for AAndNotA {
}

define_violation!(
pub struct AOrNotA {
pub struct ExprOrNotExpr {
pub name: String,
}
);
impl AlwaysAutofixableViolation for AOrNotA {
impl AlwaysAutofixableViolation for ExprOrNotExpr {
#[derive_message_formats]
fn message(&self) -> String {
let AOrNotA { name } = self;
let ExprOrNotExpr { name } = self;
format!("Use `True` instead of `{name} or not {name}`")
}

Expand All @@ -110,9 +110,9 @@ impl AlwaysAutofixableViolation for AOrNotA {
}

define_violation!(
pub struct OrTrue;
pub struct ExprOrTrue;
);
impl AlwaysAutofixableViolation for OrTrue {
impl AlwaysAutofixableViolation for ExprOrTrue {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use `True` instead of `... or True`")
Expand All @@ -124,9 +124,9 @@ impl AlwaysAutofixableViolation for OrTrue {
}

define_violation!(
pub struct AndFalse;
pub struct ExprAndFalse;
);
impl AlwaysAutofixableViolation for AndFalse {
impl AlwaysAutofixableViolation for ExprAndFalse {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use `False` instead of `... and False`")
Expand Down Expand Up @@ -369,7 +369,7 @@ pub fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
}

/// SIM220
pub fn a_and_not_a(checker: &mut Checker, expr: &Expr) {
pub fn expr_and_not_expr(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::And, values, } = &expr.node else {
return;
};
Expand Down Expand Up @@ -404,7 +404,7 @@ pub fn a_and_not_a(checker: &mut Checker, expr: &Expr) {
for non_negate_expr in &non_negated_expr {
if let Some(id) = is_same_expr(negate_expr, non_negate_expr) {
let mut diagnostic = Diagnostic::new(
AAndNotA {
ExprAndNotExpr {
name: id.to_string(),
},
Range::from_located(expr),
Expand All @@ -423,7 +423,7 @@ pub fn a_and_not_a(checker: &mut Checker, expr: &Expr) {
}

/// SIM221
pub fn a_or_not_a(checker: &mut Checker, expr: &Expr) {
pub fn expr_or_not_expr(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::Or, values, } = &expr.node else {
return;
};
Expand Down Expand Up @@ -458,7 +458,7 @@ pub fn a_or_not_a(checker: &mut Checker, expr: &Expr) {
for non_negate_expr in &non_negated_expr {
if let Some(id) = is_same_expr(negate_expr, non_negate_expr) {
let mut diagnostic = Diagnostic::new(
AOrNotA {
ExprOrNotExpr {
name: id.to_string(),
},
Range::from_located(expr),
Expand All @@ -477,7 +477,7 @@ pub fn a_or_not_a(checker: &mut Checker, expr: &Expr) {
}

/// SIM222
pub fn or_true(checker: &mut Checker, expr: &Expr) {
pub fn expr_or_true(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::Or, values, } = &expr.node else {
return;
};
Expand All @@ -490,7 +490,7 @@ pub fn or_true(checker: &mut Checker, expr: &Expr) {
..
} = &value.node
{
let mut diagnostic = Diagnostic::new(OrTrue, Range::from_located(value));
let mut diagnostic = Diagnostic::new(ExprOrTrue, Range::from_located(value));
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
"True".to_string(),
Expand All @@ -504,7 +504,7 @@ pub fn or_true(checker: &mut Checker, expr: &Expr) {
}

/// SIM223
pub fn and_false(checker: &mut Checker, expr: &Expr) {
pub fn expr_and_false(checker: &mut Checker, expr: &Expr) {
let ExprKind::BoolOp { op: Boolop::And, values, } = &expr.node else {
return;
};
Expand All @@ -517,7 +517,7 @@ pub fn and_false(checker: &mut Checker, expr: &Expr) {
..
} = &value.node
{
let mut diagnostic = Diagnostic::new(AndFalse, Range::from_located(value));
let mut diagnostic = Diagnostic::new(ExprAndFalse, Range::from_located(value));
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
"False".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ fn is_one_line_return_bool(stmts: &[Stmt]) -> Option<Bool> {
}

/// SIM103
pub fn return_bool_condition_directly(checker: &mut Checker, stmt: &Stmt) {
pub fn needless_bool(checker: &mut Checker, stmt: &Stmt) {
let StmtKind::If { test, body, orelse } = &stmt.node else {
return;
};
Expand Down
7 changes: 4 additions & 3 deletions crates/ruff/src/rules/flake8_simplify/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
pub use ast_bool_op::{
a_and_not_a, a_or_not_a, and_false, compare_with_tuple, duplicate_isinstance_call, or_true,
AAndNotA, AOrNotA, AndFalse, CompareWithTuple, DuplicateIsinstanceCall, OrTrue,
compare_with_tuple, duplicate_isinstance_call, expr_and_false, expr_and_not_expr,
expr_or_not_expr, expr_or_true, CompareWithTuple, DuplicateIsinstanceCall, ExprAndFalse,
ExprAndNotExpr, ExprOrNotExpr, ExprOrTrue,
};
pub use ast_expr::{use_capital_environment_variables, UseCapitalEnvironmentVariables};
pub use ast_if::{
if_with_same_arms, manual_dict_lookup, nested_if_statements, return_bool_condition_directly,
if_with_same_arms, manual_dict_lookup, needless_bool, nested_if_statements,
use_dict_get_with_default, use_ternary_operator, CollapsibleIf, DictGetWithDefault,
IfWithSameArms, ManualDictLookup, NeedlessBool, UseTernaryOperator,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_simplify/mod.rs
expression: diagnostics
---
- kind:
AAndNotA:
ExprAndNotExpr:
name: a
location:
row: 1
Expand All @@ -21,7 +21,7 @@ expression: diagnostics
column: 14
parent: ~
- kind:
AAndNotA:
ExprAndNotExpr:
name: a
location:
row: 4
Expand All @@ -39,7 +39,7 @@ expression: diagnostics
column: 15
parent: ~
- kind:
AAndNotA:
ExprAndNotExpr:
name: a
location:
row: 7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_simplify/mod.rs
expression: diagnostics
---
- kind:
AOrNotA:
ExprOrNotExpr:
name: a
location:
row: 1
Expand All @@ -21,7 +21,7 @@ expression: diagnostics
column: 13
parent: ~
- kind:
AOrNotA:
ExprOrNotExpr:
name: a
location:
row: 4
Expand All @@ -39,7 +39,7 @@ expression: diagnostics
column: 14
parent: ~
- kind:
AOrNotA:
ExprOrNotExpr:
name: a
location:
row: 7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_simplify/mod.rs
expression: diagnostics
---
- kind:
OrTrue: ~
ExprOrTrue: ~
location:
row: 1
column: 8
Expand All @@ -20,7 +20,7 @@ expression: diagnostics
column: 12
parent: ~
- kind:
OrTrue: ~
ExprOrTrue: ~
location:
row: 4
column: 15
Expand All @@ -37,7 +37,7 @@ expression: diagnostics
column: 19
parent: ~
- kind:
OrTrue: ~
ExprOrTrue: ~
location:
row: 7
column: 14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_simplify/mod.rs
expression: diagnostics
---
- kind:
AndFalse: ~
ExprAndFalse: ~
location:
row: 1
column: 9
Expand All @@ -20,7 +20,7 @@ expression: diagnostics
column: 14
parent: ~
- kind:
AndFalse: ~
ExprAndFalse: ~
location:
row: 4
column: 16
Expand All @@ -37,7 +37,7 @@ expression: diagnostics
column: 21
parent: ~
- kind:
AndFalse: ~
ExprAndFalse: ~
location:
row: 7
column: 15
Expand Down

0 comments on commit 994e2e0

Please sign in to comment.