Skip to content

Commit

Permalink
SIM908: Fix false-positive (#128)
Browse files Browse the repository at this point in the history
Ensure that the assigned name is equal to the name in the if.test

See #126
  • Loading branch information
MartinThoma committed Mar 29, 2022
1 parent ad67ae7 commit b0fc2bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions flake8_simplify/rules/ast_if.py
Expand Up @@ -341,6 +341,12 @@ def get_sim908(node: ast.If) -> List[Tuple[int, int, str]]:
and isinstance(node.body[0].targets[0], ast.Name)
):
return errors

test_var = node.test.left
slice_var = node.body[0].value.slice
if to_source(slice_var) != to_source(test_var):
return errors

key = to_source(node.test.left)
dictname = to_source(node.test.comparators[0])
errors.append(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_900_rules.py
Expand Up @@ -154,6 +154,25 @@ def test_sim908():
}


@pytest.mark.parametrize(
"s",
(
# Credits to jonyscathe for this example:
# https://github.com/MartinThoma/flake8-simplify/issues/126
"""if "." in resistance:
# Swap '.' with suffix
resistance = resistance.replace(".", resistance[-1])[:-1]""",
"""if "." in iterable:
iterable = iterable[:-1]""",
),
ids=("issue-125", "issue-125-simplified"),
)
def test_sim908_false_positive(s):
results = _results(s)
for el in results:
assert "SIM908" not in el


@pytest.mark.parametrize(
("s", "msg"),
(
Expand Down

0 comments on commit b0fc2bb

Please sign in to comment.