Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add is operator to FURB124 (use equal chain) #159

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions refurb/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def get_common_expr_positions(*exprs: Expression) -> tuple[int, int] | None:


def get_common_expr_in_comparison_chain(
node: OpExpr, oper: str
node: OpExpr, oper: str, cmp_oper: str = "=="
) -> tuple[Expression, tuple[int, int]] | None:
"""
This function finds the first expression shared between 2 comparison
Expand All @@ -206,9 +206,12 @@ def get_common_expr_in_comparison_chain(

match extract_binary_oper(oper, node):
case (
ComparisonExpr(operators=["=="], operands=[a, b]),
ComparisonExpr(operators=["=="], operands=[c, d]),
) if indices := get_common_expr_positions(a, b, c, d):
ComparisonExpr(operators=[lhs_oper], operands=[a, b]),
ComparisonExpr(operators=[rhs_oper], operands=[c, d]),
) if (
lhs_oper == rhs_oper == cmp_oper
and (indices := get_common_expr_positions(a, b, c, d))
):
return a, indices

return None # pragma: no cover
Expand Down
33 changes: 24 additions & 9 deletions refurb/checks/logical/use_equal_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ class ErrorInfo(Error):
```
if x == y and x == z:
pass

# and

if x is None and y is None
pass
```

Good:

```
if x == y == z:
pass

# and

if x is y is None:
pass
```

Note: if `x` depends on side-effects, then this check should be ignored.
Expand All @@ -33,19 +43,24 @@ class ErrorInfo(Error):
categories = ["logical", "readability"]


def create_message(indices: tuple[int, int]) -> str:
def create_message(indices: tuple[int, int], oper: str = "==") -> str:
names = ["x", "y", "z"]
names.insert(indices[1], names[indices[0]])

expr = f"{names[0]} == {names[1]} and {names[2]} == {names[3]}"
expr = f"{names[0]} {oper} {names[1]} and {names[2]} {oper} {names[3]}"

return f"Replace `{expr}` with `x == y == z`"
return f"Replace `{expr}` with `x {oper} y {oper} z`"


def check(node: OpExpr, errors: list[Error]) -> None:
if data := get_common_expr_in_comparison_chain(node, oper="and"):
expr, indices = data

errors.append(
ErrorInfo(expr.line, expr.column, create_message(indices))
)
for cmp_oper in ("==", "is"):
if data := get_common_expr_in_comparison_chain(node, "and", cmp_oper):
expr, indices = data

errors.append(
ErrorInfo(
expr.line,
expr.column,
create_message(indices, cmp_oper),
)
)
8 changes: 8 additions & 0 deletions test/data/err_124.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
_ = x == y and y == z and z == 1
_ = x == y and z == x

_ = x is None and y is None
_ = x is None and None is y
_ = None is x and y is None
_ = x is None and y is None and True


# these should not

_ = x == y and z == 1
_ = x == y or 1 == z
_ = x == y or 1 == z
_ = x == y or x <= z

_ = x is None and y is 1
_ = x is None or y is None
4 changes: 4 additions & 0 deletions test/data/err_124.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ test/data/err_124.py:8:5 [FURB124]: Replace `x == y and x == z` with `x == y ==
test/data/err_124.py:9:5 [FURB124]: Replace `x == y and y == z` with `x == y == z`
test/data/err_124.py:9:16 [FURB124]: Replace `x == y and y == z` with `x == y == z`
test/data/err_124.py:10:5 [FURB124]: Replace `x == y and z == x` with `x == y == z`
test/data/err_124.py:12:5 [FURB124]: Replace `x is y and z is y` with `x is y is z`
test/data/err_124.py:13:5 [FURB124]: Replace `x is y and y is z` with `x is y is z`
test/data/err_124.py:14:5 [FURB124]: Replace `x is y and z is x` with `x is y is z`
test/data/err_124.py:15:5 [FURB124]: Replace `x is y and z is y` with `x is y is z`