1

I'm developing an android app using Kotlin. I have two different build-variants - VariantA and VariantB - both containing a configuration-File:

// VariantA
object ConfigEnvironment {
    val SOME_PARAMETER: String? = null
}

// VariantB
object ConfigEnvironment {
    val SOME_PARAMETER: String = "This is a config"
}

In my code i'm calling:

ConfigEnvironment.SOME_PARAMETER?.let { Log.d("tag", "$it" }

When building VariantB the Compiler throws a warning:

Safe call on a non-null receiver will have nullable type in future releases

While this is correct for this variant, it's somewhat impractical - since i need the nullability in the other variant.

Can i safely supress this lint? (And how do i do that? My IDE didn't suggest any fixes)

m.reiter
  • 1,796
  • 2
  • 11
  • 31
  • If there are variants of you app, that must support `null`s for this property, wouldn't it be easier to just assume SOME_PARAMETER is nullable, that is, declaring `val SOME_PARAMETER: String? = "This is a config"` in your `VariantB`? – Endzeit Jun 03 '22 at 21:52
  • This would be the fastest solution, however, i don't like to give up some null-safety just because of a lint – m.reiter Jun 07 '22 at 06:28

1 Answers1

3

It should be safe to suppress this warning since you do not call something which expects a non-nullable expression for it inside the let.

You can suppress the warnung like this:

@Suppress("UNNECESSARY_SAFE_CALL")
ConfigEnvironment.SOME_PARAMETER?.let { Log.d("tag", "$it") }

IntelliJ can help you with that. Just move the cursor to the ?. and type your shortcut for Quick Fix (you can look it up in the Keyboard settings): screenshot of IntelliJ

Karsten Gabriel
  • 3,115
  • 6
  • 19
  • Oooooh i hadn't seen the submenu. Thanks! – m.reiter Jun 07 '22 at 06:30
  • You can also suppress a similar warning for fields with explicit types, like `val parameter: String? = BuildConfig.DEBUG_PARAMETER` using this suppression: `@Suppress("RedundantNullableReturnType")` – HelloImKevo Aug 24 '23 at 15:27