Skip to content

Commit b9e6a6e

Browse files
author
Vincent Potucek
committed
[prone] Apply UnnecessaryDefaultInEnumSwitch
1 parent f3d5c5d commit b9e6a6e

File tree

19 files changed

+160
-277
lines changed

19 files changed

+160
-277
lines changed

lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/DefaultJavaElementComparator.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -347,28 +347,14 @@ public int compare(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclara
347347
}
348348

349349
private static int sortPreservedCategory(int category) {
350-
switch (category) {
351-
case STATIC_FIELDS_INDEX:
352-
case STATIC_INIT_INDEX:
353-
return STATIC_FIELDS_INDEX;
354-
case FIELDS_INDEX:
355-
case INIT_INDEX:
356-
return FIELDS_INDEX;
357-
default:
358-
return category;
359-
}
350+
return switch (category) {
351+
case STATIC_FIELDS_INDEX, STATIC_INIT_INDEX -> STATIC_FIELDS_INDEX;
352+
case FIELDS_INDEX, INIT_INDEX -> FIELDS_INDEX;
353+
default -> category;
354+
};
360355
}
361356

362-
private boolean isSortPreserved(BodyDeclaration bodyDeclaration) {
363-
switch (bodyDeclaration.getNodeType()) {
364-
case ASTNode.FIELD_DECLARATION:
365-
case ASTNode.ENUM_CONSTANT_DECLARATION:
366-
case ASTNode.INITIALIZER:
367-
return true;
368-
default:
369-
return false;
370-
}
371-
}
357+
private boolean isSortPreserved(BodyDeclaration bodyDeclaration) {return switch(bodyDeclaration.getNodeType()){case ASTNode.FIELD_DECLARATION,ASTNode.ENUM_CONSTANT_DECLARATION,ASTNode.INITIALIZER->true;default->false;};}
372358

