Skip to content
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
9 changes: 9 additions & 0 deletions doc/data/messages/s/simplifiable-if-expression/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_thing(an_object):
return True if an_object in FLYING_THINGS else False # [simplifiable-if-expression]


def is_not_flying_thing(an_object):
return False if an_object in FLYING_THINGS else True # [simplifiable-if-expression]
1 change: 0 additions & 1 deletion doc/data/messages/s/simplifiable-if-expression/details.rst

This file was deleted.

10 changes: 9 additions & 1 deletion doc/data/messages/s/simplifiable-if-expression/good.py
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# This is a placeholder for correct code for this message.
FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_thing(an_object):
return an_object in FLYING_THINGS


def is_not_flying_thing(an_object):
return an_object not in FLYING_THINGS
1 change: 1 addition & 0 deletions doc/data/messages/s/simplifiable-if-expression/related.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `Simplifying an 'if' statement with bool() <https://stackoverflow.com/questions/49546992/>`_
9 changes: 9 additions & 0 deletions doc/data/messages/s/simplifiable-if-statement/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_animal(an_object):
if isinstance(an_object, Animal) and an_object in FLYING_THINGS: # [simplifiable-if-statement]
is_flying = True
else:
is_flying = False
return is_flying
1 change: 0 additions & 1 deletion doc/data/messages/s/simplifiable-if-statement/details.rst

This file was deleted.

7 changes: 6 additions & 1 deletion doc/data/messages/s/simplifiable-if-statement/good.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# This is a placeholder for correct code for this message.
FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_animal(an_object):
is_flying = isinstance(an_object, Animal) and an_object.name in FLYING_THINGS
return is_flying
3 changes: 2 additions & 1 deletion pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ class RefactoringChecker(checkers.BaseTokenChecker):
"R1719": (
"The if expression can be replaced with %s",
"simplifiable-if-expression",
"Used when an if expression can be replaced with 'bool(test)'.",
"Used when an if expression can be replaced with 'bool(test)' "
"or simply 'test' if the boolean cast is implicit.",
),
"R1720": (
'Unnecessary "%s" after "raise", %s',
Expand Down
26 changes: 26 additions & 0 deletions tests/functional/s/simplifiable/simplifiable_if_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,47 @@ def test_simplifiable_1(arg):
# Simple test that can be replaced by bool(arg)
return True if arg else False # [simplifiable-if-expression]


def test_simplifiable_2(arg):
# Simple test that can be replaced by not arg
return False if arg else True # [simplifiable-if-expression]


def test_simplifiable_3(arg):
# Simple test that can be replaced by arg == 1
return True if arg == 1 else False # [simplifiable-if-expression]


def test_simplifiable_4(arg):
# Simple test that can be replaced by not (arg == 1)
return False if arg == 1 else True # [simplifiable-if-expression]


def test_not_simplifiable(arg):
x = True if arg else True
y = 0 if arg else 1
t = False if arg != 1 else False
t2 = None if arg > 3 else False
return x, y, t, t2


def convoluted_function():
a = []
b = []
if bool('special test'):
a = True
else:
b = False
return a + b


FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_animal(an_object):
is_flying = False
if isinstance(an_object, str) and an_object in FLYING_THINGS: # [simplifiable-if-statement]
is_flying = True
else:
is_flying = False
return is_flying
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
simplifiable-if-expression:8:11:8:33:test_simplifiable_1:The if expression can be replaced with 'bool(test)':UNDEFINED
simplifiable-if-expression:12:11:12:33:test_simplifiable_2:The if expression can be replaced with 'not test':UNDEFINED
simplifiable-if-expression:16:11:16:38:test_simplifiable_3:The if expression can be replaced with 'test':UNDEFINED
simplifiable-if-expression:20:11:20:38:test_simplifiable_4:The if expression can be replaced with 'not test':UNDEFINED
simplifiable-if-expression:13:11:13:33:test_simplifiable_2:The if expression can be replaced with 'not test':UNDEFINED
simplifiable-if-expression:18:11:18:38:test_simplifiable_3:The if expression can be replaced with 'test':UNDEFINED
simplifiable-if-expression:23:11:23:38:test_simplifiable_4:The if expression can be replaced with 'not test':UNDEFINED
simplifiable-if-statement:49:4:52:25:is_flying_animal:The if statement can be replaced with 'var = bool(test)':UNDEFINED