Skip to content

Commit 0914367

Browse files
author
Vincent Potucek
committed
[prone] Apply UnnecessaryDefaultInEnumSwitch
1 parent a0ac600 commit 0914367

File tree

10 files changed

+81
-93
lines changed

10 files changed

+81
-93
lines changed

gradle/error-prone.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,25 @@ tasks.withType(JavaCompile).configureEach {
3737
'StringJoin',
3838
'StringJoining',
3939
'UnnecessarilyFullyQualified',
40+
'UnnecessaryAssignment',
41+
'UnnecessaryBoxedAssignment',
42+
'UnnecessaryBoxedVariable',
43+
'UnnecessaryBreakInSwitch',
44+
'UnnecessaryCheckNotNull',
45+
'UnnecessaryCopy',
46+
'UnnecessaryDefaultInEnumSwitch',
4047
'UnnecessaryLambda',
48+
'UnnecessaryLongToIntConversion',
49+
'UnnecessaryMethodInvocationMatcher',
50+
'UnnecessaryMethodReference',
51+
'UnnecessaryParentheses',
52+
'UnnecessaryQualifier',
53+
'UnnecessarySetDefault',
54+
'UnnecessaryStaticImport',
55+
'UnnecessaryStringBuilder',
56+
'UnnecessaryTestMethodPrefix',
57+
'UnnecessaryTypeArgument',
58+
'WildcardImport',
4159
)
4260
// bug: this only happens when the file is dirty.
4361
// might be an up2date (caching) issue, as file is currently in corrupt state.

lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package com.diffplug.spotless.extra;
1717

