Fixed
Status Update
Comments
uc...@google.com <uc...@google.com>
es...@google.com <es...@google.com>
tn...@google.com <tn...@google.com>
tn...@google.com <tn...@google.com> #4
This is now implemented for Studio 4.2.
For this test:
fun editSet(prefs: SharedPreferences) {
val s = prefs.getStringSet("key", null) // ?: return
s ?: return
s.removeIf { it.length < 3 }
s.add("error")
val t = mutableSetOf<String>()
t.add("ok")
}
lint warns
src/test/pkg/test.kt:11: Warning: Do not modify the set returned by SharedPreferences.getStringSet()` [MutatingSharedPrefs]
s.removeIf { it.length < 3 }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/test/pkg/test.kt:12: Warning: Do not modify the set returned by SharedPreferences.getStringSet()` [MutatingSharedPrefs]
s.add("error")
~~~~~~~~~~~~~~
0 errors, 2 warnings
Description
The Set<String> returned by this call is modifiable, but it can lead to subtle, mysterious ConcurrentModificationExceptions if modified, as stated in the doc.
AOSP should either wrap this set with an unmodifiable view to catch the errors faster, or at least Lint should try to find modifications of this set.