Skip to content

Commit

Permalink
Exclude possible match statements
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Aug 21, 2021
1 parent 4fcf697 commit 43ab448
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
25 changes: 25 additions & 0 deletions pylint/extensions/code_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:

prev_sibling = node.previous_sibling()
if self._check_prev_sibling_to_if_stmt(prev_sibling, node_name.name):
if isinstance(node.test, nodes.Compare):
# Check if match statement would be a better fit
# x = 2
# if x == 1: ...
# elif x == 2: ...
# elif x: ...
next_if_node: Optional[nodes.If] = None
next_sibling = node.next_sibling()
if len(node.orelse) == 1 and isinstance(node.orelse[0], nodes.If):
next_if_node = node.orelse[0]
elif isinstance(next_sibling, nodes.If):
next_if_node = next_sibling

if ( # pylint: disable=too-many-boolean-expressions
next_if_node is not None
and (
isinstance(next_if_node.test, nodes.Compare)
and isinstance(next_if_node.test.left, nodes.Name)
and next_if_node.test.left.name == node_name.name
or isinstance(next_if_node.test, nodes.Name)
and next_if_node.test.name == node_name.name
)
):
return

test_str = node.test.as_string().replace(
node_name.name,
f"({node_name.name} := {prev_sibling.value.as_string()})", # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if a4:
...

def func():
def func_a():
a5 = some___object.function_name_is_just_long_enough_to_fit_in_line() # some comment
if a5 is None: # [consider-using-assignment-expr]
...
Expand Down Expand Up @@ -63,3 +63,81 @@ def func():
d3 = 2
if d3 == 2: # [consider-using-assignment-expr]
...


# -----
# Don't emit warning if match statement would be a better fit
o1 = 2
if o1 == 1:
...
elif o1 == 2:
...
elif o1 == 3:
...

o2 = 2
if o2 == 1:
...
elif o2:
...

o3 = 2
if o3 == 1: # [consider-using-assignment-expr]
...
else:
...

o4 = 2
if o4 == 1: # [consider-using-assignment-expr]
...
elif o4 and o4_other:
...

o5 = 2
if o5 == 1: # [consider-using-assignment-expr]
...
elif o5_other == 1:
...

o6 = 2
if o6 == 1: # [consider-using-assignment-expr]
...
elif o6_other:
...

def func_p():
p1 = 2
if p1 == 1:
return
if p1 == 2:
return

p2 = 2
if p2 == 1:
return
if p2:
return

p3 = 2
if p3 == 1: # [consider-using-assignment-expr]
...
else:
...

p4 = 2
if p4 == 1: # [consider-using-assignment-expr]
...
elif p4 and p4_other:
...

p5 = 2
if p5 == 1: # [consider-using-assignment-expr]
...
elif p5_other == 1:
...

p6 = 2
if p6 == 1: # [consider-using-assignment-expr]
...
elif p6_other:
...
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
consider-using-assignment-expr:4:3::"Use 'if (a1 := 2):' instead":HIGH
consider-using-assignment-expr:16:3::"Use 'if (a3 := 2) == a3_a:' instead":HIGH
consider-using-assignment-expr:26:7:func:"Use 'if (a5 := some___object.function_name_is_just_long_enough_to_fit_in_line()) is None:' instead":HIGH
consider-using-assignment-expr:26:7:func_a:"Use 'if (a5 := some___object.function_name_is_just_long_enough_to_fit_in_line()) is None:' instead":HIGH
consider-using-assignment-expr:36:3::"Use 'if (b1 := 2):' instead":HIGH
consider-using-assignment-expr:40:3::"Use 'if (b2 := some_function(2, 3)):' instead":HIGH
consider-using-assignment-expr:44:3::"Use 'if (b3 := some_object.variable):' instead":HIGH
consider-using-assignment-expr:50:7::"Use 'if not (c1 := 2):' instead":HIGH
consider-using-assignment-expr:56:3::"Use 'if (d1 := 2) is True:' instead":HIGH
consider-using-assignment-expr:60:3::"Use 'if (d2 := 2) is not None:' instead":HIGH
consider-using-assignment-expr:64:3::"Use 'if (d3 := 2) == 2:' instead":HIGH
consider-using-assignment-expr:85:3::"Use 'if (o3 := 2) == 1:' instead":HIGH
consider-using-assignment-expr:91:3::"Use 'if (o4 := 2) == 1:' instead":HIGH
consider-using-assignment-expr:97:3::"Use 'if (o5 := 2) == 1:' instead":HIGH
consider-using-assignment-expr:103:3::"Use 'if (o6 := 2) == 1:' instead":HIGH
consider-using-assignment-expr:122:7:func_p:"Use 'if (p3 := 2) == 1:' instead":HIGH
consider-using-assignment-expr:128:7:func_p:"Use 'if (p4 := 2) == 1:' instead":HIGH
consider-using-assignment-expr:134:7:func_p:"Use 'if (p5 := 2) == 1:' instead":HIGH
consider-using-assignment-expr:140:7:func_p:"Use 'if (p6 := 2) == 1:' instead":HIGH

0 comments on commit 43ab448

Please sign in to comment.