feat(rules): detect or {} silent fallback pattern #46

Closed
opened 2026-05-25 13:42:25 +02:00 by michael · 5 comments
Owner

Goal

fragjan should detect the or {} silent fallback pattern in Lua code:

local runtime = cfg._runtime or {}
local result  = get_data()  or {}

This pattern swallows nil returns that should be errors.

Challenge

or {} has legitimate uses (e.g. default parameters in pure utility
functions). A blanket rule will have false positives.

Options:

  1. Warn on all or {} -- developer suppresses legitimate cases
  2. Limit to specific file patterns (e.g. adapters/, core/)
  3. Needs a smarter primitive (context-aware)

Option 1 with suppression is the low-tech path -- evaluate first.

Acceptance criteria

  • Approach decided
  • Rule detects or {} in target files
  • Severity: warn
## Goal fragjan should detect the `or {}` silent fallback pattern in Lua code: ```lua local runtime = cfg._runtime or {} local result = get_data() or {} ``` This pattern swallows nil returns that should be errors. ## Challenge `or {}` has legitimate uses (e.g. default parameters in pure utility functions). A blanket rule will have false positives. Options: 1. Warn on all `or {}` -- developer suppresses legitimate cases 2. Limit to specific file patterns (e.g. `adapters/`, `core/`) 3. Needs a smarter primitive (context-aware) Option 1 with suppression is the low-tech path -- evaluate first. ## Acceptance criteria - [ ] Approach decided - [ ] Rule detects `or {}` in target files - [ ] Severity: warn
Author
Owner

Additional case in core/loader.lua load_pack():

applies_to = data.applies_to or {}

A pack without applies_to silently never matches any project type when using
auto-detection (load_packs_for_type). No warning, pack just disappears.

This was intentional (packs used only via --pack explicitly) but is invisible
when it is a misconfiguration. Should produce [WARN] so the author sees it.

Additional case in core/loader.lua load_pack(): applies_to = data.applies_to or {} A pack without applies_to silently never matches any project type when using auto-detection (load_packs_for_type). No warning, pack just disappears. This was intentional (packs used only via --pack explicitly) but is invisible when it is a misconfiguration. Should produce [WARN] so the author sees it.
Author
Owner

Additional case in core/detect.lua:

Both read_local_config() and read_fragjan_config() silently swallow parse
errors via pcall and return {}. A corrupt .merkwerk or .fragjan produces no
warning -- the user only sees the downstream error 'no project type found'
with no indication why.

Additional case in core/detect.lua: Both read_local_config() and read_fragjan_config() silently swallow parse errors via pcall and return {}. A corrupt .merkwerk or .fragjan produces no warning -- the user only sees the downstream error 'no project type found' with no indication why.
Author
Owner

Additional case in core/config.lua:

try_parse() silently returns nil on parse errors. A corrupt but present
fragjan.conf produces no warning -- downstream error is 'program config
not found' even though the file exists and is just unparseable.

Additional case in core/config.lua: try_parse() silently returns nil on parse errors. A corrupt but present fragjan.conf produces no warning -- downstream error is 'program config not found' even though the file exists and is just unparseable.
Author
Owner

core/modules.lua: read_require_path() silently returns nil for both missing and corrupt module.conf -- caller cannot distinguish the two cases.

core/modules.lua: read_require_path() silently returns nil for both missing and corrupt module.conf -- caller cannot distinguish the two cases.
Author
Owner

Implemented: pattern widened from ") or {}" to "or {}" to catch non-function-call cases.

Message updated accordingly. 8 matches in fragjan, all plausible, no obvious false positives.

Implemented: pattern widened from ") or {}" to "or {}" to catch non-function-call cases. Message updated accordingly. 8 matches in fragjan, all plausible, no obvious false positives.
michael 2026-05-26 15:44:20 +02:00
michael added this to the Basic Version milestone 2026-05-30 16:05:00 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
DAW/fragjan#46
No description provided.