kotlinintellij-idea

How to disable or ignore a wrong IntelliJ inspection error


In a Kotlin file, I get the following IDE error:

Declaration annotated with '@OptionalExpectation' can only be used in familiar module sources.

The class definition is annotated as follows:

@JvmInline
value class PhoneNumber private constructor(val value: String) {
    init {
        require(value.matches(Regex("""^\+[1-9]\d{1,14}$"""))) {
            "Phone number must be in E.164 format with a leading '+'"
        }
    }
    ...

The project compiles and runs perfectly, as it is only compatible with Java 21.

sample of the inspection error I got

I've also tried to fully disable any inspection tool by tweaking the project preferences, but I was unsuccessful.

I've noticed several open bugs on youtrack, but I'm not sure which one fully describes my own one, if any.


Solution

  • This is a bug in the IntelliJ Kotlin Plugin, reported as KTIJ-22326 False positive Declaration annotated with '@OptionalExpectation' can only be used in common module sources in Android only module.

    For now, you can suppress this with the option

    -Xsuppress-warning=OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE
    

    According to some comments, this no longer works in newer compiler versions. But annotating the file with @file.Suppress("...") still works.