Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Kotlin warn on assignment of flexible/platform type to non-null type?

When calling a non-nullability-annotated Java function from Kotlin, we get flexible-typed return values, denoted by exclamation marks, e.g. String!.

Kotlin silently allows assigning these flexible values to a normal non-null type, e.g. String, which can cause NullPointerExceptions at runtime.

I would prefer to get compiler warnings or errors for such assignments. Alternatively, treat platform types as equivalent to nullable types (e.g. String?).

As an example, with this Java code:

import android.os.SystemClock; import android.support.annotation.NonNull; import android.support.annotation.Nullable;  public class NullTest {      private String maybe() {         if (SystemClock.elapsedRealtimeNanos() % 2 == 0) {             return null;         }         return "ok";     }      public String annotatedNothing()            { return maybe(); }     @Nullable public String annotatedNullable() { return maybe(); }     @NonNull  public String annotatedNonNull()  { return "ok"; }  } 

...and the following Kotlin code, I'd like to get errors on two new lines (see comments):

fun testnulls() {     val obj = NullTest()      val nullExact: String  = obj.annotatedNullable() // already gives an error     val nullMaybe: String? = obj.annotatedNullable()     val nullInfer          = obj.annotatedNullable()      val okayExact: String  = obj.annotatedNonNull()     val okayMaybe: String? = obj.annotatedNonNull()     val okayInfer          = obj.annotatedNonNull()      val bareExact: String  = obj.annotatedNothing() // I want a compiler error here     val bareMaybe: String? = obj.annotatedNothing()     val bareInfer          = obj.annotatedNothing()      print("length " + nullExact.length)     print("length " + nullMaybe.length) // already gives an error     print("length " + nullInfer.length) // already gives an error      print("length " + okayExact.length)     print("length " + okayMaybe.length) // already gives an error     print("length " + okayInfer.length)      print("length " + bareExact.length)     print("length " + bareMaybe.length) // already gives an error     print("length " + bareInfer.length) // I want a compiler error here } 

The point is that this will force me to add null checks or !!, making sure that I at least have to be explicit about it.

Is this possible?

In the comments of this 2014 JetBrains blog post, when they introduced platform/flexible types, it sounds like they were planning to add an option to warn about exactly these situations, but I haven't been able to find any further information on that.

like image 788
Snild Dolkow Avatar asked Aug 15 '17 10:08

Snild Dolkow


1 Answers

Yes, it is possible to get compiler warnings and/or errors for any assignments from Java methods with a strong assumption that if method doesn't have @NotNull annotation it is @Nullable.

How? :) You will have to write your own Idea Custom Inspection plugin.

Here are some helpful links for anyone experienced enough to build that custom inspection plugin (probably I would be among its grateful users):

  1. Idea plugin development Quick Start Guide
  2. Sources of all existing Idea Kotlin Inspections (might be useful as examples of existing null safety checks)

If you are familiar and experienced Idea plugin developer, then it might not take a lot of time. Otherwise I don't think the result you are going to achieve will really worth the time you will have to spend.

I like your idea, but AFAIK at the early stages of kotlin development there was an attempt to implement as full null safety checks as possible and it turned so, that there were too many potentially unsafe assignments.

p.s. If you will finally build that inspection plugin please let me know. I personally tried to make it, but in my case I will have first to learn more about Idea plugins.

like image 129
Клаус Шварц Avatar answered Sep 23 '22 12:09

Клаус Шварц