Skip to content

rule candidate: lenfuel — length walk only to fuel a walk of the same value #103

Description

@noah-emp

Category

suspicious

Shape

String.length(s) / List.len(xs) (or a hand-rolled count) only to feed fuel for a walk of that same s/xs, when the walk can be structurally recursive on the value itself.

fuel (U006) flags a literal Nat in a fuel-named parameter. This would flag deriving fuel from a full length walk of the value about to be walked again.

Bad

def escape_loop(fuel: Nat, s: String, acc: List<&2, Char>) -> String:
  match fuel:
    case 0n:
      String.from_list(List.reverse(&2, Char, acc))
    case 1n++p:
      match s:
        case SNil{}:
          String.from_list(List.reverse(&2, Char, acc))
        case SCon{c, t}:
          escape_loop(p, t, c <> acc)

def escape(s: String) -> String:
  escape_loop(String.length(s), s, [])

Good

def escape(s: String, acc: List<&2, Char>) -> String:
  match s:
    case SNil{}:
      String.from_list(List.reverse(&2, Char, acc))
    case SCon{c, t}:
      escape(t, c <> acc)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestsuspiciousProbably a slip / perf smell

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions