Improved Data Flow Analysis in IntelliJ IDEA 2018.2
IntelliJ IDEA gives warnings in the editor if it detects suspicious code based on an analysis of data flow. In IntelliJ IDEA 2018.2 we can now see information known about a particular symbol. For example, if a number of
instanceof checks have been performed against a parameter, pressing Ctrl+Shift+P twice shows us which types this value may be:
If we do this with an Optional value, we can see the value is known to be present if, for example, an
ifPresent() check has already been done:
This analysis can show really useful information about known values, as you can see from this example with an
Data flow analysis is smarter with array values. IntelliJ IDEA can now show the possible values from an array if the array was statically initialized. For example, if we have an array containing the number of days in each of the months of the year, and some logic to validate whether a given date value is valid, we can see which values are possible at each stage of the validation:
IntelliJ IDEA 2018.2 also knows if a collection was initialized as an empty collection, and can highlight errors caused by accessing elements that don’t exist:
Data flow analysis also supports more math operations than before, like division and right-shift.
IntelliJ IDEA 2018.2 can work out if a method returns an immutable value, or an immutable view of a value. So if, for example, a method returns a list wrapped in
Collections.unmodifiableList(), the gutter annotation icon will show that this method has an inferred annotation of
unmodifiableList returns a view onto a list that cannot be altered.
This means that if a modifying method is called on the result of this method, IntelliJ IDEA can warn us that this is probably incorrect:
Also new is the ability to look inside streams created using
Stream.of() to work out if downstream operations are logical. If, for example, we create a Stream of Optional values where all the values are known to be present, IntelliJ IDEA will point out that a call to check to see if
isPresent is true on all the Optional values will always be true, and can therefore be simplified.
Because of this increased intelligence, there will also be no warning that the call to
get needs a preceding call to
isPresent. Note that this feature only works for up to three values inside the
Smarter warnings are also available for Hamcrest matchers, so when using Hamcrest inside tests we’ll see fewer instances of warnings, for example when calling
get on Optional. Similarly we’ll also see fewer incorrect “may produce NullPointerException” warnings on Nullable values.
Data flow analysis now supports checks using
isInstance, so we see fewer false positives and more useful warnings:
Finally on this topic, IntelliJ IDEA 2018.2 has increased support for constant evaluation of simple methods, meaning that if certain Math or String methods are called with constant values, IntelliJ IDEA can warn us that certain conditions will always or never be true:
The methods that are supported on String are: contains, indexOf, startsWith, endsWith, lastIndexOf, length, trim, substring, equals, equalsIgnoreCase, charAt, codePointAt, compareTo, replace, valueOf. On Math: abs, sqrt, min, max
In summary, IntelliJ IDEA 2018.2 is even smarter about showing us where our code can be simplified or where possible bugs may be lurking, making it easier to write correct and readable code.
Subscribe to Blog updates
Thanks, we've got you!
The What, Why, and How of JPA Buddy in IntelliJ IDEA
Master JPA entity management with JPA Buddy in IntelliJ IDEA! Our latest article shows you how to simplify your workflow and reduce boilerplate code.
The Best of IntelliJ IDEA’s HTTP Client
In today's fast-paced tech world, easy and efficient management of web services and APIs is crucial. That's where IntelliJ IDEA's HTTP Client steps in, turning complex HTTP requests into a walk in the park. The HTTP Client is a feature-rich, text-based tool in IntelliJ IDEA Ultimate. It seamlessl…
Import Postman Collections to the HTTP Client
We know that many of you use Postman collections and want to stay in the flow while working with them. This means staying inside IntelliJ IDEA and not switching to a different app. Great news! You can now effortlessly transform Postman collections into .http and use them in the HTTP Client …
What’s New in IntelliJ IDEA 2023.1 for Spring Developers
The IntelliJ IDEA 2023.1 release came packed with updates for Spring. This blog post will highlight the most noticeable changes, including full support for Lombok, a reworked Spring tool window, navigation for Spring Security rules, Spring Security 6 support, and more. Remember that these featur…