Warnings about the unused results of free side effects in IntelliJ Idea

When I do not assign the result of the BigDecimal.divide() method to a variable, I get a good warning from IntelliJ Idea:

The result of BigDecimal.divide () is ignored.

Can I somehow get the same warning for my own (free from side effects) functions? Something like assigning Java annotations to my function.

+9
source share
1 answer

This is the check "The result of ignoring the method is ignored." By default, it reports only a few special methods, including all java.lang.BigDecimal methods. In the validation configuration, you can add other classes and methods that should be reported this way.

enter image description here

The "Report all ignored non-library calls" checkbox selects all classes in your project.

If you want to use annotations, you can annotate individual methods or entire classes using the JSR 305 annotation

 javax.annotation.CheckReturnValue 

With IDEA 2016.3 you can even use error annotation

 com.google.errorprone.annotations.CanIgnoreReturnValue 

exclude individual methods from checking return value. Using both annotations, you can write a class as follows:

 import javax.annotation.CheckReturnValue; import com.google.errorprone.annotations.CanIgnoreReturnValue; @CheckReturnValue class A { String a() { return "a"; } @CanIgnoreReturnValue String b() { return "b"; } void run() { a(); // Warning b(); // No warning } } 
+9
source

Source: https://habr.com/ru/post/1261176/


All Articles