Skip to content

Commit 2afd0cf

Browse files
graememorganError Prone Team
authored and
Error Prone Team
committed
Run StatementSwitchToExpressionSwitch_refactoring over EP.
PiperOrigin-RevId: 696913777
1 parent 37895d3 commit 2afd0cf

File tree

135 files changed

+1423
-1942
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+1423
-1942
lines changed

annotation/src/main/java/com/google/errorprone/BugPatternValidator.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,16 @@ public static void validate(BugPattern pattern) throws ValidationException {
3737

3838
// linkType must be consistent with link element.
3939
switch (pattern.linkType()) {
40-
case CUSTOM:
40+
case CUSTOM -> {
4141
if (pattern.link().isEmpty()) {
4242
throw new ValidationException("Expected a custom link but none was provided");
4343
}
44-
break;
45-
case AUTOGENERATED:
46-
case NONE:
44+
}
45+
case AUTOGENERATED, NONE -> {
4746
if (!pattern.link().isEmpty()) {
4847
throw new ValidationException("Expected no custom link but found: " + pattern.link());
4948
}
50-
break;
49+
}
5150
}
5251
}
5352

check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java

+11-16
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,11 @@ private static ImmutableList<String> defaultToLatestSupportedLanguageLevel(
139139

140140
String overrideLanguageLevel;
141141
switch (JAVA_SPECIFICATION_VERSION.value()) {
142-
case "1.7":
143-
overrideLanguageLevel = "7";
144-
break;
145-
case "1.8":
146-
overrideLanguageLevel = "8";
147-
break;
148-
default:
142+
case "1.7" -> overrideLanguageLevel = "7";
143+
case "1.8" -> overrideLanguageLevel = "8";
144+
default -> {
149145
return args;
146+
}
150147
}
151148

152149
return ImmutableList.<String>builder()
@@ -169,15 +166,13 @@ static void checkCompilePolicy(@Nullable String compilePolicy) {
169166
+ " pass -XDcompilePolicy=simple instead");
170167
}
171168
switch (compilePolicy) {
172-
case "byfile":
173-
case "simple":
174-
break;
175-
default:
176-
throw new InvalidCommandLineOptionException(
177-
String.format(
178-
"-XDcompilePolicy=%s is not supported by Error Prone,"
179-
+ " pass -XDcompilePolicy=simple instead",
180-
compilePolicy));
169+
case "byfile", "simple" -> {}
170+
default ->
171+
throw new InvalidCommandLineOptionException(
172+
String.format(
173+
"-XDcompilePolicy=%s is not supported by Error Prone,"
174+
+ " pass -XDcompilePolicy=simple instead",
175+
compilePolicy));
181176
}
182177
}
183178

check_api/src/main/java/com/google/errorprone/BugCheckerInfo.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,19 @@ public BugCheckerInfo withCustomDefaultSeverity(SeverityLevel defaultSeverity) {
173173
}
174174

175175
private static @Nullable String createLinkUrl(String canonicalName, BugPattern pattern) {
176-
switch (pattern.linkType()) {
177-
case AUTOGENERATED:
178-
return String.format("https://errorprone.info/bugpattern/%s", canonicalName);
179-
case CUSTOM:
176+
return switch (pattern.linkType()) {
177+
case AUTOGENERATED -> String.format("https://errorprone.info/bugpattern/%s", canonicalName);
178+
case CUSTOM -> {
180179
// annotation.link() must be provided.
181180
if (pattern.link().isEmpty()) {
182181
throw new IllegalStateException(
183182
"If linkType element of @BugPattern is CUSTOM, "
184183
+ "a link element must also be provided.");
185184
}
186-
return pattern.link();
187-
case NONE:
188-
return null;
189-
}
190-
throw new AssertionError(
191-
"Unexpected value for linkType element of @BugPattern: " + pattern.linkType());
185+
yield pattern.link();
186+
}
187+
case NONE -> null;
188+
};
192189
}
193190

