Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions gradle/error-prone.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,25 @@ tasks.withType(JavaCompile).configureEach {
'StringJoin',
'StringJoining',
'UnnecessarilyFullyQualified',
'UnnecessaryAssignment',
'UnnecessaryBoxedAssignment',
'UnnecessaryBoxedVariable',
'UnnecessaryBreakInSwitch',
'UnnecessaryCheckNotNull',
'UnnecessaryCopy',
'UnnecessaryDefaultInEnumSwitch',
'UnnecessaryLambda',
'UnnecessaryLongToIntConversion',
'UnnecessaryMethodInvocationMatcher',
'UnnecessaryMethodReference',
'UnnecessaryParentheses',
'UnnecessaryQualifier',
'UnnecessarySetDefault',
'UnnecessaryStaticImport',
'UnnecessaryStringBuilder',
'UnnecessaryTestMethodPrefix',
'UnnecessaryTypeArgument',
'WildcardImport',
)
// bug: this only happens when the file is dirty.
// might be an up2date (caching) issue, as file is currently in corrupt state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package com.diffplug.spotless.extra;

import static com.diffplug.spotless.LineEnding.PLATFORM_NATIVE;
import static com.diffplug.spotless.LineEnding.UNIX;
import static com.diffplug.spotless.LineEnding.WINDOWS;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -91,10 +95,9 @@ public LazyAllTheSame(File projectDir, Supplier<Iterable<File>> toFormat) {
protected String calculateState() throws Exception {
var files = toFormat.get().iterator();
if (files.hasNext()) {
Runtime runtime = new RuntimeInit(projectDir).atRuntime();
return runtime.getEndingFor(files.next());
return new RuntimeInit(projectDir).atRuntime().getEndingFor(files.next());
} else {
return LineEnding.UNIX.str();
return UNIX.str();
}
}

Expand Down Expand Up @@ -302,12 +305,12 @@ public String getEndingFor(File file) {
private static String convertEolToLineEnding(String eol, File file) {
switch (eol.toLowerCase(Locale.ROOT)) {
case "lf":
return LineEnding.UNIX.str();
return UNIX.str();
case "crlf":
return LineEnding.WINDOWS.str();
return WINDOWS.str();
default:
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
return LineEnding.PLATFORM_NATIVE.str();
return PLATFORM_NATIVE.str();
}
}

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

/** Creates a LineEnding from an EOL. */
private static LineEnding fromEol(EOL eol) {
// @formatter:off
switch (eol) {
case CRLF: return LineEnding.WINDOWS;
case LF: return LineEnding.UNIX;
case NATIVE: return LineEnding.PLATFORM_NATIVE;
default: throw new IllegalArgumentException("Unknown eol " + eol);
}
// @formatter:on
return switch (eol) {
case CRLF -> WINDOWS;
case LF -> UNIX;
case NATIVE -> PLATFORM_NATIVE;
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private FormattingOptions createFormattingOptions() throws Exception {
case META -> Formatter.META_FORMAT;
case GOOGLE -> Formatter.GOOGLE_FORMAT;
case KOTLIN_LANG -> Formatter.KOTLINLANG_FORMAT;
default -> throw new IllegalStateException("Unknown formatting option " + style);
};

if (ktfmtFormattingOptions != null) {
Expand Down
27 changes: 11 additions & 16 deletions lib/src/main/java/com/diffplug/spotless/PaddedCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,25 +150,20 @@ public boolean isResolvable() {

/** Returns the "canonical" form for this particular result (only possible if isResolvable). */
public String canonical() {
// @formatter:off
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know it looks intended and i know you mentioned it recently, having done this (IDE) suggedtion already.

Can we evlevate this or do we need this form explizicly, then we could suppress these rare cases. Some of this pattern makes sense on some of the parts done.

switch (type) {
case CONVERGE: return steps.get(steps.size() - 1);
case CYCLE: return Collections.min(steps, Comparator.comparingInt(String::length).thenComparing(Function.identity()));
case DIVERGE: throw new IllegalArgumentException("No canonical form for a diverging result");
default: throw new IllegalArgumentException("Unknown type: " + type);
}
// @formatter:on
return switch (type) {
case CONVERGE -> steps.get(steps.size() - 1);
case CYCLE ->
Collections.min(steps, Comparator.comparingInt(String::length).thenComparing(Function.identity()));
case DIVERGE -> throw new IllegalArgumentException("No canonical form for a diverging result");
};
}

/** Returns a string which describes this result. */
public String userMessage() {
// @formatter:off
switch (type) {
case CONVERGE: return "converges after " + steps.size() + " steps";
case CYCLE: return "cycles between " + steps.size() + " steps";
case DIVERGE: return "diverges after " + steps.size() + " steps";
default: throw new IllegalArgumentException("Unknown type: " + type);
}
// @formatter:on
return switch (type) {
case CONVERGE -> "converges after " + steps.size() + " steps";
case CYCLE -> "cycles between " + steps.size() + " steps";
case DIVERGE -> "diverges after " + steps.size() + " steps";
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,10 @@ private String computeChecksum(Path file, String algorithm) throws IOException {
* @throws IOException When the given OS is not supported by Biome.
*/
private String getArchitectureCodeName(Architecture architecture) throws IOException {
switch (architecture) {
case ARM64:
return "arm64";
case X64:
return "x64";
default:
throw new IOException("Unsupported architecture: " + architecture);
}
return switch (architecture) {
case ARM64 -> "arm64";
case X64 -> "x64";
};
}

/**
Expand Down Expand Up @@ -290,16 +286,10 @@ private String getDownloadUrl(String version, Platform platform) throws IOExcept
* @throws IOException When the given OS is not supported by Biome.
*/
private String getDownloadUrlExtension(OS os) throws IOException {
switch (os) {
case LINUX:
return "";
case MAC_OS:
return "";
case WINDOWS:
return ".exe";
default:
throw new IOException("Unsupported OS: " + os);
}
return switch (os) {
case LINUX, MAC_OS -> "";
case WINDOWS -> ".exe";
};
}

/**
Expand All @@ -326,16 +316,11 @@ private Path getExecutablePath(String version, Platform platform) {
* @throws IOException When the given OS is not supported by Biome.
*/
private String getOsCodeName(OS os) throws IOException {
switch (os) {
case LINUX:
return "linux";
case MAC_OS:
return "darwin";
case WINDOWS:
return "win32";
default:
throw new IOException("Unsupported OS: " + os);
}
return switch (os) {
case LINUX -> "linux";
case MAC_OS -> "darwin";
case WINDOWS -> "win32";
};
}

/**
Expand Down
17 changes: 9 additions & 8 deletions lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.diffplug.spotless.generic;

import static com.diffplug.spotless.generic.FenceStep.Kind.APPLY;
import static com.diffplug.spotless.generic.FenceStep.Kind.PRESERVE;

import java.io.File;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -78,15 +81,15 @@ private void assertRegexSet() {

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

/**
* Returns a step which will apply the given steps only within the blocks selected by the regex / openClose pair.
* Linting within the substeps is not supported.
*/
public FormatterStep applyWithin(List<FormatterStep> steps) {
return createStep(Kind.APPLY, steps);
return createStep(APPLY, steps);
}

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

private enum Kind {
enum Kind {
APPLY, PRESERVE
}

Expand Down Expand Up @@ -210,24 +213,22 @@ public String applyWithFile(String unix, File file) throws Exception {
}
List<String> groups = groupsZeroed();
Matcher matcher = regex.matcher(unix);
switch (kind) {
case APPLY:
if (kind == APPLY) {
while (matcher.find()) {
// apply the formatter to each group
groups.add(formatter.compute(matcher.group(1), file));
}
// and then assemble the result right away
return assembleGroups(unix);
case PRESERVE:
} else if (kind == PRESERVE) {
while (matcher.find()) {
// store whatever is within the open/close tags
groups.add(matcher.group(1));
}
String formatted = formatter.compute(unix, file);
return assembleGroups(formatted);
default:
throw new Error();
}
throw new Error();
}

@Override
Expand Down
20 changes: 7 additions & 13 deletions lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.diffplug.spotless.generic;

import static com.diffplug.spotless.generic.IndentStep.Type.SPACE;
import static com.diffplug.spotless.generic.IndentStep.Type.TAB;

import java.io.Serial;
import java.io.Serializable;

Expand Down Expand Up @@ -102,22 +105,13 @@ String format(String raw) {

// add the leading space in a canonical way
if (numSpaces > 0) {
switch (state.type) {
case SPACE:
for (int i = 0; i < numSpaces; i++) {
builder.append(' ');
}
break;
case TAB:
for (int i = 0; i < numSpaces / state.numSpacesPerTab; i++) {
builder.append('\t');
}
if (state.type == SPACE) {
builder.append(" ".repeat(numSpaces));
} else if (state.type == TAB) {
builder.append("\t".repeat(Math.max(0, numSpaces / state.numSpacesPerTab)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a need for the Math.max(0) part.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes thats off topic, as an ide suggestion:

Can be replaced with 'String.repeat()'

If we can replace a whole loop, with one single line of code its a good win reducing the overhead to a minimum:

image

Can we merge under this ciscumsstances, or should we undo?

if (mightBeMultiLineComment && (numSpaces % state.numSpacesPerTab == 1)) {
builder.append(' ');
}
break;
default:
throw new IllegalArgumentException("Unexpected enum " + state.type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private List<FormatterToken> format(final List<FormatterToken> argList) {
// Concatenate tokens
if (t0.getType() == TokenType.KEYWORD && t1.getType() == TokenType.SPACE && t2.getType() == TokenType.KEYWORD) {
if ((("ORDER".equals(tokenString) || "GROUP".equals(tokenString) || "CONNECT".equals(tokenString)) && "BY".equals(token2String))
|| (("START".equals(tokenString)) && "WITH".equals(token2String))) {
|| ("START".equals(tokenString) && "WITH".equals(token2String))) {
t0.setString(t0.getString() + " " + t2.getString());
argList.remove(index + 1);
argList.remove(index + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@ enum Policy {
INDEPENDENT, ROOT_PROJECT, ROOT_BUILDSCRIPT;

public DedupingProvisioner dedupingProvisioner(Project project) {
switch (this) {
case ROOT_PROJECT:
return new DedupingProvisioner(forProject(project));
case ROOT_BUILDSCRIPT:
return new DedupingProvisioner(forRootProjectBuildscript(project));
case INDEPENDENT:
default:
throw Unhandled.enumException(this);
}
return switch (this) {
case ROOT_PROJECT -> new DedupingProvisioner(forProject(project));
case ROOT_BUILDSCRIPT -> new DedupingProvisioner(forRootProjectBuildscript(project));
case INDEPENDENT -> throw Unhandled.enumException(this);
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public FormatterSettingsAssert containsSpecificValuesOf(File file) {
failWithMessage("Key <%s> not part of formatter settings.", key);
}
String value = settingsProps.getProperty(key);
if ((expectedValue != null) && (!expectedValue.equals(value))) {
if ((expectedValue != null) && !expectedValue.equals(value)) {
failWithMessage("Value of key <%s> is '%s' and not '%s' as expected.", key, value, expectedValue);
}
}
Expand Down
Loading