summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSushobhit <31987769+sushobhit27@users.noreply.github.com>2019-08-22 18:46:48 +0530
committerClaudiu Popa <pcmanticore@gmail.com>2019-08-22 15:16:48 +0200
commit6b3afd4f6d75debd7f286f0d3c760ed10ab1e79f (patch)
treefae404576e796b3a1f188a0a7a1fc313d9b118f4
parent827181602716f64ae226761bf36328cdf67ef782 (diff)
downloadpylint-git-6b3afd4f6d75debd7f286f0d3c760ed10ab1e79f.tar.gz
Added a new check, consider-using-sys-exit, close #2925 (#3062)
Close #2925
-rw-r--r--ChangeLog7
-rw-r--r--doc/whatsnew/2.4.rst7
-rw-r--r--pylint/checkers/refactoring.py11
-rw-r--r--tests/functional/consider_using_sys_exit.py14
-rw-r--r--tests/functional/consider_using_sys_exit.txt3
5 files changed, 42 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b981cfa00..1bfec7da3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,13 @@ What's New in Pylint 2.4.0?
Release date: TBA
+* Added a new check, ``consider-using-sys-exit``
+
+ This check is emitted when we detect that a quit() or exit() is invoked
+ instead of sys.exit(), which is the preferred way of exiting in program.
+
+ Close #2925
+
* ``useless-suppression`` check now ignores ``cyclic-import`` suppressions,
which could lead to false postiives due to incomplete context at the time
of the check.
diff --git a/doc/whatsnew/2.4.rst b/doc/whatsnew/2.4.rst
index ee4c2e8bd..4c279ca9a 100644
--- a/doc/whatsnew/2.4.rst
+++ b/doc/whatsnew/2.4.rst
@@ -13,6 +13,13 @@ Summary -- Release highlights
New checkers
============
+* Added a new check, ``consider-using-sys-exit``
+
+ This check is emitted when we detect that a quit() or exit() is invoked
+ instead of sys.exit(), which is the preferred way of exiting in program.
+
+ Close #2925
+
* Added a new check, ``arguments-out-of-order``
This check warns if you have arguments with names that match those in
diff --git a/pylint/checkers/refactoring.py b/pylint/checkers/refactoring.py
index 6291e9126..98dd80827 100644
--- a/pylint/checkers/refactoring.py
+++ b/pylint/checkers/refactoring.py
@@ -273,6 +273,11 @@ class RefactoringChecker(checkers.BaseTokenChecker):
"consider using the list, dict or set constructor. "
"It is faster and simpler.",
),
+ "R1722": (
+ "Consider using sys.exit()",
+ "consider-using-sys-exit",
+ "Instead of using exit() or quit(), consider using the sys.exit().",
+ ),
}
options = (
(
@@ -641,10 +646,16 @@ class RefactoringChecker(checkers.BaseTokenChecker):
"stop-iteration-return",
"consider-using-dict-comprehension",
"consider-using-set-comprehension",
+ "consider-using-sys-exit",
)
def visit_call(self, node):
self._check_raising_stopiteration_in_generator_next_call(node)
self._check_consider_using_comprehension_constructor(node)
+ self._check_quit_exit_call(node)
+
+ def _check_quit_exit_call(self, node):
+ if isinstance(node.func, astroid.Name) and node.func.name in ("quit", "exit"):
+ self.add_message("consider-using-sys-exit", node=node)
def _check_raising_stopiteration_in_generator_next_call(self, node):
"""Check if a StopIteration exception is raised by the call to next function
diff --git a/tests/functional/consider_using_sys_exit.py b/tests/functional/consider_using_sys_exit.py
new file mode 100644
index 000000000..4cec44847
--- /dev/null
+++ b/tests/functional/consider_using_sys_exit.py
@@ -0,0 +1,14 @@
+# pylint: disable=missing-docstring, invalid-name, blacklisted-name, redefined-builtin, unused-variable
+import sys
+
+def foo():
+ exit() # [consider-using-sys-exit]
+
+def foo_1():
+ quit() # [consider-using-sys-exit]
+
+def foo_2():
+ quit = 'abc'
+ sys.exit()
+
+quit() # [consider-using-sys-exit]
diff --git a/tests/functional/consider_using_sys_exit.txt b/tests/functional/consider_using_sys_exit.txt
new file mode 100644
index 000000000..0f46df9b0
--- /dev/null
+++ b/tests/functional/consider_using_sys_exit.txt
@@ -0,0 +1,3 @@
+consider-using-sys-exit:5:foo:Consider using sys.exit()
+consider-using-sys-exit:8:foo_1:Consider using sys.exit()
+consider-using-sys-exit:14::Consider using sys.exit()