---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---
RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:12:10
   |
11 | a, b, c = 1, 0, 2
12 | x = a or b and c  # RUF021: => `a or (b and c)`
   |          ^^^^^^^
13 | x = a or b and c  # looooooooooooooooooooooooooooooong comment but it won't prevent an autofix
   |
help: Parenthesize the `and` subexpression
   |
11 | a, b, c = 1, 0, 2
   - x = a or b and c  # RUF021: => `a or (b and c)`
12 + x = a or (b and c)  # RUF021: => `a or (b and c)`
13 | x = a or b and c  # looooooooooooooooooooooooooooooong comment but it won't prevent an autofix
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:13:10
   |
11 | a, b, c = 1, 0, 2
12 | x = a or b and c  # RUF021: => `a or (b and c)`
13 | x = a or b and c  # looooooooooooooooooooooooooooooong comment but it won't prevent an autofix
   |          ^^^^^^^
14 |
15 | a, b, c = 0, 1, 2
   |
help: Parenthesize the `and` subexpression
   |
12 | x = a or b and c  # RUF021: => `a or (b and c)`
   - x = a or b and c  # looooooooooooooooooooooooooooooong comment but it won't prevent an autofix
13 + x = a or (b and c)  # looooooooooooooooooooooooooooooong comment but it won't prevent an autofix
14 |
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:16:5
   |
15 | a, b, c = 0, 1, 2
16 | y = a and b or c  # RUF021: => `(a and b) or c`
   |     ^^^^^^^
17 |
18 | a, b, c, d = 1, 2, 0, 3
   |
help: Parenthesize the `and` subexpression
   |
15 | a, b, c = 0, 1, 2
   - y = a and b or c  # RUF021: => `(a and b) or c`
16 + y = (a and b) or c  # RUF021: => `(a and b) or c`
17 |
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:19:14
   |
18 | a, b, c, d = 1, 2, 0, 3
19 | if a or b or c and d:  # RUF021: => `a or b or (c and d)`
   |              ^^^^^^^
20 |     pass
   |
help: Parenthesize the `and` subexpression
   |
18 | a, b, c, d = 1, 2, 0, 3
   - if a or b or c and d:  # RUF021: => `a or b or (c and d)`
19 + if a or b or (c and d):  # RUF021: => `a or b or (c and d)`
20 |     pass
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:26:11
   |
24 | if bool():
25 |     pass
26 | elif a or b and c or d:  # RUF021: => `a or (b and c) or d`
   |           ^^^^^^^
27 |     pass
   |
help: Parenthesize the `and` subexpression
   |
25 |     pass
   - elif a or b and c or d:  # RUF021: => `a or (b and c) or d`
26 + elif a or (b and c) or d:  # RUF021: => `a or (b and c) or d`
27 |     pass
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:30:7
   |
29 | a, b, c, d = 0, 1, 0, 2
30 | while a and b or c and d:  # RUF021: => `(and b) or (c and d)`
   |       ^^^^^^^
31 |     pass
   |
help: Parenthesize the `and` subexpression
   |
29 | a, b, c, d = 0, 1, 0, 2
   - while a and b or c and d:  # RUF021: => `(and b) or (c and d)`
30 + while (a and b) or c and d:  # RUF021: => `(and b) or (c and d)`
31 |     pass
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:30:18
   |
29 | a, b, c, d = 0, 1, 0, 2
30 | while a and b or c and d:  # RUF021: => `(and b) or (c and d)`
   |                  ^^^^^^^
31 |     pass
   |
help: Parenthesize the `and` subexpression
   |
29 | a, b, c, d = 0, 1, 0, 2
   - while a and b or c and d:  # RUF021: => `(and b) or (c and d)`
30 + while a and b or (c and d):  # RUF021: => `(and b) or (c and d)`
31 |     pass
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:35:44
   |
33 | b, c, d, e = 2, 3, 0, 4
34 | # RUF021: => `a or b or c or (d and e)`:
35 | z = [a for a in range(5) if a or b or c or d and e]
   |                                            ^^^^^^^
36 |
37 | a, b, c, d = 0, 1, 3, 0
   |
help: Parenthesize the `and` subexpression
   |
34 | # RUF021: => `a or b or c or (d and e)`:
   - z = [a for a in range(5) if a or b or c or d and e]
35 + z = [a for a in range(5) if a or b or c or (d and e)]
36 |
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:38:8
   |
37 | a, b, c, d = 0, 1, 3, 0
38 | assert not a and b or c or d  # RUF021: => `(not a and b) or c or d`
   |        ^^^^^^^^^^^
39 |
40 | if (not a) and b or c or d:  # RUF021: => `((not a) and b) or c or d`
   |
help: Parenthesize the `and` subexpression
   |
37 | a, b, c, d = 0, 1, 3, 0
   - assert not a and b or c or d  # RUF021: => `(not a and b) or c or d`
38 + assert (not a and b) or c or d  # RUF021: => `(not a and b) or c or d`
39 |
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:40:4
   |
38 | assert not a and b or c or d  # RUF021: => `(not a and b) or c or d`
39 |
40 | if (not a) and b or c or d:  # RUF021: => `((not a) and b) or c or d`
   |    ^^^^^^^^^^^^^
41 |     if (not a and b) or c or d:  # OK
42 |         pass
   |
help: Parenthesize the `and` subexpression
   |
39 |
   - if (not a) and b or c or d:  # RUF021: => `((not a) and b) or c or d`
40 + if ((not a) and b) or c or d:  # RUF021: => `((not a) and b) or c or d`
41 |     if (not a and b) or c or d:  # OK
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:46:8
   |
44 |   if (
45 |       some_reasonably_long_condition
46 |       or some_other_reasonably_long_condition
   |  ________^
47 | |     and some_third_reasonably_long_condition
   | |____________________________________________^
48 |       or some_fourth_reasonably_long_condition
49 |       and some_fifth_reasonably_long_condition
   |
help: Parenthesize the `and` subexpression
   |
45 |     some_reasonably_long_condition
   -     or some_other_reasonably_long_condition
   -     and some_third_reasonably_long_condition
46 +     or (some_other_reasonably_long_condition
47 +     and some_third_reasonably_long_condition)
48 |     or some_fourth_reasonably_long_condition
   |

RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
  --> RUF021.py:48:8
   |
46 |       or some_other_reasonably_long_condition
47 |       and some_third_reasonably_long_condition
48 |       or some_fourth_reasonably_long_condition
   |  ________^
49 | |     and some_fifth_reasonably_long_condition
50 | |     # a comment
51 | |     and some_sixth_reasonably_long_condition
52 | |     and some_seventh_reasonably_long_condition
   | |______________________________________________^
53 |       # another comment
54 |       or some_eighth_reasonably_long_condition
   |
help: Parenthesize the `and` subexpression
   |
47 |     and some_third_reasonably_long_condition
   -     or some_fourth_reasonably_long_condition
48 +     or (some_fourth_reasonably_long_condition
49 |     and some_fifth_reasonably_long_condition
50 |     # a comment
51 |     and some_sixth_reasonably_long_condition
   -     and some_seventh_reasonably_long_condition
52 +     and some_seventh_reasonably_long_condition)
53 |     # another comment
   |
