Skip to content

Use the Java idioms error-prone asks for: instanceof patterns, arrow switches, text blocks - #86

Merged
abashev merged 4 commits into
mainfrom
errorprone-warnings
Sep 27, 2026
Merged

abashev merged 4 commits into
mainfrom
errorprone-warnings

Conversation

@abashev

@abashev abashev commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

Before this PR

A clean compile printed 91 error-prone warnings, and 83 of them came from four checks that ask for Java 16–21 idioms the code did not use yet: PatternMatchingInstanceof (33), StatementSwitchToExpressionSwitch (33), StringConcatToTextBlock (16) and StreamFlatMapOptional (1).

After this PR

==COMMIT_MSG==
The code uses the idioms error-prone asks for, one commit per check:

  • instanceof checks bind a pattern variable instead of casting afterwards (33 places);
  • switch statements use arrow cases, and where every case returns a value the method returns a switch expression (33);
  • concatenated multi-line string literals are text blocks: the inputs and expected outputs in ModuleImportTest and the output.js template in DebugRenderer (16);
  • a plugin test collects Optionals with mapMulti(Optional::ifPresent) instead of flatMap(Optional::stream) (1).
    ==COMMIT_MSG==

The switch, text block and mapMulti changes are error-prone's own fixes (./gradlew compileJava compileTestJava -PerrorProneApply=<check>), formatted with formatDiff, and every hunk was read. A clean compile (--rerun-tasks) now prints none of these four warnings and no new ones; 8 remain (NullAway 5, UnusedMethod, PreferSafeLogger, AnnotateFormatMethod).

Behaviour is unchanged:

  • ./gradlew test passes, and so does :open-java-format:test on JDK 25, which also runs the import module tests that JDK 21 skips;
  • with the instanceof and switch changes, the 15,747 files of the JDK 21 sources format exactly as with main; the later commits touch no formatting code;
  • the text blocks compile to the same string constants as the concatenations they replace (compared in the class files).

Possible downsides?

None for users. For maintainers: the switches and instanceof checks now differ in shape from palantir-java-format and google-java-format, so a fix ported from upstream that touches one of these lines will need its hunk adapted by hand rather than applying cleanly.

error-prone's PatternMatchingInstanceof warned at 33 places in the
formatter and the SPI where an instanceof check is followed by a cast
of the same value. The check now declares the variable itself, and the
cast goes. Names follow error-prone's suggestions, except classDecl,
space and methodInvocation, which read better than jCClassDecl,
nonBreakingSpace and methodInvocationTree.

Nothing else changes: ./gradlew test passes, and so does
:open-java-format:test on JDK 25, which also runs the import module
tests that JDK 21 skips.
error-prone's StatementSwitchToExpressionSwitch warned at 33 switch
statements in the formatter. They now use arrow cases: labels that
shared a body are one case, and the break statements go. Where every
case returned a value, the method returns a switch expression instead:
ImportOrderer.isJava, nextIsModifier, the sealed check in
ModifierOrderer, and TypeNameClassifier's three states, whose switches
cover all four JavaCaseFormat constants and so need no default; the
IllegalStateException after them goes.

This is error-prone's own fix, applied with
-PerrorProneApply=StatementSwitchToExpressionSwitch and formatted with
formatDiff. Nothing else changes: ./gradlew test passes, and so does
:open-java-format:test on JDK 25; the 15,747 files of the JDK 21
sources format exactly as before.
error-prone's StringConcatToTextBlock warned at 16 concatenations of
string literals: the inputs and expected outputs in ModuleImportTest,
and the template DebugRenderer writes to output.js. They are text
blocks now, from error-prone's own fix formatted with formatDiff.

The strings are the same: both classes hold the same string constants
before and after, ./gradlew test passes, and so does
:open-java-format:test on JDK 25, which runs the ModuleImportTest cases
that JDK 21 skips.
error-prone's StreamFlatMapOptional warned about
.flatMap(Optional::stream) in the IntelliJ plugin's settings page test,
where findCheckBox walks the component tree for the first check box.
It now uses the replacement error-prone suggests,
.<JCheckBox>mapMulti(Optional::ifPresent), which builds no stream per
element; findFirst still stops at the first check box.

The three tests that call findCheckBox pass, as do the plugin's other
tests.
@abashev
abashev merged commit e496bdc into main Sep 27, 2026
16 checks passed
@abashev
abashev deleted the errorprone-warnings branch September 27, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant