Skip to content

Commit

Permalink
Run StatementSwitchToExpressionSwitch_refactoring over EP.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 696913777
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 15, 2024
1 parent 37895d3 commit 2afd0cf
Show file tree
Hide file tree
Showing 135 changed files with 1,423 additions and 1,942 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@ public static void validate(BugPattern pattern) throws ValidationException {

// linkType must be consistent with link element.
switch (pattern.linkType()) {
case CUSTOM:
case CUSTOM -> {
if (pattern.link().isEmpty()) {
throw new ValidationException("Expected a custom link but none was provided");
}
break;
case AUTOGENERATED:
case NONE:
}
case AUTOGENERATED, NONE -> {
if (!pattern.link().isEmpty()) {
throw new ValidationException("Expected no custom link but found: " + pattern.link());
}
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,11 @@ private static ImmutableList<String> defaultToLatestSupportedLanguageLevel(

String overrideLanguageLevel;
switch (JAVA_SPECIFICATION_VERSION.value()) {
case "1.7":
overrideLanguageLevel = "7";
break;
case "1.8":
overrideLanguageLevel = "8";
break;
default:
case "1.7" -> overrideLanguageLevel = "7";
case "1.8" -> overrideLanguageLevel = "8";
default -> {
return args;
}
}

return ImmutableList.<String>builder()
Expand All @@ -169,15 +166,13 @@ static void checkCompilePolicy(@Nullable String compilePolicy) {
+ " pass -XDcompilePolicy=simple instead");
}
switch (compilePolicy) {
case "byfile":
case "simple":
break;
default:
throw new InvalidCommandLineOptionException(
String.format(
"-XDcompilePolicy=%s is not supported by Error Prone,"
+ " pass -XDcompilePolicy=simple instead",
compilePolicy));
case "byfile", "simple" -> {}
default ->
throw new InvalidCommandLineOptionException(
String.format(
"-XDcompilePolicy=%s is not supported by Error Prone,"
+ " pass -XDcompilePolicy=simple instead",
compilePolicy));
}
}

Expand Down
17 changes: 7 additions & 10 deletions check_api/src/main/java/com/google/errorprone/BugCheckerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,19 @@ public BugCheckerInfo withCustomDefaultSeverity(SeverityLevel defaultSeverity) {
}

private static @Nullable String createLinkUrl(String canonicalName, BugPattern pattern) {
switch (pattern.linkType()) {
case AUTOGENERATED:
return String.format("https://errorprone.info/bugpattern/%s", canonicalName);
case CUSTOM:
return switch (pattern.linkType()) {
case AUTOGENERATED -> String.format("https://errorprone.info/bugpattern/%s", canonicalName);
case CUSTOM -> {
// annotation.link() must be provided.
if (pattern.link().isEmpty()) {
throw new IllegalStateException(
"If linkType element of @BugPattern is CUSTOM, "
+ "a link element must also be provided.");
}
return pattern.link();
case NONE:
return null;
}
throw new AssertionError(
"Unexpected value for linkType element of @BugPattern: " + pattern.linkType());
yield pattern.link();
}
case NONE -> null;
};
}

public String canonicalName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,20 @@ private boolean finishedCompilation(CompilationUnitTree tree) {
OUTER:
for (Tree decl : tree.getTypeDecls()) {
switch (decl.getKind()) {
case EMPTY_STATEMENT:
case EMPTY_STATEMENT -> {
// ignore ";" at the top level, which counts as an empty type decl
continue OUTER;
case IMPORT:
}
case IMPORT -> {
// The spec disallows mixing imports and empty top-level declarations (";"), but
// javac has a bug that causes it to accept empty declarations interspersed with imports:
// http://mail.openjdk.java.net/pipermail/compiler-dev/2013-August/006968.html
//
// Any import declarations after the first semi are incorrectly added to the list
// of type declarations, so we have to skip over them here.
continue OUTER;
default:
break;
}
default -> {}
}
if (!seen.contains(decl)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,37 +411,18 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
Builder builder = new Builder();
for (String arg : args) {
switch (arg) {
case IGNORE_SUPPRESSION_ANNOTATIONS:
builder.setIgnoreSuppressionAnnotations(true);
break;
case IGNORE_UNKNOWN_CHECKS_FLAG:
builder.setIgnoreUnknownChecks(true);
break;
case DISABLE_WARNINGS_IN_GENERATED_CODE_FLAG:
builder.setDisableWarningsInGeneratedCode(true);
break;
case ERRORS_AS_WARNINGS_FLAG:
builder.setDropErrorsToWarnings(true);
break;
case SUGGESTIONS_AS_WARNINGS_FLAG:
builder.setSuggestionsAsWarnings(true);
break;
case ENABLE_ALL_CHECKS:
builder.setEnableAllChecksAsWarnings(true);
break;
case DISABLE_ALL_CHECKS:
builder.setDisableAllChecks(true);
break;
case COMPILING_TEST_ONLY_CODE:
builder.setTestOnlyTarget(true);
break;
case COMPILING_PUBLICLY_VISIBLE_CODE:
builder.setPubliclyVisibleTarget(true);
break;
case DISABLE_ALL_WARNINGS:
builder.setDisableAllWarnings(true);
break;
default:
case IGNORE_SUPPRESSION_ANNOTATIONS -> builder.setIgnoreSuppressionAnnotations(true);
case IGNORE_UNKNOWN_CHECKS_FLAG -> builder.setIgnoreUnknownChecks(true);
case DISABLE_WARNINGS_IN_GENERATED_CODE_FLAG ->
builder.setDisableWarningsInGeneratedCode(true);
case ERRORS_AS_WARNINGS_FLAG -> builder.setDropErrorsToWarnings(true);
case SUGGESTIONS_AS_WARNINGS_FLAG -> builder.setSuggestionsAsWarnings(true);
case ENABLE_ALL_CHECKS -> builder.setEnableAllChecksAsWarnings(true);
case DISABLE_ALL_CHECKS -> builder.setDisableAllChecks(true);
case COMPILING_TEST_ONLY_CODE -> builder.setTestOnlyTarget(true);
case COMPILING_PUBLICLY_VISIBLE_CODE -> builder.setPubliclyVisibleTarget(true);
case DISABLE_ALL_WARNINGS -> builder.setDisableAllWarnings(true);
default -> {
if (arg.startsWith(SEVERITY_PREFIX)) {
builder.parseSeverity(arg);
} else if (arg.startsWith(ErrorProneFlags.PREFIX)) {
Expand Down Expand Up @@ -494,6 +475,7 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
}
remainingArgs.add(arg);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,14 @@ public final class ImportOrderParser {
* @return the {@link ImportOrganizer}
*/
public static ImportOrganizer getImportOrganizer(String importOrder) {
switch (importOrder) {
case "static-first":
return ImportOrganizer.STATIC_FIRST_ORGANIZER;
case "static-last":
return ImportOrganizer.STATIC_LAST_ORGANIZER;
case "android-static-first":
return ImportOrganizer.ANDROID_STATIC_FIRST_ORGANIZER;
case "android-static-last":
return ImportOrganizer.ANDROID_STATIC_LAST_ORGANIZER;
case "idea":
return ImportOrganizer.IDEA_ORGANIZER;
default:
throw new IllegalStateException("Unknown import order: '" + importOrder + "'");
}
return switch (importOrder) {
case "static-first" -> ImportOrganizer.STATIC_FIRST_ORGANIZER;
case "static-last" -> ImportOrganizer.STATIC_LAST_ORGANIZER;
case "android-static-first" -> ImportOrganizer.ANDROID_STATIC_FIRST_ORGANIZER;
case "android-static-last" -> ImportOrganizer.ANDROID_STATIC_LAST_ORGANIZER;
case "idea" -> ImportOrganizer.IDEA_ORGANIZER;
default -> throw new IllegalStateException("Unknown import order: '" + importOrder + "'");
};
}

private ImportOrderParser() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,16 @@ public void onDescribed(Description description) {
JavaFileObject originalSource = log.useSource(sourceFile);
try {
JCDiagnostic.Factory factory = JCDiagnostic.Factory.instance(context);
JCDiagnostic.DiagnosticType type = JCDiagnostic.DiagnosticType.ERROR;
DiagnosticPosition pos = description.position;
switch (description.severity()) {
case ERROR:
if (dontUseErrors) {
type = JCDiagnostic.DiagnosticType.WARNING;
} else {
type = JCDiagnostic.DiagnosticType.ERROR;
}
break;
case WARNING:
type = JCDiagnostic.DiagnosticType.WARNING;
break;
case SUGGESTION:
type = JCDiagnostic.DiagnosticType.NOTE;
break;
}
JCDiagnostic.DiagnosticType type =
switch (description.severity()) {
case ERROR ->
dontUseErrors
? JCDiagnostic.DiagnosticType.WARNING
: JCDiagnostic.DiagnosticType.ERROR;
case WARNING -> JCDiagnostic.DiagnosticType.WARNING;
case SUGGESTION -> JCDiagnostic.DiagnosticType.NOTE;
};
log.report(
factory.create(
type,
Expand Down
34 changes: 12 additions & 22 deletions check_api/src/main/java/com/google/errorprone/VisitorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,28 +627,18 @@ private static void validateTypeStr(String typeStr) {
* the corresponding Type, or null otherwise.
*/
private @Nullable Type getPrimitiveOrVoidType(String typeStr) {
switch (typeStr) {
case "byte":
return getSymtab().byteType;
case "short":
return getSymtab().shortType;
case "int":
return getSymtab().intType;
case "long":
return getSymtab().longType;
case "float":
return getSymtab().floatType;
case "double":
return getSymtab().doubleType;
case "boolean":
return getSymtab().booleanType;
case "char":
return getSymtab().charType;
case "void":
return getSymtab().voidType;
default:
return null;
}
return switch (typeStr) {
case "byte" -> getSymtab().byteType;
case "short" -> getSymtab().shortType;
case "int" -> getSymtab().intType;
case "long" -> getSymtab().longType;
case "float" -> getSymtab().floatType;
case "double" -> getSymtab().doubleType;
case "boolean" -> getSymtab().booleanType;
case "char" -> getSymtab().charType;
case "void" -> getSymtab().voidType;
default -> null;
};
}

/** Returns true if the compilation is targeting Android. */
Expand Down
23 changes: 11 additions & 12 deletions check_api/src/main/java/com/google/errorprone/apply/SourceFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,18 @@ public void replaceChars(int startPosition, int endPosition, String replacement)
void makeReplacements(Replacements changes) {
ImmutableSet<Replacement> replacements = changes.ascending();
switch (replacements.size()) {
case 0:
case 0 -> {
return;
case 1:
{
Replacement onlyReplacement = Iterables.getOnlyElement(replacements);
replaceChars(
onlyReplacement.startPosition(),
onlyReplacement.endPosition(),
onlyReplacement.replaceWith());
return;
}
default:
break;
}
case 1 -> {
Replacement onlyReplacement = Iterables.getOnlyElement(replacements);
replaceChars(
onlyReplacement.startPosition(),
onlyReplacement.endPosition(),
onlyReplacement.replaceWith());
return;
}
default -> {}
}

// Since we have many replacements to make all at once, it's better to start off with a clean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,25 @@ public BugChecker() {

private static BiPredicate<Set<? extends Name>, VisitorState> suppressionPredicate(
Set<Class<? extends Annotation>> suppressionClasses) {
switch (suppressionClasses.size()) {
case 0:
return (annos, state) -> false;
case 1:
{
Supplier<Name> self =
VisitorState.memoize(
state -> state.getName(Iterables.getOnlyElement(suppressionClasses).getName()));
return (annos, state) -> annos.contains(self.get(state));
}
default:
{
Supplier<Set<? extends Name>> self =
VisitorState.memoize(
state ->
suppressionClasses.stream()
.map(Class::getName)
.map(state::getName)
.collect(toImmutableSet()));
return (annos, state) -> !Collections.disjoint(self.get(state), annos);
}
}
return switch (suppressionClasses.size()) {
case 0 -> (annos, state) -> false;
case 1 -> {
Supplier<Name> self =
VisitorState.memoize(
state -> state.getName(Iterables.getOnlyElement(suppressionClasses).getName()));
yield (annos, state) -> annos.contains(self.get(state));
}
default -> {
Supplier<Set<? extends Name>> self =
VisitorState.memoize(
state ->
suppressionClasses.stream()
.map(Class::getName)
.map(state::getName)
.collect(toImmutableSet()));
yield (annos, state) -> !Collections.disjoint(self.get(state), annos);
}
};
}

/** Helper to create a Description for the common case where there is a fix. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,12 @@ public Nullness greatestLowerBound(Nullness other) {
* get the set of all values, or {@code NULLABLE}.
*/
public Nullness deducedValueWhenNotEqual() {
switch (this) {
case NULLABLE:
case NONNULL:
return NULLABLE;
case NULL:
return NONNULL;
case BOTTOM:
return BOTTOM;
default:
throw new AssertionError("Inverse of " + this + " not defined");
}
return switch (this) {
case NULLABLE, NONNULL -> NULLABLE;
case NULL -> NONNULL;
case BOTTOM -> BOTTOM;
default -> throw new AssertionError("Inverse of " + this + " not defined");
};
}

@Override
Expand Down
Loading

0 comments on commit 2afd0cf

Please sign in to comment.