summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorsten Marek <tmarek@google.com>2013-03-29 18:11:40 +0100
committerTorsten Marek <tmarek@google.com>2013-03-29 18:11:40 +0100
commit902a31a444005b02a58576a0a196e7f3ce28fc49 (patch)
tree40e2b9cbf52d4f580e46af636103b5867c1cf646
parente04d38b413c1258f4b62acb305a4aa5ab634a5b6 (diff)
downloadpylint-902a31a444005b02a58576a0a196e7f3ce28fc49.tar.gz
Warn about suspicious arguments in {bytes,str,unicode}.{l,r,}strip calls. Closes #74013
-rw-r--r--ChangeLog3
-rw-r--r--checkers/strings.py (renamed from checkers/string_format.py)34
-rw-r--r--test/input/func_bad_str_strip_call.py9
-rw-r--r--test/messages/func_bad_str_strip_call.txt3
4 files changed, 47 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 0dbd29b..bb44729 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,9 @@ ChangeLog for PyLint
====================
--
+ * #74013: new E1310[bad-str-strip-call] message warning when a call to a
+ {l,r,}strip method contains duplicate characters (patch by Torsten Marek)
+
* #124660: internal dependencies should not appear in external dependencies
report
diff --git a/checkers/string_format.py b/checkers/strings.py
index 0384f03..9786090 100644
--- a/checkers/string_format.py
+++ b/checkers/strings.py
@@ -1,5 +1,7 @@
# Copyright (c) 2009-2010 Arista Networks, Inc. - James Lingard
-# Copyright (c) 2004-2010 LOGILAB S.A. (Paris, FRANCE).
+# Copyright (c) 2004-2013 LOGILAB S.A. (Paris, FRANCE).
+# Copyright 2012 Google Inc.
+#
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
@@ -17,6 +19,7 @@
"""
from logilab import astng
+
from pylint.interfaces import IASTNGChecker
from pylint.checkers import BaseChecker
from pylint.checkers import utils
@@ -75,7 +78,7 @@ class StringFormatChecker(BaseChecker):
"""
__implements__ = (IASTNGChecker,)
- name = 'string_format'
+ name = 'string'
msgs = MSGS
def visit_binop(self, node):
@@ -158,6 +161,33 @@ class StringFormatChecker(BaseChecker):
self.add_message('E1306', node=node)
+class StringMethodsChecker(BaseChecker):
+ __implements__ = (IASTNGChecker,)
+ name = 'string'
+ msgs = {
+ 'E1310': ("Suspicious argument in %s.%s call",
+ "bad-str-strip-call",
+ "The argument to a str.{l,r,}strip call contains a"
+ " duplicate character, "),
+ }
+
+ def visit_callfunc(self, node):
+ func = utils.safe_infer(node.func)
+ if (isinstance(func, astng.BoundMethod)
+ and isinstance(func.bound, astng.Instance)
+ and func.bound.name in ('str', 'unicode', 'bytes')
+ and func.name in ('strip', 'lstrip', 'rstrip')
+ and node.args):
+ arg = utils.safe_infer(node.args[0])
+ if not isinstance(arg, astng.Const):
+ return
+ if len(arg.value) != len(set(arg.value)):
+ self.add_message('E1310', node=node,
+ args=(func.bound.name, func.name))
+
+
+
def register(linter):
"""required method to auto register this checker """
linter.register_checker(StringFormatChecker(linter))
+ linter.register_checker(StringMethodsChecker(linter))
diff --git a/test/input/func_bad_str_strip_call.py b/test/input/func_bad_str_strip_call.py
new file mode 100644
index 0000000..2d94a6e
--- /dev/null
+++ b/test/input/func_bad_str_strip_call.py
@@ -0,0 +1,9 @@
+"""Suspicious str.strip calls."""
+__revision__ = 1
+
+''.strip('yo')
+''.strip()
+
+u''.strip('http://')
+u''.lstrip('http://')
+b''.rstrip('http://')
diff --git a/test/messages/func_bad_str_strip_call.txt b/test/messages/func_bad_str_strip_call.txt
new file mode 100644
index 0000000..44b8780
--- /dev/null
+++ b/test/messages/func_bad_str_strip_call.txt
@@ -0,0 +1,3 @@
+E: 7: Suspicious argument in unicode.strip call
+E: 8: Suspicious argument in unicode.lstrip call
+E: 9: Suspicious argument in str.rstrip call