Skip to content

Commit 5d67390

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

File tree

18 files changed

+271
-342
lines changed

18 files changed

+271
-342
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/LineEnding.java

Lines changed: 112 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,53 @@
3030
* Represents the line endings which should be written by the tool.
3131
*/
3232
public enum LineEnding {
33-
// @formatter:off
34-
/** Uses the same line endings as Git, using {@code .gitattributes} and the {@code core.eol} property. */
33+
/**
34+
* Uses the same line endings as Git, using {@code .gitattributes} and the {@code core.eol} property.
35+
*/
3536
GIT_ATTRIBUTES {
3637
/** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
37-
@Override @Deprecated
38+
@Override
39+
@Deprecated
3840
public Policy createPolicy() {
3941
return super.createPolicy();
4042
}
4143
},
42-
/** Uses the same line endings as Git, and assumes that every single file being formatted will have the same line ending. */
44+
/**
45+
* Uses the same line endings as Git, and assumes that every single file being formatted will have the same line
46+
* ending.
47+
*/
4348
GIT_ATTRIBUTES_FAST_ALLSAME {
4449
/** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
45-
@Override @Deprecated
50+
@Override
51+
@Deprecated
4652
public Policy createPolicy() {
4753
return super.createPolicy();
4854
}
4955
},
50-
/** {@code \n} on unix systems, {@code \r\n} on windows systems. */
56+
/**
57+
* {@code \n} on unix systems, {@code \r\n} on windows systems.
58+
*/
5159
PLATFORM_NATIVE,
52-
/** {@code \r\n} */
60+
/**
61+
* {@code \r\n}
62+
*/
5363
WINDOWS,
54-
/** {@code \n} */
55-
UNIX,
56-
/** {@code \r} */
57-
MAC_CLASSIC,
58-
/** preserve the line ending of the first line (no matter which format) */
59-
PRESERVE;
60-
// @formatter:on
61-
62-
/** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
64+
/**
65+
* {@code \n}
66+
*/
67+
UNIX,
68+
/**
69+
* {@code \r}
70+
*/
71+
MAC_CLASSIC,
72+
/**
73+
* preserve the line ending of the first line (no matter which format)
74+
*/
75+
PRESERVE;
76+
77+
/**
78+
* Returns a {@link Policy} appropriate for files which are contained within the given rootFolder.
79+
*/
6380
public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
6481
Objects.requireNonNull(projectDir, "projectDir");
6582
Objects.requireNonNull(toFormat, "toFormat");
@@ -76,25 +93,29 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
7693
Method method = clazz.getMethod(gitAttributesMethod, File.class, Supplier.class);
7794
return ThrowingEx.get(() -> (Policy) method.invoke(null, projectDir, toFormat));
7895
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) {
79-
throw new IllegalStateException("LineEnding.GIT_ATTRIBUTES requires the spotless-lib-extra library, but it is not on the classpath", e);
96+
throw new IllegalStateException("LineEnding.GIT_ATTRIBUTES requires the spotless-lib-extra library, but it" +
97+
" is not on the classpath", e);
8098
}
8199
}
82100

83-
// @formatter:off
84-
/** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
101+
/**
102+
* Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent
103+
* LineEnding policy.
104+
*/
85105
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-
}
106+
return switch (this) {
107+
case PLATFORM_NATIVE -> _PLATFORM_NATIVE_POLICY;
108+
case WINDOWS -> WINDOWS_POLICY;
109+
case UNIX -> UNIX_POLICY;
110+
case MAC_CLASSIC -> MAC_CLASSIC_POLICY;
111+
case PRESERVE -> PRESERVE_POLICY;
112+
default -> throw new UnsupportedOperationException(this + " is a path-specific line ending.");
113+
};
94114
}
95115

