Skip to content

Commit fd85bb8

Browse files
committed
fix NPE
1 parent 7c70098 commit fd85bb8

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/main/java/spoon/reflect/visitor/filter/PotentialVariableDeclarationFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private record VariableScope(CtVariable<?> variable, CtElement element) implemen
248248
// the parent `CtBinaryOperator`, the variables introduced by the branch `CtExpression b` will become relevant too.
249249
//
250250
// The function below can be used to explore the variables introduced by that unexplored branch `CtExpression b`.
251-
private static List<Scope> exploreBranchForNewScopes(CtQueryable branch) {
251+
private static List<Scope> exploreBranchForNewScopes(@NonNull CtQueryable branch) {
252252
List<Scope> result = new ArrayList<>();
253253

254254
// Where pattern variables apply changes depending on their parents (e.g. !(< pattern >) will change the matches),
@@ -306,14 +306,14 @@ private static List<Scope> exploreBranchForNewScopes(CtQueryable branch) {
306306
}
307307

308308
@SuppressWarnings("unchecked")
309-
private static List<PatternScope> exploreBranchForNewPatternScopes(CtExpression<?> branch) {
309+
private static List<PatternScope> exploreBranchForNewPatternScopes(@NonNull CtExpression<?> branch) {
310310
// SAFETY: In an expression only PatternScope can appear
311311
// -> the returns of exploreBranchForNewScopes is guaranteed to be a list of PatternScope
312312
return (List<PatternScope>) (List<?>) exploreBranchForNewScopes(branch);
313313
}
314314

315315
@SuppressWarnings("unchecked")
316-
private static List<PatternScope> exploreBranchForNewPatternScopes(CtPattern branch) {
316+
private static List<PatternScope> exploreBranchForNewPatternScopes(@NonNull CtPattern branch) {
317317
// SAFETY: In a pattern only PatternScope can appear
318318
// -> the returns of exploreBranchForNewScopes is guaranteed to be a list of PatternScope
319319
return (List<PatternScope>) (List<?>) exploreBranchForNewScopes(branch);
@@ -577,7 +577,7 @@ private static List<Scope> updatePatternScopesForParent(Collection<PatternScope>
577577
return result;
578578
}
579579

580-
if (parent instanceof CtFor ctFor) {
580+
if (parent instanceof CtFor ctFor && ctFor.getExpression() != null) {
581581
List<Scope> result = new ArrayList<>();
582582

583583
// The following rules apply to a basic for statement (§14.14.1):

src/test/java/spoon/test/query_function/VariableReferencesTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,24 @@ public void testIfWithNullThen(@BySimpleName("Test") CtClass<?> ctClass) {
588588
// System.out
589589
assertThat(references.get(2)).hasExactlyPotentialDeclarations(patternVar, fieldVariable); // println(s)
590590
}
591+
592+
@ModelTest(code = """
593+
class Test {
594+
void method() {
595+
int i = 0;
596+
for (; ;) {
597+
System.out.println(i);
598+
}
599+
}
600+
}
601+
""")
602+
public void testForConditionNull(@BySimpleName("Test") CtClass<?> ctClass) {
603+
// contract: the code must not crash if the condition in the for is null
604+
List<CtVariable<?>> variables = ctClass.getElements(new TypeFilter<>(CtVariable.class));
605+
List<CtVariableReference<?>> references = ctClass.getElements(new TypeFilter<>(CtVariableReference.class));
606+
607+
assertThat(variables).hasSize(1);
608+
609+
assertThat(references.get(1)).hasExactlyPotentialDeclarations(variables.get(0));
610+
}
591611
}

0 commit comments

Comments
 (0)