I’m migrating some pre Java 10 code and I’m wondering if IntelliJ offers a way to automatically refactor the code to replace the variable declarations that uses the actual type with var wherever it’s possible.
The code is full of stuff like:
String status = "empty"; BigDecimal interest = BigDecimal.ZERO; List<Future<Boolean>> results = es.invokeAll(tasks); LocalDate start = LocalDate.of(2020, 1, 1);
And I would prefer:
var status = "empty"; var interest = BigDecimal.ZERO; var results = es.invokeAll(tasks); var start = LocalDate.of(2020, 1, 1);
I already looked in IntelliJ’s settings (Code Style/Inspections) and couldn’t find anything.
Advertisement
Answer
Go to
File | Settings
, there selectEditor | Inspections
, then underJava | Java language level migration aids | Java 10
.Right click on
Local variable type can be omitted
and selectWeak Warning
or similar.Move Your cursor onto any of those warnings in Your code (highlighted grey), open quick fix context help (alt+enter), at
Replace explicit type with 'var'
move to the right and selectFix all 'Local variable type can be omitted' problems in file
Thanks for @olga-klisho for the idea (in comments)