Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect unused recursive parameters #19

Open
jfmengels opened this issue Dec 26, 2020 · 2 comments
Open

Detect unused recursive parameters #19

jfmengels opened this issue Dec 26, 2020 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jfmengels
Copy link
Owner

What the rule should do:

Detect parameters of recursive function that are only used for passing it to the function again.

What problems does it solve:

This would help remove the argument and simplify both the recursive function and its call sites.

Example of things the rule would report:

recursive unusedValue list =
  case list of
    [] -> 1
    x :: xs -> x + recursive unusedValue xs

In the case above, unusedValue is marked as used by NoUnused.Variables because it is used as an argument of recursive. But we never use the value anywhere else, meaning it serves no purpose.

We can report the same thing even if different values are passed in some cases.

recursive unusedValue list =
  case list of
    [] -> 1
    x1 :: x2 :: xs -> x1 + recursive x2 xs
    x :: xs -> x + recursive unusedValue xs

In the example above, the value of unusedValue may change, but we never use it anywhere.

Slightly more advanced:

recursive unusedValue list =
  case list of
    [] -> 1
    x :: xs -> x + recursive (unusedValue - 1) xs

For this one, it is used in an expression, but that expression ultimately ends up only being used as the same argument for recursive, meaning whatever the value may turn out to be, it will not serve any purpose.

To go one step further, we could detect let declarations

recursive unusedValue list =
  case list of
    [] -> 1
    x :: xs ->
      let value = unusedValue - 1
      in x + recursive value xs

And also pipeline usage (note that I've reversed arguments)

recursive list unusedValue =
  case list of
    [] -> 1
    x :: xs -> unusedValue |> recursive xs

Example of things the rule would not report:

recursive accumulator list =
  case list of
    [] -> accumulator
    x :: xs -> recursive (accumulator + x) xs

When (not) to enable this rule:

🤷‍♂️

I am looking for:

  • Other similar cases I've missed that could fit in this rule.
  • Someone to implement it with/for me
@miniBill
Copy link

Something something mark arguments has having an impact on flow something something fixpoint?

@jfmengels jfmengels added enhancement New feature or request help wanted Extra attention is needed labels Jul 22, 2022
@jfmengels
Copy link
Owner Author

Most of these have been implemented a while ago. Only the let declaration case hasn't:

recursive unusedValue list =
  case list of
    [] -> 1
    x :: xs ->
      let value = unusedValue - 1
      in x + recursive value xs

This one would be the most complex because as @miniBill mentioned, this will likely need a bit more flow-like analysis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants