Skip to content

Commit 11aec70

Browse files
SONARPY-777 Fix FP on S1764 when expressions contain function calls
1 parent a66a415 commit 11aec70

3 files changed

Lines changed: 16 additions & 47 deletions

File tree

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

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
322,
3939
327,
4040
328,
41-
617,
4241
],
4342
'project:numpy-1.16.4/numpy/core/tests/test_regression.py':[
4443
2279,
@@ -53,9 +52,6 @@
5352
'project:numpy-1.16.4/numpy/lib/nanfunctions.py':[
5453
75,
5554
],
56-
'project:numpy-1.16.4/numpy/lib/polynomial.py':[
57-
957,
58-
],
5955
'project:numpy-1.16.4/numpy/lib/tests/test_polynomial.py':[
6056
237,
6157
],
@@ -89,9 +85,6 @@
8985
175,
9086
176,
9187
],
92-
'project:numpy-1.16.4/numpy/tests/test_ctypeslib.py':[
93-
122,
94-
],
9588
'project:tensorflow/python/autograph/pyct/transpiler_test.py':[
9689
112,
9790
137,
@@ -110,29 +103,16 @@
110103
'project:tensorflow/python/eager/backprop_test.py':[
111104
784,
112105
],
113-
'project:tensorflow/python/eager/core_test.py':[
114-
264,
115-
267,
116-
],
117106
'project:tensorflow/python/eager/ops_test.py':[
118107
186,
119108
],
120109
'project:tensorflow/python/framework/tensor_shape_test.py':[
121-
84,
122-
86,
123-
88,
124-
90,
125-
142,
126-
162,
127110
392,
128111
394,
129112
],
130113
'project:tensorflow/python/keras/mixed_precision/experimental/autocast_variable_test.py':[
131114
250,
132115
],
133-
'project:tensorflow/python/kernel_tests/distributions/uniform_test.py':[
134-
239,
135-
],
136116
'project:tensorflow/python/kernel_tests/metrics_test.py':[
137117
2481,
138118
2484,
@@ -150,8 +130,6 @@
150130
4174,
151131
],
152132
'project:tensorflow/python/lib/core/bfloat16_test.py':[
153-
105,
154-
128,
155133
198,
156134
],
157135
'project:tensorflow/python/ops/clip_ops.py':[
@@ -163,22 +141,6 @@
163141
'project:tensorflow/python/tf_program/tests/mlir_gen_test.py':[
164142
79,
165143
],
166-
'project:tensorflow/python/tpu/tensor_tracer.py':[
167-
1540,
168-
],
169-
'project:tornado-2.3/demos/appengine/markdown.py':[
170-
375,
171-
],
172-
'project:tornado-2.3/demos/blog/markdown.py':[
173-
375,
174-
],
175-
'project:tornado-2.3/tornado/test/simple_httpclient_test.py':[
176-
120,
177-
],
178-
'project:twisted-12.1.0/twisted/internet/test/test_address.py':[
179-
27,
180-
28,
181-
],
182144
'project:twisted-12.1.0/twisted/internet/test/test_base.py':[
183145
211,
184146
212,
@@ -205,12 +167,8 @@
205167
55,
206168
],
207169
'project:twisted-12.1.0/twisted/python/test/test_util.py':[
208-
572,
209-
573,
210170
575,
211171
576,
212-
585,
213-
596,
214172
],
215173
'project:twisted-12.1.0/twisted/python/test/test_versions.py':[
216174
75,
@@ -219,11 +177,6 @@
219177
'project:twisted-12.1.0/twisted/test/test_internet.py':[
220178
670,
221179
],
222-
'project:twisted-12.1.0/twisted/test/test_paths.py':[
223-
974,
224-
976,
225-
987,
226-
],
227180
'project:twisted-12.1.0/twisted/trial/test/test_testcase.py':[
228181
37,
229182
],

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.sonar.plugins.python.api.tree.NumericLiteral;
3030
import org.sonar.plugins.python.api.tree.Token;
3131
import org.sonar.plugins.python.api.tree.Tree;
32+
import org.sonar.python.tree.TreeUtils;
3233

3334
@Rule(key = "S1764")
3435
public class IdenticalExpressionOnBinaryOperatorCheck extends PythonSubscriptionCheck {
@@ -46,6 +47,9 @@ private static void checkBinaryExpression(SubscriptionContext ctx) {
4647
Expression leftOperand = binaryExpression.leftOperand();
4748
Expression rightOperand = binaryExpression.rightOperand();
4849
Token operator = binaryExpression.operator();
50+
if (leftOperand.is(Tree.Kind.CALL_EXPR) || TreeUtils.hasDescendant(leftOperand, t -> t.is(Tree.Kind.CALL_EXPR))) {
51+
return;
52+
}
4953
if (CheckUtils.areEquivalent(leftOperand, rightOperand) && !isLeftShiftBy1(leftOperand, operator)) {
5054
ctx.addIssue(rightOperand, "Correct one of the identical sub-expressions on both sides of operator \"" + operator.value() + "\".")
5155
.secondary(leftOperand, "");

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@ def work():
4141
exclusion = 1 << 1
4242
exclusion2 = (a * b) << 1
4343
result = x @ x #compliant matrix operator should not raise issue on this rule.
44+
45+
def foo(): ...
46+
47+
class MyClass:
48+
def bar(): ...
49+
50+
def no_issues_on_function_calls():
51+
if foo() == foo(): ...
52+
if foo().x == foo().x: ...
53+
if MyClass() == MyClass(): ...
54+
my_class = MyClass()
55+
if my_class.bar() == my_class.bar(): ...

0 commit comments

Comments
 (0)