373359
private int preserveRelativeOrder(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclaration2) {
374360
int value1 = (Integer) bodyDeclaration1.getProperty(CompilationUnitSorter.RELATIVE_ORDER);

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

Lines changed: 21 additions & 22 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

@@ -300,15 +303,14 @@ public String getEndingFor(File file) {
300303
}
301304

302305
private static String convertEolToLineEnding(String eol, File file) {
303-
switch (eol.toLowerCase(Locale.ROOT)) {
304-
case "lf":
305-
return LineEnding.UNIX.str();
306-
case "crlf":
307-
return LineEnding.WINDOWS.str();
308-
default:
309-
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
310-
return LineEnding.PLATFORM_NATIVE.str();
311-
}
306+
return switch (eol.toLowerCase(Locale.ROOT)) {
307+
case "lf" -> UNIX.str();
308+
case "crlf" -> WINDOWS.str();
309+
default -> {
310+
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
311+
yield PLATFORM_NATIVE.str();
312+
}
313+
};
312314
}
313315

314316
private LineEnding findDefaultLineEnding(Config config) {
@@ -318,12 +320,12 @@ private LineEnding findDefaultLineEnding(Config config) {
318320
// autocrlf=true converts CRLF->LF during commit
319321
// and converts LF->CRLF during checkout
320322
// so CRLF is the default line ending
321-
return LineEnding.WINDOWS;
323+
return WINDOWS;
322324
} else if (autoCRLF == AutoCRLF.INPUT) {
323325
// autocrlf=input converts CRLF->LF during commit
324326
// and does no conversion during checkout
325327
// mostly used on Unix, so LF is the default encoding
326-
return LineEnding.UNIX;
328+
return UNIX;
327329
} else if (autoCRLF == AutoCRLF.FALSE) {
328330
// handle core.eol
329331
EOL eol = config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EOL, EOL.NATIVE);
@@ -335,14 +337,11 @@ private LineEnding findDefaultLineEnding(Config config) {
335337

336338
/** Creates a LineEnding from an EOL. */
337339
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
340+
return switch (eol) {
341+
case CRLF -> WINDOWS;
342+
case LF -> UNIX;
343+
case NATIVE -> PLATFORM_NATIVE;
344+
};
346345
}
347346
}
348347

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/GitPrePushHookInstaller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protected String preHookTemplate(Executor executor, String commandCheck, String
266266
private String executorPath(Executor executor) {
267267
final var wrapper = executorWrapperFile(executor);
268268
if (wrapper.exists()) {
269-
return wrapper.getAbsolutePath().replace("\\", "/");
269+
return "./" + wrapper.getName();
270270
}
271271

272272
logger.info("Local %s wrapper (%s) not found, falling back to global command '%s'",

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,8 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
8383
// @formatter:off
8484
/** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
8585
public Policy createPolicy() {
86-
switch (this) {
87-
case PLATFORM_NATIVE: return _PLATFORM_NATIVE_POLICY;
88-
case WINDOWS: return WINDOWS_POLICY;
89-
case UNIX: return UNIX_POLICY;
90-
case MAC_CLASSIC: return MAC_CLASSIC_POLICY;
91-
case PRESERVE: return PRESERVE_POLICY;
92-
default: throw new UnsupportedOperationException(this + " is a path-specific line ending.");
93-
}
86+
return switch (this) {case PLATFORM_NATIVE->_PLATFORM_NATIVE_POLICY;case WINDOWS->WINDOWS_POLICY;case UNIX->UNIX_POLICY;case MAC_CLASSIC->MAC_CLASSIC_POLICY;case PRESERVE->PRESERVE_POLICY;default->throw new UnsupportedOperationException(this + " is a path-specific line ending.");
87+
};
9488
}
9589

9690
static class ConstantLineEndingPolicy extends NoLambda.EqualityBasedOnSerialization implements Policy {
@@ -169,13 +163,8 @@ public static boolean nativeIsWin() {
169163

170164
/** Returns the standard line ending for this policy. */
171165
public String str() {
172-
switch (this) {
173-
case PLATFORM_NATIVE: return _PLATFORM_NATIVE;
174-
case WINDOWS: return "\r\n";
175-
case UNIX: return "\n";
176-
case MAC_CLASSIC: return "\r";
177-
default: throw new UnsupportedOperationException(this + " is a path-specific line ending.");
178-
}
166+
return switch (this) {case PLATFORM_NATIVE->_PLATFORM_NATIVE;case WINDOWS->"\r\n";case UNIX->"\n";case MAC_CLASSIC->"\r";default->throw new UnsupportedOperationException(this + " is a path-specific line ending.");
167+
};
179168
}
180169
// @formatter:on
181170

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/biome/BiomeStep.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -456,32 +456,22 @@ private String resolveFileName(File file) {
456456
}
457457
var dot = name.lastIndexOf(".");
458458
var ext = dot >= 0 ? name.substring(dot + 1) : name;
459-
switch (language) {
460-
case "js?":
461-
return "jsx".equals(ext) || "js".equals(ext) || "mjs".equals(ext) || "cjs".equals(ext) ? name
462-
: "file.js";
463-
case "ts?":
464-
return "tsx".equals(ext) || "ts".equals(ext) || "mts".equals(ext) || "cts".equals(ext) ? name
465-
: "file.js";
466-
case "js":
467-
return "js".equals(ext) || "mjs".equals(ext) || "cjs".equals(ext) ? name : "file.js";
468-
case "jsx":
469-
return "jsx".equals(ext) ? name : "file.jsx";
470-
case "ts":
471-
return "ts".equals(ext) || "mts".equals(ext) || "cts".equals(ext) ? name : "file.ts";
472-
case "tsx":
473-
return "tsx".equals(ext) ? name : "file.tsx";
474-
case "json":
475-
return "json".equals(ext) ? name : "file.json";
476-
case "jsonc":
477-
return "jsonc".equals(ext) ? name : "file.jsonc";
478-
case "css":
479-
return "css".equals(ext) ? name : "file.css";
480-
// so that we can support new languages such as css or yaml when Biome adds
481-
// support for them without having to change the code
482-
default:
483-
return "file." + language;
484-
}
459+
return switch (language) {
460+
case "js?" -> "jsx".equals(ext) || "js".equals(ext) || "mjs".equals(ext) || "cjs".equals(ext) ? name
461+
: "file.js";
462+
case "ts?" -> "tsx".equals(ext) || "ts".equals(ext) || "mts".equals(ext) || "cts".equals(ext) ? name
463+
: "file.js";
464+
case "js" -> "js".equals(ext) || "mjs".equals(ext) || "cjs".equals(ext) ? name : "file.js";
465+
case "jsx" -> "jsx".equals(ext) ? name : "file.jsx";
466+
case "ts" -> "ts".equals(ext) || "mts".equals(ext) || "cts".equals(ext) ? name : "file.ts";
467+
case "tsx" -> "tsx".equals(ext) ? name : "file.tsx";
468+
case "json" -> "json".equals(ext) ? name : "file.json";
469+
case "jsonc" -> "jsonc".equals(ext) ? name : "file.jsonc";
470+
case "css" -> "css".equals(ext) ? name : "file.css";
471+
// so that we can support new languages such as css or yaml when Biome adds
472+
// support for them without having to change the code
473+
default -> "file." + language;
474+
};
485475
}
486476

487477
/**
@@ -491,8 +481,7 @@ private String resolveFileName(File file) {
491481
* @return A formatter function for formatting code.
492482
*/
493483
private FormatterFunc.Closeable toFunc() {
494-
var runner = new ProcessRunner();
495-
return FormatterFunc.Closeable.of(runner, this::format);
484+
return FormatterFunc.Closeable.of(new ProcessRunner(), this::format);
496485
}
497486
}
498487
}

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

0 commit comments

Comments
 (0)