Skip to content

Commit 8e6bd36

Browse files
SONARPY-2070 Fix NPE when type inference visit is missing (#1928)
1 parent 4fc70a7 commit 8e6bd36

File tree

8 files changed

+22
-6
lines changed

8 files changed

+22
-6
lines changed

python-frontend/src/main/java/org/sonar/python/semantic/v2/types/TrivialTypeInferenceVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ public void visitImportFrom(ImportFrom importFrom) {
353353
@Override
354354
public void visitAssignmentStatement(AssignmentStatement assignmentStatement) {
355355
scan(assignmentStatement.assignedValue());
356+
scan(assignmentStatement.lhsExpressions());
356357
Optional.of(assignmentStatement)
357358
.map(AssignmentStatement::lhsExpressions)
358359
.filter(lhs -> lhs.size() == 1)

python-frontend/src/main/java/org/sonar/python/tree/ComprehensionExpressionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class ComprehensionExpressionImpl extends PyTree implements Comprehension
4444
private final ComprehensionFor comprehensionFor;
4545
private final Token closingToken;
4646
private Set<Symbol> symbols = new HashSet<>();
47-
private PythonType pythonType;
47+
private PythonType pythonType = PythonType.UNKNOWN;
4848

4949
public ComprehensionExpressionImpl(Kind kind, @Nullable Token openingToken, Expression resultExpression,
5050
ComprehensionFor compFor, @Nullable Token closingToken) {

python-frontend/src/main/java/org/sonar/python/tree/DictCompExpressionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class DictCompExpressionImpl extends PyTree implements DictCompExpression
4444
private final ComprehensionFor comprehensionFor;
4545
private final Token closingBrace;
4646
private Set<Symbol> symbols = new HashSet<>();
47-
private PythonType pythonType;
47+
private PythonType pythonType = PythonType.UNKNOWN;
4848

4949
public DictCompExpressionImpl(Token openingBrace, Expression keyExpression, Token colon, Expression valueExpression,
5050
ComprehensionFor compFor, Token closingBrace) {

python-frontend/src/main/java/org/sonar/python/tree/ListLiteralImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class ListLiteralImpl extends PyTree implements ListLiteral {
3636
private final ExpressionList elements;
3737
private final Token rightBracket;
3838

39-
private PythonType typeV2;
39+
private PythonType typeV2 = PythonType.UNKNOWN;
4040

4141
public ListLiteralImpl(Token leftBracket, ExpressionList elements, Token rightBracket) {
4242
this.leftBracket = leftBracket;

python-frontend/src/main/java/org/sonar/python/tree/NoneExpressionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
public class NoneExpressionImpl extends PyTree implements NoneExpression {
3333
private final Token none;
34-
PythonType pythonType;
34+
PythonType pythonType = PythonType.UNKNOWN;
3535

3636
public NoneExpressionImpl(Token none) {
3737
this.none = none;

python-frontend/src/main/java/org/sonar/python/tree/NumericLiteralImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class NumericLiteralImpl extends PyTree implements NumericLiteral {
3636
private final Token token;
3737
private final InferredType type;
3838

39-
private PythonType typeV2;
39+
private PythonType typeV2 = PythonType.UNKNOWN;
4040

4141
NumericLiteralImpl(Token token) {
4242
this.token = token;

python-frontend/src/main/java/org/sonar/python/tree/StringLiteralImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class StringLiteralImpl extends PyTree implements StringLiteral {
3737

3838
private final List<StringElement> stringElements;
3939
private static final Set<String> BYTES_PREFIXES = new HashSet<>(Arrays.asList("b", "B", "br", "Br", "bR", "BR", "rb", "rB", "Rb", "RB"));
40-
private PythonType typeV2;
40+
private PythonType typeV2 = PythonType.UNKNOWN;
4141

4242
StringLiteralImpl(List<StringElement> stringElements) {
4343
this.stringElements = stringElements;

python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.sonar.plugins.python.api.tree.RegularArgument;
4949
import org.sonar.plugins.python.api.tree.Statement;
5050
import org.sonar.plugins.python.api.tree.StatementList;
51+
import org.sonar.plugins.python.api.tree.StringLiteral;
5152
import org.sonar.plugins.python.api.tree.Tree;
5253
import org.sonar.python.PythonTestUtils;
5354
import org.sonar.python.semantic.ClassSymbolImpl;
@@ -2329,6 +2330,20 @@ def foo():
23292330
Assertions.assertThat(fType).isSameAs(PythonType.UNKNOWN);
23302331
}
23312332

2333+
@Test
2334+
void assignmentStatementLhsTypeTest() {
2335+
var fileInput = inferTypes("""
2336+
def foo():
2337+
subscription[call('goal_{}'.format(1))] = 1
2338+
""");
2339+
2340+
var literal = TreeUtils.firstChild(fileInput, StringLiteral.class::isInstance)
2341+
.map(StringLiteral.class::cast)
2342+
.get();
2343+
2344+
var type = literal.typeV2();
2345+
Assertions.assertThat(type.unwrappedType()).isSameAs(STR_TYPE);
2346+
}
23322347

23332348
private static FileInput inferTypes(String lines) {
23342349
return inferTypes(lines, PROJECT_LEVEL_TYPE_TABLE);

0 commit comments

Comments
 (0)