(#) Missing `add` call in `buildList`
!!! WARNING: Missing `add` call in `buildList`
This is a warning.
Id
: `BuildListAdds`
Summary
: Missing `add` call in `buildList`
Severity
: Warning
Category
: Correctness
Platform
: Any
Vendor
: Android Open Source Project
Feedback
: https://issuetracker.google.com/issues/new?component=192708
Since
: 8.4.0 (April 2024)
Affects
: Kotlin and Java files
Editing
: This check runs on the fly in the IDE editor
Implementation
: [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/BuildListDetector.kt)
Tests
: [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/BuildListDetectorTest.kt)
The `buildList { }` standard library function is a convenient way to
build lists, but you need to actually call `add` on the items.
(##) Example
Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/Cubic.kt:4:Warning: No add calls within buildList lambda; this is
usually a mistake [BuildListAdds]
return buildList { // ERROR
---------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the source file referenced above:
`src/Cubic.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
class Cubic(id: Int)
val _morphMatch = listOf(1)
fun asCubics_broken(progress: Float): List {
return buildList { // ERROR
for (i in _morphMatch.indices) {
Cubic(i) // ERROR: Should have been wrapped in an add call.
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/BuildListDetectorTest.kt)
for the unit tests for this check to see additional scenarios.
(##) Suppressing
You can suppress false positives using one of the following mechanisms:
* Using a suppression annotation like this on the enclosing
element:
```kt
// Kotlin
@Suppress("BuildListAdds")
fun method() {
buildList(...)
}
```
or
```java
// Java
@SuppressWarnings("BuildListAdds")
void method() {
buildList(...);
}
```
* Using a suppression comment like this on the line above:
```kt
//noinspection BuildListAdds
problematicStatement()
```
* Using a special `lint.xml` file in the source tree which turns off
the check in that folder and any sub folder. A simple file might look
like this:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="BuildListAdds" severity="ignore" />
</lint>
```
Instead of `ignore` you can also change the severity here, for
example from `error` to `warning`. You can find additional
documentation on how to filter issues by path, regular expression and
so on
[here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).
* In Gradle projects, using the DSL syntax to configure lint. For
example, you can use something like
```gradle
lintOptions {
disable 'BuildListAdds'
}
```
In Android projects this should be nested inside an `android { }`
block.
* For manual invocations of `lint`, using the `--ignore` flag:
```
$ lint --ignore BuildListAdds ...`
```
* Last, but not least, using baselines, as discussed
[here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).