Styled attributes are obtained and the TypedArray is never recycled

tx0/kotlin:typed-array-not-recycledrecommendedwarningframeworks/androiddepth fastbug, android, resource

What it finds

obtainStyledAttributes hands out a TypedArray from a shared pool. It has to be given back with recycle(), or with use { } on newer platforms; otherwise the pool keeps allocating. Reported when the function that obtains it calls neither.

From the rule's source, typed_array_not_recycled.rs.

Example from the rule's tests
class Badge(context: Context, attrs: AttributeSet) : View(context, attrs) {    init {        val a = context.obtainStyledAttributes(attrs, R.styleable.Badge)tx0 reports: the TypedArray is never recycled        color = a.getColor(R.styleable.Badge_color, 0)    }     fun read(attrs: AttributeSet) {        val a = context.obtainStyledAttributes(attrs, R.styleable.Badge)        color = a.getColor(R.styleable.Badge_color, 0)        a.recycle()    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:typed-array-not-recycled" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:typed-array-not-recycled] reason="..."

tx0 explain tx0/kotlin:typed-array-not-recycled shows the same in the terminal. Found a false positive? Report it.