Skip to content

Commit ce9c8bb

Browse files
pynicolasalban-auzeill
authored andcommitted
SONARPY-229 UselessParenthesisAfterKeywordCheck should not raise issues on tuples (SonarSource#122)
1 parent 691b8a5 commit ce9c8bb

3 files changed

Lines changed: 15 additions & 14 deletions

File tree

its/ruling/src/test/resources/expected/python-S1721.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,6 @@
323323
'project:django-1.4/django/contrib/gis/db/models/proxy.py':[
324324
33,
325325
],
326-
'project:django-1.4/django/contrib/gis/db/models/sql/compiler.py':[
327-
81,
328-
],
329326
'project:django-1.4/django/contrib/gis/db/models/sql/where.py':[
330327
28,
331328
],
@@ -514,10 +511,6 @@
514511
'project:django-1.4/django/db/models/base.py':[
515512
423,
516513
],
517-
'project:django-1.4/django/db/models/deletion.py':[
518-
245,
519-
274,
520-
],
521514
'project:django-1.4/django/db/models/fields/files.py':[
522515
217,
523516
],
@@ -549,7 +542,6 @@
549542
],
550543
'project:django-1.4/django/dispatch/dispatcher.py':[
551544
10,
552-
224,
553545
],
554546
'project:django-1.4/django/dispatch/saferef.py':[
555547
142,
@@ -752,11 +744,9 @@
752744
],
753745
'project:tornado-2.3/demos/appengine/markdown.py':[
754746
1067,
755-
1497,
756747
],
757748
'project:tornado-2.3/demos/blog/markdown.py':[
758749
1067,
759-
1497,
760750
],
761751
'project:tornado-2.3/tornado/httpclient.py':[
762752
217,
@@ -1417,9 +1407,6 @@
14171407
248,
14181408
327,
14191409
],
1420-
'project:twisted-12.1.0/twisted/web/template.py':[
1421-
224,
1422-
],
14231410
'project:twisted-12.1.0/twisted/web/test/test_http.py':[
14241411
901,
14251412
902,

python-checks/src/main/java/org/sonar/python/checks/UselessParenthesisAfterKeywordCheck.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void visitNode(AstNode node) {
7575
checkParenthesis(testNodes.get(1), "elif", testNodes.get(1));
7676
}
7777
} else if (node.is(PythonGrammar.FOR_STMT)) {
78-
checkParenthesis(node.getFirstChild(PythonGrammar.EXPRLIST), "for", node);
78+
visitForExpression(node);
7979
checkParenthesis(node.getFirstChild(PythonGrammar.TESTLIST), "in", node);
8080
} else if (node.is(PythonGrammar.RETURN_STMT)) {
8181
checkParenthesis(node.getFirstChild(PythonGrammar.TESTLIST), "return", node);
@@ -88,6 +88,12 @@ public void visitNode(AstNode node) {
8888
}
8989
}
9090

91+
private void visitForExpression(AstNode node) {
92+
if (node.getFirstChild(PythonGrammar.EXPRLIST).getNumberOfChildren() == 1) {
93+
checkParenthesis(node.getFirstChild(PythonGrammar.EXPRLIST), "for", node);
94+
}
95+
}
96+
9197
private void visitNotTest(AstNode node) {
9298
boolean hasUselessParenthesis = node.select()
9399
.children(PythonGrammar.ATOM)

python-checks/src/test/resources/checks/uselessParenthesisAfterKeyword.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,11 @@ def func():
7575
if (x > 0 and
7676
x < 3):
7777
pass
78+
79+
my_pairs = [(1, 2), (5, 6)]
80+
names = ['small', 'large']
81+
for (first, second), name in zip(my_pairs, names): # the parenthesis after the "for" keyword is not useless
82+
print(name, first + second)
83+
84+
for (x, y) in foo: # Noncompliant
85+
print(x, y)

0 commit comments

Comments
 (0)