Skip to content

[Medium] Stringly-typed context selector is brittle #226

@coderabbitai

Description

@coderabbitai

Description

PatternContext::new branches on string literals ("match pattern", "for-loop pattern"), making the context selector brittle and prone to errors.

Details

Location:

  • src/parser/expression/pattern_collection.rs:51

Current implementation:
PatternContext::new uses string literal matching to determine context type:

  • "match pattern"
  • "for-loop pattern"

Problems:

  • String literals are error-prone (typos, case sensitivity)
  • No compile-time exhaustiveness checking
  • Unreachable string branches can slip through
  • Difficult to refactor or extend with new context types

Impact

  • Type safety: No compiler guarantees for valid context types
  • Maintainability: Adding new contexts requires string coordination across call sites
  • Reliability: Typos in string literals lead to runtime failures
  • Refactoring: IDE refactoring tools cannot help with string-based dispatch

Actionable Fix

Replace the string-based context selector with an enum:

  1. Introduce PatternContextKind enum with variants:
    • MatchPattern
    • ForLoopPattern
  2. Replace PatternContext::new string parameter with PatternContextKind
  3. Use match over enum variants instead of string comparisons
  4. Remove unreachable string branch

Benefits:

  • Compile-time exhaustiveness checking
  • Type-safe context selection
  • Better IDE support and refactoring
  • Self-documenting code

Priority

Medium – Type safety and maintainability issue, but not blocking current functionality.

References

Requested by

@leynos

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions