Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions its/ruling/src/test/resources/expected/python-S100.json
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,15 @@
743,
762,
826,
911,
915,
926,
938,
941,
960,
969,
975,
982,
1116,
1137,
1151,
Expand Down Expand Up @@ -2392,6 +2401,8 @@
149,
],
'project:twisted-12.1.0/twisted/protocols/ident.py':[
149,
153,
162,
165,
171,
Expand Down
25 changes: 0 additions & 25 deletions its/ruling/src/test/resources/expected/python-S1542.json
Original file line number Diff line number Diff line change
Expand Up @@ -1549,15 +1549,6 @@
],
'project:twisted-12.1.0/twisted/internet/base.py':[
856,
911,
915,
926,
938,
941,
960,
969,
975,
982,
],
'project:twisted-12.1.0/twisted/internet/default.py':[
17,
Expand Down Expand Up @@ -1615,15 +1606,6 @@
'project:twisted-12.1.0/twisted/internet/inotify.py':[
99,
],
'project:twisted-12.1.0/twisted/internet/iocpreactor/reactor.py':[
172,
184,
193,
205,
],
'project:twisted-12.1.0/twisted/internet/iocpreactor/tcp.py':[
145,
],
'project:twisted-12.1.0/twisted/internet/main.py':[
20,
],
Expand Down Expand Up @@ -2266,10 +2248,6 @@
'project:twisted-12.1.0/twisted/protocols/htb.py':[
264,
],
'project:twisted-12.1.0/twisted/protocols/ident.py':[
149,
153,
],
'project:twisted-12.1.0/twisted/protocols/loopback.py':[
98,
115,
Expand Down Expand Up @@ -2865,9 +2843,6 @@
1065,
1108,
1141,
1374,
1382,
1390,
],
'project:twisted-12.1.0/twisted/test/test_iutils.py':[
67,
Expand Down
7 changes: 7 additions & 0 deletions its/ruling/src/test/resources/expected/python-S1700.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
94,
114,
],
'project:buildbot-0.8.6p1/contrib/bb_applet.py':[
96,
],
'project:django-2.2.3/django/contrib/messages/storage/base.py':[
16,
],
Expand Down Expand Up @@ -38,5 +41,9 @@
],
'project:twisted-12.1.0/twisted/test/test_pb.py':[
1716,
1723,
],
'project:twisted-12.1.0/twisted/words/protocols/jabber/xmpp_stringprep.py':[
196,
],
}
22 changes: 0 additions & 22 deletions its/ruling/src/test/resources/expected/python-S1720.json
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,7 @@
71,
82,
99,
231,
233,
243,
246,
263,
],
'project:buildbot-0.8.6p1/buildbot/db/schedulers.py':[
Expand Down Expand Up @@ -6430,9 +6427,6 @@
'project:django-2.2.3/django/db/models/fields/related_descriptors.py':[
550,
638,
685,
701,
705,
802,
],
'project:django-2.2.3/django/db/models/fields/related_lookups.py':[
Expand Down Expand Up @@ -9998,8 +9992,6 @@
'project:numpy-1.16.4/numpy/distutils/fcompiler/pg.py':[
0,
13,
56,
60,
70,
127,
],
Expand Down Expand Up @@ -10774,8 +10766,6 @@
'project:numpy-1.16.4/numpy/lib/npyio.py':[
0,
39,
274,
282,
463,
542,
629,
Expand Down Expand Up @@ -11481,10 +11471,8 @@
1735,
2578,
3177,
3869,
5798,
6255,
6307,
6514,
6526,
6539,
Expand Down Expand Up @@ -14436,9 +14424,6 @@
'project:twisted-12.1.0/twisted/internet/base.py':[
34,
277,
911,
938,
982,
],
'project:twisted-12.1.0/twisted/internet/cfreactor.py':[
411,
Expand Down Expand Up @@ -15934,8 +15919,6 @@
264,
],
'project:twisted-12.1.0/twisted/protocols/ident.py':[
149,
153,
187,
],
'project:twisted-12.1.0/twisted/protocols/loopback.py':[
Expand Down Expand Up @@ -17005,9 +16988,6 @@
1141,
1166,
1372,
1374,
1382,
1390,
],
'project:twisted-12.1.0/twisted/test/test_iutils.py':[
67,
Expand Down Expand Up @@ -18066,8 +18046,6 @@
163,
403,
407,
691,
694,
701,
840,
1048,
Expand Down
2 changes: 2 additions & 0 deletions its/ruling/src/test/resources/expected/python-S2325.json
Original file line number Diff line number Diff line change
Expand Up @@ -4181,6 +4181,8 @@
149,
],
'project:twisted-12.1.0/twisted/protocols/ident.py':[
149,
153,
156,
162,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,35 @@
*/
package org.sonar.python.checks;

import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.AstNodeType;
import com.sonar.sslr.api.Token;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import org.sonar.check.Rule;
import org.sonar.python.PythonCheckAstNode;
import org.sonar.python.api.PythonGrammar;
import org.sonar.python.PythonSubscriptionCheck;
import org.sonar.python.api.tree.PyClassDefTree;
import org.sonar.python.api.tree.Tree;
import org.sonar.python.semantic.TreeSymbol;

@Rule(key = FieldDuplicatesClassNameCheck.CHECK_KEY)
public class FieldDuplicatesClassNameCheck extends PythonCheckAstNode {

public static final String CHECK_KEY = "S1700";
@Rule(key = "S1700")
public class FieldDuplicatesClassNameCheck extends PythonSubscriptionCheck {

private static final String MESSAGE = "Rename field \"%s\"";

@Override
public Set<AstNodeType> subscribedKinds() {
return Collections.singleton(PythonGrammar.CLASSDEF);
}

@Override
public void visitNode(AstNode astNode) {
if (!CheckUtils.classHasInheritance(astNode)) {
List<Token> allFields = new NewSymbolsAnalyzer().getClassFields(astNode);
String className = astNode.getFirstChild(PythonGrammar.CLASSNAME).getTokenValue();

for (Token name : allFields) {
if (className.equalsIgnoreCase(name.getValue())) {
addIssue(name, String.format(MESSAGE, name.getValue()))
.secondary(astNode.getFirstChild(PythonGrammar.CLASSNAME), "Class declaration");
}
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.CLASSDEF, ctx -> {
PyClassDefTree classDef = (PyClassDefTree) ctx.syntaxNode();
if (CheckUtils.classHasInheritance(classDef)) {
return;
}
}
String className = classDef.name().name();
Set<TreeSymbol> allFields = new HashSet<>(classDef.classFields());
allFields.addAll(classDef.instanceFields());
allFields.stream()
.filter(symbol -> className.equalsIgnoreCase(symbol.name()))
.forEach(symbol -> symbol.usages()
.stream()
.findFirst()
.ifPresent(usage -> ctx.addIssue(usage.tree(), String.format(MESSAGE, symbol.name())).secondary(classDef.name(), "Class declaration")));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def __int__(self, myclass):
self.myclass: type = myclass # Noncompliant [[secondary=-4]]

def fun(self):
self.myClass += 1
self.myClass += 1 # Noncompliant
if True:
self.MYCLASS = 10 # Noncompliant
self.field = 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,8 @@ private PyDecoratorTree decorator(AstNode astNode) {

private static boolean isMethodDefinition(AstNode node) {
AstNode parent = node.getParent();
for (int i = 0; i < 3; i++) {
if (parent != null) {
parent = parent.getParent();
}
while (parent != null && !parent.is(PythonGrammar.CLASSDEF, PythonGrammar.FUNCDEF)) {
parent = parent.getParent();
}
return parent != null && parent.is(PythonGrammar.CLASSDEF);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,11 @@ public void classdef_statement() {
PyFunctionDefTree funcDef = (PyFunctionDefTree) classDefTree.body().statements().get(0);
assertThat(funcDef.isMethodDefinition()).isTrue();

astNode = p.parse("class clazz:\n if True:\n def foo(): pass");
classDefTree = treeMaker.classDefStatement(astNode);
funcDef = (PyFunctionDefTree) ((PyIfStatementTree) classDefTree.body().statements().get(0)).body().statements().get(0);
assertThat(funcDef.isMethodDefinition()).isTrue();

astNode = p.parse("class ClassWithDocstring:\n" +
"\t\"\"\"This is a docstring\"\"\"\n" +
"\tpass");
Expand Down