Skip to content

Commit ee9865c

Browse files
maksim-grebeniuk-sonarsourcesonartech
authored andcommitted
SONARPY-3127 Fix S7498 Rule: should not raise if an type annotation is present
GitOrigin-RevId: a823fc6e9ab59551d5653bedd953c57ba287f019
1 parent d53aedd commit ee9865c

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.sonar.plugins.python.api.tree.CallExpression;
2626
import org.sonar.plugins.python.api.tree.Expression;
2727
import org.sonar.plugins.python.api.tree.RegularArgument;
28+
import org.sonar.plugins.python.api.tree.SubscriptionExpression;
2829
import org.sonar.plugins.python.api.tree.Tree;
2930
import org.sonar.plugins.python.api.types.v2.TriBool;
3031
import org.sonar.python.quickfix.TextEditUtils;
@@ -73,7 +74,7 @@ private boolean isUnnecessaryCollectionConstructor(CallExpression callExpression
7374

7475
private boolean isCollectionConstructor(Expression calleeExpression) {
7576
var type = calleeExpression.typeV2();
76-
return collectionConstructorTypeCheckers.getOptionalForType(type).isPresent();
77+
return collectionConstructorTypeCheckers.getOptionalForType(type).isPresent() && !(calleeExpression instanceof SubscriptionExpression);
7778
}
7879

7980
private static boolean isEmptyCall(CallExpression callExpression) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ def dict(): pass
4949
tuple()
5050
set()
5151
dict()
52+
53+
def empty_collection_with_generic_type_hint():
54+
list[int | None]()

0 commit comments

Comments
 (0)