Skip to content

Commit

Permalink
Address reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Jan 8, 2024
1 parent d4395af commit 87ddff6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ impl Violation for ParenthesizeChainedOperators {
#[derive_message_formats]
fn message(&self) -> String {
format!(
"Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear"
"Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear"
)
}
}

/// RUF021
pub(crate) fn parenthesize_chained_logical_operators(
checker: &mut Checker,
expr: &ast::ExprBoolOp,
Expand All @@ -56,22 +57,21 @@ pub(crate) fn parenthesize_chained_logical_operators(
// than in the superexpression:
// `a or b or c` => `BoolOp(values=[Name('a'), Name('b'), Name('c')], op=Or)`
// `a or b and c` => `BoolOp(values=[Name('a'), BoolOp(values=[Name('b'), Name('c')], op=And)], op=Or)`
ast::Expr::BoolOp(
bool_op @ ast::ExprBoolOp {
op: ast::BoolOp::And,
..
},
) => {
ast::Expr::BoolOp(bool_op) => {
// `and` binds more tightly than `or`, and the AST reflects this:
// the BoolOp subexpression *must*, logically, be an `And`
// (and the superexpression *must*, logically, be an `Or`)
assert!(bool_op.op.is_and());
if parenthesized_range(
ast::ExpressionRef::BoolOp(bool_op),
ast::AnyNodeRef::ExprBoolOp(expr),
bool_op.into(),
expr.into(),
checker.indexer().comment_ranges(),
checker.locator().contents(),
)
.is_none()
{
checker.diagnostics.push(Diagnostic::new(
ParenthesizeChainedOperators {},
ParenthesizeChainedOperators,
bool_op.range(),
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---
RUF021.py:12:10: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:12:10: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
11 | a, b, c = 1, 0, 2
12 | x = a or b and c # RUF021: => `a or (b and c)`
Expand All @@ -10,7 +10,7 @@ RUF021.py:12:10: RUF021 Parenthesize `foo and bar` expressions when chaining `an
14 | a, b, c = 0, 1, 2
|

RUF021.py:15:5: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:15:5: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
14 | a, b, c = 0, 1, 2
15 | y = a and b or c # RUF021: => `(a and b) or c`
Expand All @@ -19,15 +19,15 @@ RUF021.py:15:5: RUF021 Parenthesize `foo and bar` expressions when chaining `and
17 | a, b, c, d = 1, 2, 0, 3
|

RUF021.py:18:14: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:18:14: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
17 | a, b, c, d = 1, 2, 0, 3
18 | if a or b or c and d: # RUF021: => `a or b or (c and d)`
| ^^^^^^^ RUF021
19 | pass
|

RUF021.py:25:11: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:25:11: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
23 | if bool():
24 | pass
Expand All @@ -36,23 +36,23 @@ RUF021.py:25:11: RUF021 Parenthesize `foo and bar` expressions when chaining `an
26 | pass
|

RUF021.py:29:7: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:29:7: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
28 | a, b, c, d = 0, 1, 0, 2
29 | while a and b or c and d: # RUF021: => `(and b) or (c and d)`
| ^^^^^^^ RUF021
30 | pass
|

RUF021.py:29:18: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:29:18: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
28 | a, b, c, d = 0, 1, 0, 2
29 | while a and b or c and d: # RUF021: => `(and b) or (c and d)`
| ^^^^^^^ RUF021
30 | pass
|

RUF021.py:33:44: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:33:44: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
32 | b, c, d, e = 2, 3, 0, 4
33 | z = [a for a in range(5) if a or b or c or d and e] # RUF021: => `a or b or c or (d and e)`
Expand All @@ -61,7 +61,7 @@ RUF021.py:33:44: RUF021 Parenthesize `foo and bar` expressions when chaining `an
35 | a, b, c, d = 0, 1, 3, 0
|

RUF021.py:36:8: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:36:8: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
35 | a, b, c, d = 0, 1, 3, 0
36 | assert not a and b or c or d # RUF021: => `(not a and b) or c or d`
Expand All @@ -70,7 +70,7 @@ RUF021.py:36:8: RUF021 Parenthesize `foo and bar` expressions when chaining `and
38 | if (not a) and b or c or d: # RUF021: => `((not a) and b) or c or d`
|

RUF021.py:38:4: RUF021 Parenthesize `foo and bar` expressions when chaining `and` and `or` together, to make the precedence clear
RUF021.py:38:4: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
|
36 | assert not a and b or c or d # RUF021: => `(not a and b) or c or d`
37 |
Expand Down

0 comments on commit 87ddff6

Please sign in to comment.