From 6e6927341aa38b2153a3ca7096f1a6447e135dbf Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 11 May 2022 09:34:55 +0200 Subject: [PATCH] Add a doc example for simplifiable-if-expression and simplifiable-if-statement Refs #5953 Closes #5882 --- .../s/simplifiable-if-expression/bad.py | 9 +++++++ .../s/simplifiable-if-expression/details.rst | 1 - .../s/simplifiable-if-expression/good.py | 10 ++++++- .../s/simplifiable-if-expression/related.rst | 1 + .../s/simplifiable-if-statement/bad.py | 9 +++++++ .../s/simplifiable-if-statement/details.rst | 1 - .../s/simplifiable-if-statement/good.py | 7 ++++- .../simplifiable_if_expression.py | 26 +++++++++++++++++++ .../simplifiable_if_expression.txt | 7 ++--- 9 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 doc/data/messages/s/simplifiable-if-expression/bad.py delete mode 100644 doc/data/messages/s/simplifiable-if-expression/details.rst create mode 100644 doc/data/messages/s/simplifiable-if-expression/related.rst create mode 100644 doc/data/messages/s/simplifiable-if-statement/bad.py delete mode 100644 doc/data/messages/s/simplifiable-if-statement/details.rst diff --git a/doc/data/messages/s/simplifiable-if-expression/bad.py b/doc/data/messages/s/simplifiable-if-expression/bad.py new file mode 100644 index 0000000000..834ee38332 --- /dev/null +++ b/doc/data/messages/s/simplifiable-if-expression/bad.py @@ -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] diff --git a/doc/data/messages/s/simplifiable-if-expression/details.rst b/doc/data/messages/s/simplifiable-if-expression/details.rst deleted file mode 100644 index ab82045295..0000000000 --- a/doc/data/messages/s/simplifiable-if-expression/details.rst +++ /dev/null @@ -1 +0,0 @@ -You can help us make the doc better `by contributing `_ ! diff --git a/doc/data/messages/s/simplifiable-if-expression/good.py b/doc/data/messages/s/simplifiable-if-expression/good.py index c40beb573f..cfe7db28cf 100644 --- a/doc/data/messages/s/simplifiable-if-expression/good.py +++ b/doc/data/messages/s/simplifiable-if-expression/good.py @@ -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 diff --git a/doc/data/messages/s/simplifiable-if-expression/related.rst b/doc/data/messages/s/simplifiable-if-expression/related.rst new file mode 100644 index 0000000000..97e369f5a6 --- /dev/null +++ b/doc/data/messages/s/simplifiable-if-expression/related.rst @@ -0,0 +1 @@ +- `Simplifying an 'if' statement with bool() `_ diff --git a/doc/data/messages/s/simplifiable-if-statement/bad.py b/doc/data/messages/s/simplifiable-if-statement/bad.py new file mode 100644 index 0000000000..0c9fb317fe --- /dev/null +++ b/doc/data/messages/s/simplifiable-if-statement/bad.py @@ -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 diff --git a/doc/data/messages/s/simplifiable-if-statement/details.rst b/doc/data/messages/s/simplifiable-if-statement/details.rst deleted file mode 100644 index ab82045295..0000000000 --- a/doc/data/messages/s/simplifiable-if-statement/details.rst +++ /dev/null @@ -1 +0,0 @@ -You can help us make the doc better `by contributing `_ ! diff --git a/doc/data/messages/s/simplifiable-if-statement/good.py b/doc/data/messages/s/simplifiable-if-statement/good.py index c40beb573f..66692290d0 100644 --- a/doc/data/messages/s/simplifiable-if-statement/good.py +++ b/doc/data/messages/s/simplifiable-if-statement/good.py @@ -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 diff --git a/tests/functional/s/simplifiable/simplifiable_if_expression.py b/tests/functional/s/simplifiable/simplifiable_if_expression.py index bcfdf890b8..94db05f2c8 100644 --- a/tests/functional/s/simplifiable/simplifiable_if_expression.py +++ b/tests/functional/s/simplifiable/simplifiable_if_expression.py @@ -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 diff --git a/tests/functional/s/simplifiable/simplifiable_if_expression.txt b/tests/functional/s/simplifiable/simplifiable_if_expression.txt index 9d3f871141..d6b2758479 100644 --- a/tests/functional/s/simplifiable/simplifiable_if_expression.txt +++ b/tests/functional/s/simplifiable/simplifiable_if_expression.txt @@ -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