Skip to content

Commit

Permalink
Code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
smillst committed Sep 27, 2023
1 parent 0138c87 commit b183000
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType;
import org.checkerframework.javacutil.AnnotationMirrorSet;
import org.checkerframework.javacutil.TreeUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.CaseUtils;

/** The visitor for Fenum Checker. */
public class FenumVisitor extends BaseTypeVisitor<FenumAnnotatedTypeFactory> {
Expand Down
13 changes: 13 additions & 0 deletions checker/tests/nullness/java21/FlowSwitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@

public class FlowSwitch {

void test0(Number n) {
String s = null;
switch (n) {
case null, default: {
// TODO: this should issue a dereference of nullable error.
n.toString();
s = "";
}
}
s.toString();
}

void test1(Integer i) {
String msg = null;
switch (i) {
Expand Down Expand Up @@ -36,6 +48,7 @@ void test2(Integer i) {
msg = "pos";
break;
}
msg.toString();
}

class A {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@
import org.checkerframework.javacutil.SystemUtil;
import org.checkerframework.javacutil.TreePathUtil;
import org.checkerframework.javacutil.TreeUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.DeconstructionPatternUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.InstanceOfUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.YieldUtils;
import org.checkerframework.javacutil.TypeAnnotationUtils;
import org.checkerframework.javacutil.TypeKindUtils;
import org.checkerframework.javacutil.TypesUtils;
import org.checkerframework.javacutil.trees.TreeBuilder;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.DeconstructionPatternUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.InstanceOfUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.YieldUtils;
import org.plumelib.util.ArrayMap;
import org.plumelib.util.ArraySet;
import org.plumelib.util.CollectionsPlume;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@
import org.checkerframework.javacutil.TreePathUtil;
import org.checkerframework.javacutil.TreeUtils;
import org.checkerframework.javacutil.TreeUtils.MemberReferenceKind;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.InstanceOfUtils;
import org.checkerframework.javacutil.TypesUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.InstanceOfUtils;
import org.plumelib.util.ArrayMap;
import org.plumelib.util.ArraySet;
import org.plumelib.util.ArraysPlume;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.util.HashSet;
import java.util.Set;
import org.checkerframework.javacutil.TreeUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.SwitchExpressionUtils;

/**
* After this visitor visits a tree, {@link #getTrees} returns all the trees that should match with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.javacutil.BugInCF;
import org.checkerframework.javacutil.TreeUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.InstanceOfUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.YieldUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.InstanceOfUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.YieldUtils;

/**
* A visitor that processes javac trees and JavaParser nodes simultaneously, matching corresponding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.YieldUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.YieldUtils;

/**
* A class that visits each result expression of a switch expression and calls {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.signature.qual.FullyQualifiedName;
import org.checkerframework.dataflow.qual.Pure;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.InstanceOfUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.trees.TreeUtilsAfterJava11.YieldUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.BindingPatternUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.CaseUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.InstanceOfUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.YieldUtils;
import org.plumelib.util.CollectionsPlume;
import org.plumelib.util.UniqueIdMap;

Expand Down Expand Up @@ -2220,13 +2220,14 @@ public static boolean sameTree(ExpressionTree expr1, ExpressionTree expr2) {
}

/**
* Returns true if this is the default case for a switch statement or expression.
* Returns true if this is the default case for a switch statement or expression. (Also, returns
* true if {@code caseTree} is {@code case null, default:}.)
*
* @param caseTree a case tree
* @return true if {@code caseTree} is the default case for a switch statement or expression
*/
public static boolean isDefaultCaseTree(CaseTree caseTree) {
return CaseUtils.getLabels(caseTree).isEmpty();
return CaseUtils.isDefaultCaseTree(caseTree);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.checkerframework.javacutil.trees;
package org.checkerframework.javacutil;

import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ExpressionTree;
Expand All @@ -13,8 +13,6 @@
import javax.lang.model.SourceVersion;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.signature.qual.ClassGetName;
import org.checkerframework.javacutil.BugInCF;
import org.checkerframework.javacutil.TreeUtils;

/**
* This class contains util methods for reflective accessing Tree classes and methods that were
Expand Down Expand Up @@ -116,14 +114,39 @@ public static boolean isCaseRule(CaseTree caseTree) {
}

/**
* Get the list of labels from a case expression. For {@code default}, this is empty. Otherwise,
* in JDK 11 and earlier, this is a singleton list of expression trees. In JDK 12, this is a
* list of expression trees. In JDK 21+, this is a list of expression and pattern trees.
* Get the list of labels from a case expression. For {@code default}, this is empty. For {@code
* case null, default}, the list contains {@code null}. Otherwise, in JDK 11 and earlier, this
* is a list of a single expression tree. In JDK 12+, the list may have multiple expression
* trees. In JDK 21+, the list might contain a single pattern tree.
*
* @param caseTree the case expression to get the labels from
* @return the list of case labels in the case
*/
public static List<? extends Tree> getLabels(CaseTree caseTree) {
return getLabels(caseTree, false);
}

/**
* Returns true if this is the default case for a switch statement or expression. (Also, returns
* true if {@code caseTree} is {@code case null, default:}.)
*
* @param caseTree a case tree
* @return true if {@code caseTree} is the default case for a switch statement or expression
*/
public static boolean isDefaultCaseTree(CaseTree caseTree) {
if (sourceVersionNumber >= 21) {
for (Tree label : getLabels(caseTree, true)) {
if (TreeUtils.isDefaultCaseLabelTree(label)) {
return true;
}
}
return false;
} else {
return getExpressions(caseTree).isEmpty();
}
}

private static List<? extends Tree> getLabels(CaseTree caseTree, boolean useDefault) {
if (sourceVersionNumber >= 21) {
if (GET_LABELS == null) {
GET_LABELS = getMethod(CaseTree.class, "getLabels");
Expand All @@ -134,7 +157,9 @@ public static List<? extends Tree> getLabels(CaseTree caseTree) {
List<Tree> labels = new ArrayList<>();
for (Tree caseLabel : caseLabelTrees) {
if (TreeUtils.isDefaultCaseLabelTree(caseLabel)) {
return Collections.emptyList();
if (useDefault) {
labels.add(caseLabel);
}
} else if (TreeUtils.isConstantCaseLabelTree(caseLabel)) {
labels.add(ConstantCaseLabelUtils.getConstantExpression(caseLabel));
} else if (TreeUtils.isPatternCaseLabelTree(caseLabel)) {
Expand Down Expand Up @@ -184,7 +209,7 @@ public static List<? extends ExpressionTree> getExpressions(CaseTree caseTree) {
if (GET_GUARD == null) {
GET_GUARD = getMethod(CaseTree.class, "getGuard");
}
return (ExpressionTree) invokeNonNullResult(GET_GUARD, caseTree);
return (ExpressionTree) invoke(GET_GUARD, caseTree);
}
}

Expand Down

0 comments on commit b183000

Please sign in to comment.