Skip to content

Commit d23b808

Browse files
SONARPY-498 Fix False Positive S4143: assigned collection in rhs (SonarSource#559)
1 parent 5a7934f commit d23b808

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,4 @@
3030
'project:twisted-12.1.0/twisted/test/test_dirdbm.py':[
3131
26,
3232
],
33-
'project:twisted-12.1.0/twisted/words/protocols/irc.py':[
34-
2910,
35-
],
3633
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ private static CollectionWrite collectionWrite(AssignmentStatement assignment, E
106106
if (collection.is(Kind.SLICE_EXPR, Kind.SUBSCRIPTION)) {
107107
CollectionWrite nested = collectionWrite(assignment, collection);
108108
if (nested != null) {
109-
return new CollectionWrite(nested.collectionKey.nest(key), nested.leftBracket, rBracket, assignment);
109+
return new CollectionWrite(nested.collectionKey.nest(key), nested.leftBracket, rBracket, assignment, collection);
110110
}
111111
}
112112

113113
if (collection instanceof HasSymbol) {
114114
Symbol symbol = ((HasSymbol) collection).symbol();
115115
if (symbol != null) {
116116
CollectionKey collectionKey = new CollectionKey(symbol, key);
117-
return new CollectionWrite(collectionKey, lBracket, rBracket, assignment);
117+
return new CollectionWrite(collectionKey, lBracket, rBracket, assignment, collection);
118118
}
119119
}
120120

@@ -163,8 +163,7 @@ private static void reportOverwrites(SubscriptionContext ctx, Map<CollectionKey,
163163
CollectionWrite firstWrite = writes.get(0);
164164
CollectionWrite secondWrite = writes.get(1);
165165
AssignmentStatement assignment = secondWrite.assignment;
166-
Expression lhs = lhs(assignment);
167-
if (TreeUtils.hasDescendant(assignment.assignedValue(), t -> CheckUtils.areEquivalent(lhs, t))) {
166+
if (TreeUtils.hasDescendant(assignment.assignedValue(), t -> CheckUtils.areEquivalent(firstWrite.collection, t))) {
168167
return;
169168
}
170169
String message = String.format(
@@ -192,12 +191,14 @@ private static class CollectionWrite {
192191
private final Token leftBracket;
193192
private final Token rightBracket;
194193
private final AssignmentStatement assignment;
194+
private final Expression collection;
195195

196-
private CollectionWrite(CollectionKey collectionKey, Token leftBracket, Token rightBracket, AssignmentStatement assignment) {
196+
private CollectionWrite(CollectionKey collectionKey, Token leftBracket, Token rightBracket, AssignmentStatement assignment, Expression collection) {
197197
this.collectionKey = collectionKey;
198198
this.leftBracket = leftBracket;
199199
this.rightBracket = rightBracket;
200200
this.assignment = assignment;
201+
this.collection = collection;
201202
}
202203
}
203204
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ def non_trivial_collections(f, obj1, obj2):
9191
def valid_replacement(list1, foo, x):
9292
list1["foo"] = foo(x)
9393
list1["foo"] = list1["foo"].replace("a", "b")
94+
95+
def used_collection(list1):
96+
list1[1] = 42
97+
list1[1] = foo(list1[1])
98+
99+
list1[2] = 42
100+
list1[2] = foo(list1)
101+
102+
list[3] = 42
103+
list[3] = list[1] # FN

0 commit comments

Comments
 (0)