Skip to content

Commit

Permalink
SONARPY-2296 Fix quality issues: ensure calls to Name#symbolV2 are ch… (
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-dequenne-sonarsource authored Nov 1, 2024
1 parent e6ec935 commit 9637258
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.sonar.plugins.python.api.tree.Tree;
import org.sonar.python.cfg.fixpoint.ForwardAnalysis;
import org.sonar.python.cfg.fixpoint.ProgramState;
import org.sonar.python.semantic.v2.ProjectLevelTypeTable;
import org.sonar.python.semantic.v2.SymbolV2;
import org.sonar.python.semantic.v2.TypeTable;
import org.sonar.python.types.v2.PythonType;
Expand Down Expand Up @@ -123,9 +122,13 @@ private void handleParameter(Parameter parameter, TypeInferenceProgramState stat
if (name == null || !trackedVars.contains(name.symbolV2())) {
return;
}
SymbolV2 symbol = name.symbolV2();
if (symbol == null) {
return;
}

var type = parameterTypesByName.getOrDefault(name.name(), PythonType.UNKNOWN);
state.setTypes(name.symbolV2(), new HashSet<>(Set.of(type)));
state.setTypes(symbol, new HashSet<>(Set.of(type)));
updateTree(name, state);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public void visitFunctionDef(FunctionDef functionDef) {
super.visitFunctionDef(functionDef);
Name name = functionDef.name();
var symbol = name.symbolV2();
if (symbol == null) {
return;
}
Definition definition = new Definition(symbol, name);
definitionsByDefinitionStatement.computeIfAbsent(functionDef, k -> new HashSet<>()).add(definition);
propagationsByLhs.computeIfAbsent(symbol, s -> new HashSet<>()).add(definition);
Expand All @@ -84,6 +87,9 @@ public void visitClassDef(ClassDef classDef) {
super.visitClassDef(classDef);
var name = classDef.name();
var symbol = name.symbolV2();
if (symbol == null) {
return;
}
var definition = new Definition(symbol, name);
definitionsByDefinitionStatement.computeIfAbsent(classDef, k -> new HashSet<>()).add(definition);
propagationsByLhs.computeIfAbsent(symbol, s -> new HashSet<>()).add(definition);
Expand Down Expand Up @@ -186,8 +192,11 @@ public void visitForStatement(ForStatement forStatement) {
}

private void processAssignment(Statement assignmentStatement, Expression lhsExpression, Expression rhsExpression){
if (lhsExpression instanceof Name lhs && lhs.symbolV2() != null) {
if (lhsExpression instanceof Name lhs) {
var symbol = lhs.symbolV2();
if (symbol == null) {
return;
}
var assignment = new Assignment(symbol, lhs, rhsExpression, propagationsByLhs);
assignmentsByAssignmentStatement.put(assignmentStatement, assignment);
propagationsByLhs.computeIfAbsent(symbol, s -> new HashSet<>()).add(assignment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,11 @@ private ClassType buildClassType(ClassDef classDef) {
ClassType classType = classTypeBuilder.build();

if (currentType() instanceof ClassType ownerClass) {
PythonType memberType = className.symbolV2().hasSingleBindingUsage() ? classType : PythonType.UNKNOWN;
ownerClass.members().add(new Member(classType.name(), memberType));
SymbolV2 symbolV2 = className.symbolV2();
if (symbolV2 != null) {
PythonType memberType = symbolV2.hasSingleBindingUsage() ? classType : PythonType.UNKNOWN;
ownerClass.members().add(new Member(classType.name(), memberType));
}
}
return classType;
}
Expand Down Expand Up @@ -335,8 +338,9 @@ private FunctionType buildFunctionType(FunctionDef functionDef) {
functionTypeBuilder.withTypeOrigin(TypeOrigin.LOCAL);
}
FunctionType functionType = functionTypeBuilder.build();
if (owner != null) {
if (functionDef.name().symbolV2().hasSingleBindingUsage()) {
SymbolV2 symbolV2 = functionDef.name().symbolV2();
if (owner != null && symbolV2 != null) {
if (symbolV2.hasSingleBindingUsage()) {
owner.members().add(new Member(functionType.name(), functionType));
} else {
owner.members().add(new Member(functionType.name(), PythonType.UNKNOWN));
Expand Down

0 comments on commit 9637258

Please sign in to comment.