adobe/elixir-styler

Decreasing range should have -1 as incremetor

Closed this issue · 4 comments

Versions

  • Elixir: see below
  • Styler: v0.11.6

Example Input

Erlang/OTP 26 [erts-14.1.1] [source] [64-bit] [smp:20:20] [ds:20:20:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.16.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Enum.to_list(10..0)
[10, 9, 8, ...]
iex(2)> Enum.to_list(10..0//1)
[]
iex(3)> Enum.to_list(10..0//-1)
[10, 9, 8, ...]
iex(4)>

Stacktrace / Current Behaviour

It adds "1" to a decreasing range, it should add -1 as I see here https://hexdocs.pm/elixir/1.16.0/Range.html

My bad, I misunderstood the point of the fix

iex(14)> String.slice("elixir", 1..-2)
warning: negative steps are not supported in String.slice/2, pass 1..-2//1 instead
  (elixir 1.16.0) lib/string.ex:2368: String.slice/2
  (elixir 1.16.0) src/elixir.erl:405: :elixir.eval_external_handler/3
  (stdlib 5.1.1) erl_eval.erl:750: :erl_eval.do_apply/7
  (elixir 1.16.0) src/elixir.erl:378: :elixir.eval_forms/4
  (elixir 1.16.0) lib/module/parallel_checker.ex:112: Module.ParallelChecker.verify/1
  (iex 1.16.0) lib/iex/evaluator.ex:331: IEx.Evaluator.eval_and_inspect/3

"lixi"
iex(15)> String.slice("elixir", 1..-2//1)
"lixi"
iex(16)> 

Works like a charm, I didn't notice it was only applied to String/List.slice

no worries, appreciate the check.
not going to lie, i don't understand what //1 means at the end of a decreasing range or why these functions want it but... at least the warnings go away?

to answer my confusion, i guess when a range is decreasing but with a positive step it's called a slice and represents something different from what ranges typically do
https://hexdocs.pm/elixir/1.16.0/Range.html#module-ranges-as-slices

makes much more sense! thanks for double checking