Skip to content

Commit 35fe42b

Browse files
Add a doc example for simplifiable-if-expression and simplifiable-if-statement
Refs #5953 Closes #5882
1 parent f4c6a25 commit 35fe42b

File tree

9 files changed

+64
-7
lines changed

9 files changed

+64
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
2+
3+
4+
def is_flying_thing(an_object):
5+
return True if an_object in FLYING_THINGS else False # [simplifiable-if-expression]
6+
7+
8+
def is_not_flying_thing(an_object):
9+
return False if an_object in FLYING_THINGS else True # [simplifiable-if-expression]

doc/data/messages/s/simplifiable-if-expression/details.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
# This is a placeholder for correct code for this message.
1+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
2+
3+
4+
def is_flying_thing(an_object):
5+
return an_object in FLYING_THINGS
6+
7+
8+
def is_not_flying_thing(an_object):
9+
return an_object not in FLYING_THINGS
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `Simplifying an 'if' statement with bool() <https://stackoverflow.com/questions/49546992/>`_
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
2+
3+
4+
def is_flying_animal(an_object):
5+
if isinstance(an_object, Animal) and an_object in FLYING_THINGS: # [simplifiable-if-statement]
6+
is_flying = True
7+
else:
8+
is_flying = False
9+
return is_flying

doc/data/messages/s/simplifiable-if-statement/details.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# This is a placeholder for correct code for this message.
1+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
2+
3+
4+
def is_flying_animal(an_object):
5+
is_flying = isinstance(an_object, Animal) and an_object.name in FLYING_THINGS
6+
return is_flying

tests/functional/s/simplifiable/simplifiable_if_expression.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,47 @@ def test_simplifiable_1(arg):
77
# Simple test that can be replaced by bool(arg)
88
return True if arg else False # [simplifiable-if-expression]
99

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

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

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

25+
2226
def test_not_simplifiable(arg):
2327
x = True if arg else True
2428
y = 0 if arg else 1
2529
t = False if arg != 1 else False
2630
t2 = None if arg > 3 else False
2731
return x, y, t, t2
32+
33+
34+
def convoluted_function():
35+
a = []
36+
b = []
37+
if bool('special test'):
38+
a = True
39+
else:
40+
b = False
41+
return a + b
42+
43+
44+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
45+
46+
47+
def is_flying_animal(an_object):
48+
is_flying = False
49+
if isinstance(an_object, str) and an_object in FLYING_THINGS: # [simplifiable-if-statement]
50+
is_flying = True
51+
else:
52+
is_flying = False
53+
return is_flying
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
simplifiable-if-expression:8:11:8:33:test_simplifiable_1:The if expression can be replaced with 'bool(test)':UNDEFINED
2-
simplifiable-if-expression:12:11:12:33:test_simplifiable_2:The if expression can be replaced with 'not test':UNDEFINED
3-
simplifiable-if-expression:16:11:16:38:test_simplifiable_3:The if expression can be replaced with 'test':UNDEFINED
4-
simplifiable-if-expression:20:11:20:38:test_simplifiable_4:The if expression can be replaced with 'not test':UNDEFINED
2+
simplifiable-if-expression:13:11:13:33:test_simplifiable_2:The if expression can be replaced with 'not test':UNDEFINED
3+
simplifiable-if-expression:18:11:18:38:test_simplifiable_3:The if expression can be replaced with 'test':UNDEFINED
4+
simplifiable-if-expression:23:11:23:38:test_simplifiable_4:The if expression can be replaced with 'not test':UNDEFINED
5+
simplifiable-if-statement:49:4:52:25:is_flying_animal:The if statement can be replaced with 'var = bool(test)':UNDEFINED

0 commit comments

Comments
 (0)