18+
import static com.diffplug.spotless.LineEnding.PLATFORM_NATIVE;
19+
import static com.diffplug.spotless.LineEnding.UNIX;
20+
import static com.diffplug.spotless.LineEnding.WINDOWS;
21+
1822
import java.io.File;
1923
import java.io.FileInputStream;
2024
import java.io.IOException;
@@ -91,10 +95,9 @@ public LazyAllTheSame(File projectDir, Supplier<Iterable<File>> toFormat) {
9195
protected String calculateState() throws Exception {
9296
var files = toFormat.get().iterator();
9397
if (files.hasNext()) {
94-
Runtime runtime = new RuntimeInit(projectDir).atRuntime();
95-
return runtime.getEndingFor(files.next());
98+
return new RuntimeInit(projectDir).atRuntime().getEndingFor(files.next());
9699
} else {
97-
return LineEnding.UNIX.str();
100+
return UNIX.str();
98101
}
99102
}
100103

@@ -302,12 +305,12 @@ public String getEndingFor(File file) {
302305
private static String convertEolToLineEnding(String eol, File file) {
303306
switch (eol.toLowerCase(Locale.ROOT)) {
304307
case "lf":
305-
return LineEnding.UNIX.str();
308+
return UNIX.str();
306309
case "crlf":
307-
return LineEnding.WINDOWS.str();
310+
return WINDOWS.str();
308311
default:
309312
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
310-
return LineEnding.PLATFORM_NATIVE.str();
313+
return PLATFORM_NATIVE.str();
311314
}
312315
}
313316

@@ -318,12 +321,12 @@ private LineEnding findDefaultLineEnding(Config config) {
318321
// autocrlf=true converts CRLF->LF during commit
319322
// and converts LF->CRLF during checkout
320323
// so CRLF is the default line ending
321-
return LineEnding.WINDOWS;
324+
return WINDOWS;
322325
} else if (autoCRLF == AutoCRLF.INPUT) {
323326
// autocrlf=input converts CRLF->LF during commit
324327
// and does no conversion during checkout
325328
// mostly used on Unix, so LF is the default encoding
326-
return LineEnding.UNIX;
329+
return UNIX;
327330
} else if (autoCRLF == AutoCRLF.FALSE) {
328331
// handle core.eol
329332
EOL eol = config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EOL, EOL.NATIVE);
@@ -335,14 +338,11 @@ private LineEnding findDefaultLineEnding(Config config) {
335338

336339
/** Creates a LineEnding from an EOL. */
337340
private static LineEnding fromEol(EOL eol) {
338-
// @formatter:off
339-
switch (eol) {
340-
case CRLF: return LineEnding.WINDOWS;
341-
case LF: return LineEnding.UNIX;
342-
case NATIVE: return LineEnding.PLATFORM_NATIVE;
343-
default: throw new IllegalArgumentException("Unknown eol " + eol);
344-
}
345-
// @formatter:on
341+
return switch (eol) {
342+
case CRLF -> WINDOWS;
343+
case LF -> UNIX;
344+
case NATIVE -> PLATFORM_NATIVE;
345+
};
346346
}
347347
}
348348

lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ private FormattingOptions createFormattingOptions() throws Exception {
5858
case META -> Formatter.META_FORMAT;
5959
case GOOGLE -> Formatter.GOOGLE_FORMAT;
6060
case KOTLIN_LANG -> Formatter.KOTLINLANG_FORMAT;
61-
default -> throw new IllegalStateException("Unknown formatting option " + style);
6261
};
6362

6463
if (ktfmtFormattingOptions != null) {

lib/src/main/java/com/diffplug/spotless/PaddedCell.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,20 @@ public boolean isResolvable() {
150150

151151
/** Returns the "canonical" form for this particular result (only possible if isResolvable). */
152152
public String canonical() {
153-
// @formatter:off
154-
switch (type) {
155-
case CONVERGE: return steps.get(steps.size() - 1);
156-
case CYCLE: return Collections.min(steps, Comparator.comparingInt(String::length).thenComparing(Function.identity()));
157-
case DIVERGE: throw new IllegalArgumentException("No canonical form for a diverging result");
158-
default: throw new IllegalArgumentException("Unknown type: " + type);
159-
}
160-
// @formatter:on
153+
return switch (type) {
154+
case CONVERGE -> steps.get(steps.size() - 1);
155+
case CYCLE ->
156+
Collections.min(steps, Comparator.comparingInt(String::length).thenComparing(Function.identity()));
157+
case DIVERGE -> throw new IllegalArgumentException("No canonical form for a diverging result");
158+
};
161159
}
162160

163161
/** Returns a string which describes this result. */
164162
public String userMessage() {
165-
// @formatter:off
166-
switch (type) {
167-
case CONVERGE: return "converges after " + steps.size() + " steps";
168-
case CYCLE: return "cycles between " + steps.size() + " steps";
169-
case DIVERGE: return "diverges after " + steps.size() + " steps";
170-
default: throw new IllegalArgumentException("Unknown type: " + type);
171-
}
172-
// @formatter:on
163+
return switch (type) {
164+
case CONVERGE -> "converges after " + steps.size() + " steps";
165+
case CYCLE -> "cycles between " + steps.size() + " steps";
166+
case DIVERGE -> "diverges after " + steps.size() + " steps";
167+
};
173168
}
174169
}

lib/src/main/java/com/diffplug/spotless/biome/BiomeExecutableDownloader.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,10 @@ private String computeChecksum(Path file, String algorithm) throws IOException {
241241
* @throws IOException When the given OS is not supported by Biome.
242242
*/
243243
private String getArchitectureCodeName(Architecture architecture) throws IOException {
244-
switch (architecture) {
245-
case ARM64:
246-
return "arm64";
247-
case X64:
248-
return "x64";
249-
default:
250-
throw new IOException("Unsupported architecture: " + architecture);
251-
}
244+
return switch (architecture) {
245+
case ARM64 -> "arm64";
246+
case X64 -> "x64";
247+
};
252248
}
253249

254250
/**
@@ -290,16 +286,10 @@ private String getDownloadUrl(String version, Platform platform) throws IOExcept
290286
* @throws IOException When the given OS is not supported by Biome.
291287
*/
292288
private String getDownloadUrlExtension(OS os) throws IOException {
293-
switch (os) {
294-
case LINUX:
295-
return "";
296-
case MAC_OS:
297-
return "";
298-
case WINDOWS:
299-
return ".exe";
300-
default:
301-
throw new IOException("Unsupported OS: " + os);
302-
}
289+
return switch (os) {
290+
case LINUX, MAC_OS -> "";
291+
case WINDOWS -> ".exe";
292+
};
303293
}
304294

305295
/**
@@ -326,16 +316,11 @@ private Path getExecutablePath(String version, Platform platform) {
326316
* @throws IOException When the given OS is not supported by Biome.
327317
*/
328318
private String getOsCodeName(OS os) throws IOException {
329-
switch (os) {
330-
case LINUX:
331-
return "linux";
332-
case MAC_OS:
333-
return "darwin";
334-
case WINDOWS:
335-
return "win32";
336-
default:
337-
throw new IOException("Unsupported OS: " + os);
338-
}
319+
return switch (os) {
320+
case LINUX -> "linux";
321+
case MAC_OS -> "darwin";
322+
case WINDOWS -> "win32";
323+
};
339324
}
340325

341326
/**

lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.diffplug.spotless.generic;
1717

18+
import static com.diffplug.spotless.generic.FenceStep.Kind.APPLY;
19+
import static com.diffplug.spotless.generic.FenceStep.Kind.PRESERVE;
20+
1821
import java.io.File;
1922
import java.io.Serializable;
2023
import java.nio.charset.StandardCharsets;
@@ -78,15 +81,15 @@ private void assertRegexSet() {
7881

7982
/** Returns a step which will apply the given steps but preserve the content selected by the regex / openClose pair. */
8083
public FormatterStep preserveWithin(List<FormatterStep> steps) {
81-
return createStep(Kind.PRESERVE, steps);
84+
return createStep(PRESERVE, steps);
8285
}
8386

8487
/**
8588
* Returns a step which will apply the given steps only within the blocks selected by the regex / openClose pair.
8689
* Linting within the substeps is not supported.
8790
*/
8891
public FormatterStep applyWithin(List<FormatterStep> steps) {
89-
return createStep(Kind.APPLY, steps);
92+
return createStep(APPLY, steps);
9093
}
9194

9295
private FormatterStep createStep(Kind kind, List<FormatterStep> steps) {
@@ -96,7 +99,7 @@ private FormatterStep createStep(Kind kind, List<FormatterStep> steps) {
9699
RoundtripAndEqualityState::toFormatterFunc);
97100
}
98101

99-
private enum Kind {
102+
enum Kind {
100103
APPLY, PRESERVE
101104
}
102105

@@ -210,24 +213,22 @@ public String applyWithFile(String unix, File file) throws Exception {
210213
}
211214
List<String> groups = groupsZeroed();
212215
Matcher matcher = regex.matcher(unix);
213-
switch (kind) {
214-
case APPLY:
216+
if (kind == APPLY) {
215217
while (matcher.find()) {
216218
// apply the formatter to each group
217219
groups.add(formatter.compute(matcher.group(1), file));
218220
}
219221
// and then assemble the result right away
220222
return assembleGroups(unix);
221-
case PRESERVE:
223+
} else if (kind == PRESERVE) {
222224
while (matcher.find()) {
223225
// store whatever is within the open/close tags
224226
groups.add(matcher.group(1));
225227
}
226228
String formatted = formatter.compute(unix, file);
227229
return assembleGroups(formatted);
228-
default:
229-
throw new Error();
230230
}
231+
throw new Error();
231232
}
232233

233234
@Override

lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.diffplug.spotless.generic;
1717

18+
import static com.diffplug.spotless.generic.IndentStep.Type.SPACE;
19+
import static com.diffplug.spotless.generic.IndentStep.Type.TAB;
20+
1821
import java.io.Serial;
1922
import java.io.Serializable;
2023

@@ -102,22 +105,13 @@ String format(String raw) {
102105

103106
// add the leading space in a canonical way
104107
if (numSpaces > 0) {
105-
switch (state.type) {
106-
case SPACE:
107-
for (int i = 0; i < numSpaces; i++) {
108-
builder.append(' ');
109-
}
110-
break;
111-
case TAB:
112-
for (int i = 0; i < numSpaces / state.numSpacesPerTab; i++) {
113-
builder.append('\t');
114-
}
108+
if (state.type == SPACE) {
109+
builder.append(" ".repeat(numSpaces));
110+
} else if (state.type == TAB) {
111+
builder.append("\t".repeat(Math.max(0, numSpaces / state.numSpacesPerTab)));
115112
if (mightBeMultiLineComment && (numSpaces % state.numSpacesPerTab == 1)) {
116113
builder.append(' ');
117114
}
118-
break;
119-
default:
120-
throw new IllegalArgumentException("Unexpected enum " + state.type);
121115
}
122116
}
123117

lib/src/main/java/com/diffplug/spotless/sql/dbeaver/SQLTokenizedFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private List<FormatterToken> format(final List<FormatterToken> argList) {
125125
// Concatenate tokens
126126
if (t0.getType() == TokenType.KEYWORD && t1.getType() == TokenType.SPACE && t2.getType() == TokenType.KEYWORD) {
127127
if ((("ORDER".equals(tokenString) || "GROUP".equals(tokenString) || "CONNECT".equals(tokenString)) && "BY".equals(token2String))
128-
|| (("START".equals(tokenString)) && "WITH".equals(token2String))) {
128+
|| ("START".equals(tokenString) && "WITH".equals(token2String))) {
129129
t0.setString(t0.getString() + " " + t2.getString());
130130
argList.remove(index + 1);
131131
argList.remove(index + 1);

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleProvisioner.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,11 @@ enum Policy {
4545
INDEPENDENT, ROOT_PROJECT, ROOT_BUILDSCRIPT;
4646

4747
public DedupingProvisioner dedupingProvisioner(Project project) {
48-
switch (this) {
49-
case ROOT_PROJECT:
50-
return new DedupingProvisioner(forProject(project));
51-
case ROOT_BUILDSCRIPT:
52-
return new DedupingProvisioner(forRootProjectBuildscript(project));
53-
case INDEPENDENT:
54-
default:
55-
throw Unhandled.enumException(this);
56-
}
48+
return switch (this) {
49+
case ROOT_PROJECT -> new DedupingProvisioner(forProject(project));
50+
case ROOT_BUILDSCRIPT -> new DedupingProvisioner(forRootProjectBuildscript(project));
51+
case INDEPENDENT -> throw Unhandled.enumException(this);
52+
};
5753
}
5854
}
5955

testlib/src/test/java/com/diffplug/spotless/FormatterPropertiesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public FormatterSettingsAssert containsSpecificValuesOf(File file) {
214214
failWithMessage("Key <%s> not part of formatter settings.", key);
215215
}
216216
String value = settingsProps.getProperty(key);
217-
if ((expectedValue != null) && (!expectedValue.equals(value))) {
217+
if ((expectedValue != null) && !expectedValue.equals(value)) {
218218
failWithMessage("Value of key <%s> is '%s' and not '%s' as expected.", key, value, expectedValue);
219219
}
220220
}

0 commit comments

Comments
 (0)