Skip to content

Commit d53aedd

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

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.sonar.plugins.python.api.tree.ComprehensionExpression;
2929
import org.sonar.plugins.python.api.tree.Expression;
3030
import org.sonar.plugins.python.api.tree.RegularArgument;
31+
import org.sonar.plugins.python.api.tree.SubscriptionExpression;
3132
import org.sonar.plugins.python.api.tree.Tree;
3233
import org.sonar.plugins.python.api.tree.Tree.Kind;
3334
import org.sonar.python.checks.utils.IsComprehensionTransformedChecker;
@@ -87,7 +88,7 @@ private void checkCalls(SubscriptionContext ctx) {
8788
}
8889

8990
var collectionType = collectionTypeCheckerMap.getForType(callee.typeV2());
90-
if (collectionType == null) {
91+
if (collectionType == null || callee instanceof SubscriptionExpression) {
9192
return;
9293
}
9394

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def test_list():
4848

4949
l7 = list({x:x for x in [1,2]}) # Noncompliant {{Replace this list constructor call by a list literal.}}
5050

51+
l8 = list[int | None]([None]) # Compliant: list call is used to specify the list generic type
52+
5153
def test_set():
5254
s = set(x * 2 for x in [1,2,3]) # Compliant
5355

0 commit comments

Comments
 (0)