# manual-filter

Prefer Array.filter over manually collecting matching elements

Creating an array, conditionally pushing each input element, and returning it spells Array.filter manually.
Instead, you SHOULD return the result of `filter` directly.

- Category: style
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
function positive(values: int32[]): int32[] {
    const result: int32[] = [];
    for (const value of values) {
        if (value > 0) {
            result.push(value);
        }
    }
    return result;
}
```

## Accepted

```ds title="main.ds"
function positive(values: int32[]): int32[] {
    return values.filter((value) => value > 0);
}
```

## Prior art

- [Clippy · manual_filter](https://rust-lang.github.io/rust-clippy/master/index.html#manual_filter)

[language/linter/src/rules/style/manual_filter.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/manual_filter.rs#L8)
