Skip to content

rule candidate: spanmat — always materialize a (src,n) span before a read #104

Description

@noah-emp

Category

suspicious

Shape

A (src, n) span is always turned into an owned String (span.str / take/drop) before a consumer that only needs to read those n characters (equality, quoting when plain, hashing). Prefer span-aware helpers that walk src for n steps.

Bad

def span.str(src: String, +n: U32, z: Bool) -> String:
  match src z:
    case s True{}:
      ""
    case SCon{h, t} False{}:
      SCon{h, span.str(t, U32.sub(n, 1), U32.is_zero(U32.sub(n, 1)))}
    case _ _:
      ""

def quote_span(src: String, +n: U32) -> String:
  key.basic(span.str(src, n, U32.is_zero(n)))

Good

def quote_span(src: String, +n: U32) -> String:
  # walk n chars; if all plain, cons quotes; else fall back to escaping
  span.quote(src, n)

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