# prefer-negative-index

Prefer negative indices in equivalent at calls

`array.at(array.length as isize - offset)` performs the same lookup as `array.at(-offset)` for a positive constant offset.
Instead, you SHOULD pass the negative offset directly.

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

## Reported

```ds title="main.ds"
function penultimate(values: int32[]): int32 | undefined {
    return values.at((values.length as isize) - 2);
}
```

## Accepted

```ds title="main.ds"
function penultimate(values: int32[]): int32 | undefined {
    return values.at(-2);
}
```

## Prior art

- [eslint-plugin-unicorn · prefer-negative-index](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-negative-index.md)

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