Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IntelliJ to warn about unsafe usages of Optional.get()

Some not-so-careful colleagues of mine have been passing around Optional instances and assuming it's safe to call get on them without calling isPresent first.

I know... this shouldn't happen, but the type-system won't stop them!!

So, I wanted IntelliJ to do it. Is it possible to configure IntelliJ so that it will warn (or even throw a compiling error) about calling Optional.get() without first calling Optional.isPresent()?

like image 742
Renato Avatar asked Dec 14 '15 09:12

Renato


1 Answers

IntelliJ 2016.1 supports this out-of-the-box!

By default, calling get() without verifying with isPresent() first will issue a warning, but you can go to the "Inspections" screen and set Optiong.get() without isPresent() check to have severity "Error" if you prefer.

like image 121
Renato Avatar answered Nov 15 '22 01:11

Renato