96116
static class ConstantLineEndingPolicy extends NoLambda.EqualityBasedOnSerialization implements Policy {
97-
@Serial private static final long serialVersionUID = 1L;
117+
@Serial
118+
private static final long serialVersionUID = 1L;
98119

99120
final String lineEnding;
100121

@@ -109,90 +130,99 @@ public String getEndingFor(File file) {
109130
}
110131

111132
static class PreserveLineEndingPolicy extends NoLambda.EqualityBasedOnSerialization implements Policy {
112-
@Serial private static final long serialVersionUID = 2L;
113-
114-
@Override
115-
public String getEndingFor(File file) {
116-
// assume US-ASCII encoding (only line ending characters need to be decoded anyways)
117-
try (Reader reader = new FileReader(file, StandardCharsets.US_ASCII)) {
118-
return getEndingFor(reader);
119-
} catch (IOException e) {
120-
throw new IllegalArgumentException("Could not determine line ending of file: " + file, e);
121-
}
122-
}
123-
124-
static String getEndingFor(Reader reader) throws IOException {
125-
char previousCharacter = 0;
126-
char currentCharacter = 0;
127-
int readResult;
128-
while ((readResult = reader.read()) != -1) {
129-
currentCharacter = (char) readResult;
130-
if (currentCharacter == '\n') {
131-
if (previousCharacter == '\r') {
132-
return WINDOWS.str();
133-
} else {
134-
return UNIX.str();
135-
}
136-
} else {
137-
if (previousCharacter == '\r') {
138-
return MAC_CLASSIC.str();
139-
}
140-
}
141-
previousCharacter = currentCharacter;
142-
}
143-
if (previousCharacter == '\r') {
144-
return MAC_CLASSIC.str();
145-
}
146-
// assume UNIX line endings if no line ending was found
147-
return UNIX.str();
148-
}
133+
@Serial
134+
private static final long serialVersionUID = 2L;
135+
136+
@Override
137+
public String getEndingFor(File file) {
138+
// assume US-ASCII encoding (only line ending characters need to be decoded anyways)
139+
try (Reader reader = new FileReader(file, StandardCharsets.US_ASCII)) {
140+
return getEndingFor(reader);
141+
} catch (IOException e) {
142+
throw new IllegalArgumentException("Could not determine line ending of file: " + file, e);
143+
}
144+
}
145+
146+
static String getEndingFor(Reader reader) throws IOException {
147+
char previousCharacter = 0;
148+
char currentCharacter;
149+
int readResult;
150+
while ((readResult = reader.read()) != -1) {
151+
currentCharacter = (char) readResult;
152+
if (currentCharacter == '\n') {
153+
if (previousCharacter == '\r') {
154+
return WINDOWS.str();
155+
} else {
156+
return UNIX.str();
157+
}
158+
} else {
159+
if (previousCharacter == '\r') {
160+
return MAC_CLASSIC.str();
161+
}
162+
}
163+
previousCharacter = currentCharacter;
164+
}
165+
if (previousCharacter == '\r') {
166+
return MAC_CLASSIC.str();
167+
}
168+
// assume UNIX line endings if no line ending was found
169+
return UNIX.str();
170+
}
149171
}
150172

151173
private static final Policy WINDOWS_POLICY = new ConstantLineEndingPolicy(WINDOWS.str());
152174
private static final Policy UNIX_POLICY = new ConstantLineEndingPolicy(UNIX.str());
153-
private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy(MAC_CLASSIC.str());
154-
private static final Policy PRESERVE_POLICY = new PreserveLineEndingPolicy();
175+
private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy(MAC_CLASSIC.str());
176+
private static final Policy PRESERVE_POLICY = new PreserveLineEndingPolicy();
155177
private static final String _PLATFORM_NATIVE = System.getProperty("line.separator");
156178
private static final Policy _PLATFORM_NATIVE_POLICY = new ConstantLineEndingPolicy(_PLATFORM_NATIVE);
157179
private static final boolean NATIVE_IS_WIN = _PLATFORM_NATIVE.equals(WINDOWS.str());
158180

159181
/**
182+
* @see FileSignature#machineIsWin()
160183
* @deprecated Using the system-native line endings to detect the windows operating system has turned out
161184
* to be unreliable. Use {@link FileSignature#machineIsWin()} instead.
162-
*
163-
* @see FileSignature#machineIsWin()
164185
*/
165186
@Deprecated
166187
public static boolean nativeIsWin() {
167188
return NATIVE_IS_WIN;
168189
}
169190

170-
/** Returns the standard line ending for this policy. */
191+
/**
192+
* Returns the standard line ending for this policy.
193+
*/
171194
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-
}
195+
return switch (this) {
196+
case PLATFORM_NATIVE -> _PLATFORM_NATIVE;
197+
case WINDOWS -> "\r\n";
198+
case UNIX -> "\n";
199+
case MAC_CLASSIC -> "\r";
200+
default -> throw new UnsupportedOperationException(this + " is a path-specific line ending.");
201+
};
179202
}
180-
// @formatter:on
181203

182-
/** A policy for line endings which can vary based on the specific file being requested. */
204+
/**
205+
* A policy for line endings which can vary based on the specific file being requested.
206+
*/
183207
public interface Policy extends Serializable, NoLambda {
184-
/** Returns the line ending appropriate for the given file. */
208+
/**
209+
* Returns the line ending appropriate for the given file.
210+
*/
185211
String getEndingFor(File file);
186212

187-
/** Returns true iff this file has unix line endings. */
213+
/**
214+
* Returns true iff this file has unix line endings.
215+
*/
188216
public default boolean isUnix(File file) {
189217
Objects.requireNonNull(file);
190218
String ending = getEndingFor(file);
191219
return ending.equals(UNIX.str());
192220
}
193221
}
194222

195-
/** Returns a string with exclusively unix line endings. */
223+
/**
224+
* Returns a string with exclusively unix line endings.
225+
*/
196226
public static String toUnix(String input) {
197227
int lastCarriageReturn = input.lastIndexOf('\r');
198228
if (lastCarriageReturn == -1) {

0 commit comments

Comments
 (0)