194191
public String canonicalName() {

check_api/src/main/java/com/google/errorprone/ErrorProneAnalyzer.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,20 @@ private boolean finishedCompilation(CompilationUnitTree tree) {
272272
OUTER:
273273
for (Tree decl : tree.getTypeDecls()) {
274274
switch (decl.getKind()) {
275-
case EMPTY_STATEMENT:
275+
case EMPTY_STATEMENT -> {
276276
// ignore ";" at the top level, which counts as an empty type decl
277277
continue OUTER;
278-
case IMPORT:
278+
}
279+
case IMPORT -> {
279280
// The spec disallows mixing imports and empty top-level declarations (";"), but
280281
// javac has a bug that causes it to accept empty declarations interspersed with imports:
281282
// http://mail.openjdk.java.net/pipermail/compiler-dev/2013-August/006968.html
282283
//
283284
// Any import declarations after the first semi are incorrectly added to the list
284285
// of type declarations, so we have to skip over them here.
285286
continue OUTER;
286-
default:
287-
break;
287+
}
288+
default -> {}
288289
}
289290
if (!seen.contains(decl)) {
290291
return false;

check_api/src/main/java/com/google/errorprone/ErrorProneOptions.java

+13-31
Original file line numberDiff line numberDiff line change
@@ -411,37 +411,18 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
411411
Builder builder = new Builder();
412412
for (String arg : args) {
413413
switch (arg) {
414-
case IGNORE_SUPPRESSION_ANNOTATIONS:
415-
builder.setIgnoreSuppressionAnnotations(true);
416-
break;
417-
case IGNORE_UNKNOWN_CHECKS_FLAG:
418-
builder.setIgnoreUnknownChecks(true);
419-
break;
420-
case DISABLE_WARNINGS_IN_GENERATED_CODE_FLAG:
421-
builder.setDisableWarningsInGeneratedCode(true);
422-
break;
423-
case ERRORS_AS_WARNINGS_FLAG:
424-
builder.setDropErrorsToWarnings(true);
425-
break;
426-
case SUGGESTIONS_AS_WARNINGS_FLAG:
427-
builder.setSuggestionsAsWarnings(true);
428-
break;
429-
case ENABLE_ALL_CHECKS:
430-
builder.setEnableAllChecksAsWarnings(true);
431-
break;
432-
case DISABLE_ALL_CHECKS:
433-
builder.setDisableAllChecks(true);
434-
break;
435-
case COMPILING_TEST_ONLY_CODE:
436-
builder.setTestOnlyTarget(true);
437-
break;
438-
case COMPILING_PUBLICLY_VISIBLE_CODE:
439-
builder.setPubliclyVisibleTarget(true);
440-
break;
441-
case DISABLE_ALL_WARNINGS:
442-
builder.setDisableAllWarnings(true);
443-
break;
444-
default:
414+
case IGNORE_SUPPRESSION_ANNOTATIONS -> builder.setIgnoreSuppressionAnnotations(true);
415+
case IGNORE_UNKNOWN_CHECKS_FLAG -> builder.setIgnoreUnknownChecks(true);
416+
case DISABLE_WARNINGS_IN_GENERATED_CODE_FLAG ->
417+
builder.setDisableWarningsInGeneratedCode(true);
418+
case ERRORS_AS_WARNINGS_FLAG -> builder.setDropErrorsToWarnings(true);
419+
case SUGGESTIONS_AS_WARNINGS_FLAG -> builder.setSuggestionsAsWarnings(true);
420+
case ENABLE_ALL_CHECKS -> builder.setEnableAllChecksAsWarnings(true);
421+
case DISABLE_ALL_CHECKS -> builder.setDisableAllChecks(true);
422+
case COMPILING_TEST_ONLY_CODE -> builder.setTestOnlyTarget(true);
423+
case COMPILING_PUBLICLY_VISIBLE_CODE -> builder.setPubliclyVisibleTarget(true);
424+
case DISABLE_ALL_WARNINGS -> builder.setDisableAllWarnings(true);
425+
default -> {
445426
if (arg.startsWith(SEVERITY_PREFIX)) {
446427
builder.parseSeverity(arg);
447428
} else if (arg.startsWith(ErrorProneFlags.PREFIX)) {
@@ -494,6 +475,7 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
494475
}
495476
remainingArgs.add(arg);
496477
}
478+
}
497479
}
498480
}
499481

check_api/src/main/java/com/google/errorprone/ImportOrderParser.java

+8-14
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,14 @@ public final class ImportOrderParser {
2727
* @return the {@link ImportOrganizer}
2828
*/
2929
public static ImportOrganizer getImportOrganizer(String importOrder) {
30-
switch (importOrder) {
31-
case "static-first":
32-
return ImportOrganizer.STATIC_FIRST_ORGANIZER;
33-
case "static-last":
34-
return ImportOrganizer.STATIC_LAST_ORGANIZER;
35-
case "android-static-first":
36-
return ImportOrganizer.ANDROID_STATIC_FIRST_ORGANIZER;
37-
case "android-static-last":
38-
return ImportOrganizer.ANDROID_STATIC_LAST_ORGANIZER;
39-
case "idea":
40-
return ImportOrganizer.IDEA_ORGANIZER;
41-
default:
42-
throw new IllegalStateException("Unknown import order: '" + importOrder + "'");
43-
}
30+
return switch (importOrder) {
31+
case "static-first" -> ImportOrganizer.STATIC_FIRST_ORGANIZER;
32+
case "static-last" -> ImportOrganizer.STATIC_LAST_ORGANIZER;
33+
case "android-static-first" -> ImportOrganizer.ANDROID_STATIC_FIRST_ORGANIZER;
34+
case "android-static-last" -> ImportOrganizer.ANDROID_STATIC_LAST_ORGANIZER;
35+
case "idea" -> ImportOrganizer.IDEA_ORGANIZER;
36+
default -> throw new IllegalStateException("Unknown import order: '" + importOrder + "'");
37+
};
4438
}
4539

4640
private ImportOrderParser() {}

check_api/src/main/java/com/google/errorprone/JavacErrorDescriptionListener.java

+9-16
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,16 @@ public void onDescribed(Description description) {
111111
JavaFileObject originalSource = log.useSource(sourceFile);
112112
try {
113113
JCDiagnostic.Factory factory = JCDiagnostic.Factory.instance(context);
114-
JCDiagnostic.DiagnosticType type = JCDiagnostic.DiagnosticType.ERROR;
115114
DiagnosticPosition pos = description.position;
116-
switch (description.severity()) {
117-
case ERROR:
118-
if (dontUseErrors) {
119-
type = JCDiagnostic.DiagnosticType.WARNING;
120-
} else {
121-
type = JCDiagnostic.DiagnosticType.ERROR;
122-
}
123-
break;
124-
case WARNING:
125-
type = JCDiagnostic.DiagnosticType.WARNING;
126-
break;
127-
case SUGGESTION:
128-
type = JCDiagnostic.DiagnosticType.NOTE;
129-
break;
130-
}
115+
JCDiagnostic.DiagnosticType type =
116+
switch (description.severity()) {
117+
case ERROR ->
118+
dontUseErrors
119+
? JCDiagnostic.DiagnosticType.WARNING
120+
: JCDiagnostic.DiagnosticType.ERROR;
121+
case WARNING -> JCDiagnostic.DiagnosticType.WARNING;
122+
case SUGGESTION -> JCDiagnostic.DiagnosticType.NOTE;
123+
};
131124
log.report(
132125
factory.create(
133126
type,

check_api/src/main/java/com/google/errorprone/VisitorState.java

+12-22
Original file line numberDiff line numberDiff line change
@@ -627,28 +627,18 @@ private static void validateTypeStr(String typeStr) {
627627
* the corresponding Type, or null otherwise.
628628
*/
629629
private @Nullable Type getPrimitiveOrVoidType(String typeStr) {
630-
switch (typeStr) {
631-
case "byte":
632-
return getSymtab().byteType;
633-
case "short":
634-
return getSymtab().shortType;
635-
case "int":
636-
return getSymtab().intType;
637-
case "long":
638-
return getSymtab().longType;
639-
case "float":
640-
return getSymtab().floatType;
641-
case "double":
642-
return getSymtab().doubleType;
643-
case "boolean":
644-
return getSymtab().booleanType;
645-
case "char":
646-
return getSymtab().charType;
647-
case "void":
648-
return getSymtab().voidType;
649-
default:
650-
return null;
651-
}
630+
return switch (typeStr) {
631+
case "byte" -> getSymtab().byteType;
632+
case "short" -> getSymtab().shortType;
633+
case "int" -> getSymtab().intType;
634+
case "long" -> getSymtab().longType;
635+
case "float" -> getSymtab().floatType;
636+
case "double" -> getSymtab().doubleType;
637+
case "boolean" -> getSymtab().booleanType;
638+
case "char" -> getSymtab().charType;
639+
case "void" -> getSymtab().voidType;
640+
default -> null;
641+
};
652642
}
653643

654644
/** Returns true if the compilation is targeting Android. */

check_api/src/main/java/com/google/errorprone/apply/SourceFile.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,18 @@ public void replaceChars(int startPosition, int endPosition, String replacement)
168168
void makeReplacements(Replacements changes) {
169169
ImmutableSet<Replacement> replacements = changes.ascending();
170170
switch (replacements.size()) {
171-
case 0:
171+
case 0 -> {
172172
return;
173-
case 1:
174-
{
175-
Replacement onlyReplacement = Iterables.getOnlyElement(replacements);
176-
replaceChars(
177-
onlyReplacement.startPosition(),
178-
onlyReplacement.endPosition(),
179-
onlyReplacement.replaceWith());
180-
return;
181-
}
182-
default:
183-
break;
173+
}
174+
case 1 -> {
175+
Replacement onlyReplacement = Iterables.getOnlyElement(replacements);
176+
replaceChars(
177+
onlyReplacement.startPosition(),
178+
onlyReplacement.endPosition(),
179+
onlyReplacement.replaceWith());
180+
return;
181+
}
182+
default -> {}
184183
}
185184

186185
// Since we have many replacements to make all at once, it's better to start off with a clean

check_api/src/main/java/com/google/errorprone/bugpatterns/BugChecker.java

+19-22
Original file line numberDiff line numberDiff line change
@@ -134,28 +134,25 @@ public BugChecker() {
134134

135135
private static BiPredicate<Set<? extends Name>, VisitorState> suppressionPredicate(
136136
Set<Class<? extends Annotation>> suppressionClasses) {
137-
switch (suppressionClasses.size()) {
138-
case 0:
139-
return (annos, state) -> false;
140-
case 1:
141-
{
142-
Supplier<Name> self =
143-
VisitorState.memoize(
144-
state -> state.getName(Iterables.getOnlyElement(suppressionClasses).getName()));
145-
return (annos, state) -> annos.contains(self.get(state));
146-
}
147-
default:
148-
{
149-
Supplier<Set<? extends Name>> self =
150-
VisitorState.memoize(
151-
state ->
152-
suppressionClasses.stream()
153-
.map(Class::getName)
154-
.map(state::getName)
155-
.collect(toImmutableSet()));
156-
return (annos, state) -> !Collections.disjoint(self.get(state), annos);
157-
}
158-
}
137+
return switch (suppressionClasses.size()) {
138+
case 0 -> (annos, state) -> false;
139+
case 1 -> {
140+
Supplier<Name> self =
141+
VisitorState.memoize(
142+
state -> state.getName(Iterables.getOnlyElement(suppressionClasses).getName()));
143+
yield (annos, state) -> annos.contains(self.get(state));
144+
}
145+
default -> {
146+
Supplier<Set<? extends Name>> self =
147+
VisitorState.memoize(
148+
state ->
149+
suppressionClasses.stream()
150+
.map(Class::getName)
151+
.map(state::getName)
152+
.collect(toImmutableSet()));
153+
yield (annos, state) -> !Collections.disjoint(self.get(state), annos);
154+
}
155+
};
159156
}
160157

161158
/** Helper to create a Description for the common case where there is a fix. */

check_api/src/main/java/com/google/errorprone/dataflow/nullnesspropagation/Nullness.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,12 @@ public Nullness greatestLowerBound(Nullness other) {
104104
* get the set of all values, or {@code NULLABLE}.
105105
*/
106106
public Nullness deducedValueWhenNotEqual() {
107-
switch (this) {
108-
case NULLABLE:
109-
case NONNULL:
110-
return NULLABLE;
111-
case NULL:
112-
return NONNULL;
113-
case BOTTOM:
114-
return BOTTOM;
115-
default:
116-
throw new AssertionError("Inverse of " + this + " not defined");
117-
}
107+
return switch (this) {
108+
case NULLABLE, NONNULL -> NULLABLE;
109+
case NULL -> NONNULL;
110+
case BOTTOM -> BOTTOM;
111+
default -> throw new AssertionError("Inverse of " + this + " not defined");
112+
};
118113
}
119114

120115
@Override

0 commit comments

Comments
 (0)