rsc-last-item

the last item of a list written as xs[size(xs) - 1].

Group
Code
Runs
On by default
Level
warning
Reads
Every file
Fix
--fix fixes it: indexes the list from its end

Why

A negative index counts from the end of a list or a str, so xs[-1] is the last item and says so without naming the list twice.

The fix, as Ur suggests it: write xs[-1].

For agents

Write xs[-1] for the last item of a list, never xs[size(xs) - 1].

Example

checks/Calls.rsc
module checks::Calls

int lastArgument(list[int] arguments) = arguments[size(arguments) - 1];
What Ur prints
$ ur lint --rule rsc-last-item
warning[rsc-last-item]: write `arguments[-1]`
  --> ./checks/Calls.rsc:3:41 `arguments[size(arguments) - 1]`
  = hint: write `xs[-1]`; `--fix` does this
0 clone classes and 1 `rsc-last-item` finding in 1 file

Try it in the playground →