kotlinspotbugs

Is spotbugs compatible with Kotlin?


When running spotbugs on a Kolin project I get errors such as:

[ERROR] Private method com.example.CSVRecord.component1() is never called [com.example.CSVRecord] In CSVRecord.kt UPM_UNCALLED_PRIVATE_METHOD

on classes such as:

data class CSVRecord(private val columns: SortedSet<CSVColumn>) : Iterable<String> {

    override fun iterator(): Iterator<String> {
        return columns.map { it.value }.iterator()
    }
}

I'm not really clear where component1 came from!


Solution

  • According to the Data Classes documentation:

    The compiler automatically derives the following members from all properties declared in the primary constructor:

    • equals()/hashCode() pair;
    • toString() of the form "User(name=John, age=42)";
    • componentN() functions corresponding to the properties in their order of declaration;
    • copy() function (see below).

    This is one the features of data classes. The auto-generated componentN functions allow you to use Destructuring Declarations on this type of classes:

    data class Result(val result: Int, val status: Status)
    fun function(...): Result {
        // computations
    
        return Result(result, status)
    }
    
    // Now, to use this function:
    val (result, status) = function(...)