jfmengels / elm-review-simplify

Provides elm-review rules to simplify your Elm code
https://package.elm-lang.org/packages/jfmengels/elm-review-simplify/latest/
BSD 3-Clause "New" or "Revised" License
20 stars 9 forks source link

Detect unnecessary lazy #160

Open jfmengels opened 12 months ago

jfmengels commented 12 months ago

(Originally suggested in #109)

We should detect when lazy functions (not the Html one) that allow recursive elements is used unnecessarily.

This would be useful for the following modules:

Success

Lazy is needed

probabilities : Random.Generator (List Float)
probabilities =
  Random.andThen identity <|
    Random.uniform
      [ Random.constant []
      , Random.map2 (::)
          (Random.float 0 1)
          (Random.lazy (\_ -> probabilities))
      ]

We'd need to check for cycles, because sometimes the recursion is indirect

-- Probably a terrible example
x =
     Random.lazy (\_ -> y)
y = x

Fail

Lazy is unnecessary because referenced in a function (and not a constant)

probabilities : Random.Generator (List Float)
probabilities =
  Random.andThen identity <|
    Random.uniform
      [ Random.constant []
      , Random.map2 (::)
          (Random.float 0 1)
          (Random.lazy (\_ -> otherGenerator))
      ]

Lazy is unnecessary, because not self-recursive

probabilities : Random.Generator (List Float)
probabilities =
  Random.andThen identity <|
    Random.uniform
      [ Random.constant []
      , Random.map2 (::)
          (Random.float 0 1)
          (Random.lazy (\_ -> otherGenerator))
      ]

Advice

I recommend we start looking at this by implementing parsers and reviewing known language parsers (the #language-implementation Slack channel probably has quite a few we can run it on). As long as the fix doesn't introduce a compiler error, the removal should should be okay.