# manual-find

Prefer find over a loop returning the first matching element

A loop that returns its first match and otherwise returns undefined implements `find` manually.
Instead, you SHOULD return the result of `find` directly.

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

## Reported

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

## Accepted

```ds title="main.ds"
function firstPositive(values: int32[]): int32 | undefined {
    return values.find((value) => value > 0);
}
```

## Prior art

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

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