# manual-pop-if

Prefer Array.popIf over conditionally popping the last element

Testing an array's last element before popping it performs two separate accesses to the same element.
Instead, you SHOULD use `popIf` to test and remove the last element together.

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

## Reported

```ds title="main.ds"
function popExpected(values: int32[], expected: int32): int32 | undefined {
    return values.last() === expected ? values.pop() : undefined;
}
```

## Accepted

```ds title="main.ds"
function popExpected(values: int32[], expected: int32): int32 | undefined {
    return values.popIf((value) => value === expected);
}
```

## Prior art

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

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