Skip to content

Commit d6c7cd9

Browse files
committed
Add 'useSwitchForInstanceofPattern' to the cleanup settings.
Signed-off-by: Roland Grunberg <[email protected]>
1 parent 907578a commit d6c7cd9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

document/_java.learnMoreAboutCleanUps.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,44 @@ switch (r) {
432432
case R _ -> {}
433433
}
434434
```
435+
436+
### `useSwitchForInstanceofPattern`
437+
438+
Convert if/else chains to pattern matching switch statements.
439+
440+
For example:
441+
442+
```java
443+
int i, j;
444+
double d;
445+
boolean b;
446+
if (x instanceof Integer xint) {
447+
i = xint.intValue();
448+
} else if (x instanceof Double xdouble) {
449+
d = xdouble.doubleValue();
450+
} else if (x instanceof Boolean xboolean) {
451+
b = xboolean.booleanValue();
452+
} else {
453+
i = 0;
454+
d = 0.0D;
455+
b = false;
456+
}
457+
```
458+
459+
becomes:
460+
461+
```java
462+
int i, j;
463+
double d;
464+
boolean b;
465+
switch (x) {
466+
case Integer xint -> i = xint.intValue();
467+
case Double xdouble -> d = xdouble.doubleValue();
468+
case Boolean xboolean -> b = xboolean.booleanValue();
469+
case null, default -> {
470+
i = 0;
471+
d = 0.0D;
472+
b = false;
473+
}
474+
}
475+
```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,8 @@
13141314
"tryWithResource",
13151315
"renameFileToType",
13161316
"organizeImports",
1317-
"renameUnusedLocalVariables"
1317+
"renameUnusedLocalVariables",
1318+
"useSwitchForInstanceofPattern"
13181319
]
13191320
},
13201321
"default": [

0 commit comments

Comments
 (0)