diff --git a/doc/data/messages/c/confusing-consecutive-elif/bad.py b/doc/data/messages/c/confusing-consecutive-elif/bad.py new file mode 100644 index 0000000000..93e1e2dee8 --- /dev/null +++ b/doc/data/messages/c/confusing-consecutive-elif/bad.py @@ -0,0 +1,6 @@ +def myfunc(shall_continue: bool, shall_exit: bool): + if shall_continue: + if input("Are you sure?") == "y": + print("Moving on.") + elif shall_exit: # [confusing-consecutive-elif] + print("Exiting.") diff --git a/doc/data/messages/c/confusing-consecutive-elif/details.rst b/doc/data/messages/c/confusing-consecutive-elif/details.rst new file mode 100644 index 0000000000..bd2ecc4ee5 --- /dev/null +++ b/doc/data/messages/c/confusing-consecutive-elif/details.rst @@ -0,0 +1 @@ +Creating a function for the nested conditional, or adding an explicit ``else`` in the indented ``if`` statement, even if it only contains a ``pass`` statement, can help clarify the code. diff --git a/doc/data/messages/c/confusing-consecutive-elif/good.py b/doc/data/messages/c/confusing-consecutive-elif/good.py new file mode 100644 index 0000000000..1722a6bfa6 --- /dev/null +++ b/doc/data/messages/c/confusing-consecutive-elif/good.py @@ -0,0 +1,22 @@ +# Option 1: add explicit 'else' +def myfunc(shall_continue: bool, shall_exit: bool): + if shall_continue: + if input("Are you sure?") == "y": + print("Moving on.") + else: + pass + elif shall_exit: + print("Exiting.") + + +# Option 2: extract function +def user_confirmation(): + if input("Are you sure?") == "y": + print("Moving on.") + + +def myfunc2(shall_continue: bool, shall_exit: bool): + if shall_continue: + user_confirmation() + elif shall_exit: + print("Exiting.") diff --git a/doc/data/messages/c/confusing-consecutive-elif/pylintrc b/doc/data/messages/c/confusing-consecutive-elif/pylintrc new file mode 100644 index 0000000000..6a11b2c099 --- /dev/null +++ b/doc/data/messages/c/confusing-consecutive-elif/pylintrc @@ -0,0 +1,2 @@ +[MASTER] +load-plugins=pylint.extensions.confusing_elif diff --git a/doc/test_messages_documentation.py b/doc/test_messages_documentation.py index e131d318ba..f80542f07d 100644 --- a/doc/test_messages_documentation.py +++ b/doc/test_messages_documentation.py @@ -7,7 +7,7 @@ from collections import Counter from pathlib import Path from typing import Counter as CounterType -from typing import List, TextIO, Tuple +from typing import List, Optional, TextIO, Tuple import pytest @@ -53,7 +53,13 @@ def __init__(self, test_file: Tuple[str, Path]) -> None: self._linter.config.persistent = 0 checkers.initialize(self._linter) - config_file = next(config.find_default_config_files(), None) + # Check if this message has a custom configuration file (e.g. for enabling optional checkers). + # If not, use the default configuration. + config_file: Optional[Path] + if (test_file[1].parent / "pylintrc").exists(): + config_file = test_file[1].parent / "pylintrc" + else: + config_file = next(config.find_default_config_files(), None) _config_initialization( self._linter,