Preference changes are saved with commit() whose result is ignored, which writes to disk on the calling thread

tx0/kotlin:preferences-commit-unused-resultstrictwarningframeworks/androiddepth fastperformance, android

What it finds

SharedPreferences.Editor.commit() writes synchronously and returns whether it worked. When the result is not looked at, apply() does the same write in the background without blocking the thread, which is often the main thread.

From the rule's source, preferences_commit.rs.

Example from the rule's tests
fun save(prefs: SharedPreferences) {    prefs.edit().putString("name", "x").commit()tx0 reports: commit() blocks for a disk write whose result is ignored    val ok = prefs.edit().putInt("n", 1).commit()    prefs.edit().putInt("n", 2).apply()    transaction.commit()}
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:preferences-commit-unused-result" = "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:preferences-commit-unused-result] reason="..."

tx0 explain tx0/kotlin:preferences-commit-unused-result shows the same in the terminal. Found a false positive? Report it.