Skip to content

feat(lint): add --fix option to auto-remove unused locals#58

Merged
julienduchesne merged 1 commit intomasterfrom
worktree-lint-fix-option
Feb 27, 2026
Merged

feat(lint): add --fix option to auto-remove unused locals#58
julienduchesne merged 1 commit intomasterfrom
worktree-lint-fix-option

Conversation

@julienduchesne
Copy link
Collaborator

Summary

  • Adds a --fix flag to rtk lint and the standalone jrsonnet-lint binary
  • For the unused_locals check, the fix automatically removes the offending local statement from the source file
  • Fix ranges are computed in the linter and attached to each Diagnostic via a new Fix struct
  • A new apply_fixes public function in jrsonnet-lint applies all fixes to a source string (in reverse offset order to preserve positions)

How it works

  • Single-bind local statements (local x = 1;): the entire statement is removed, including surrounding whitespace and trailing newline
  • Object locals (local x = 1, inside {}): the member is removed, including surrounding whitespace and the trailing comma
  • Multi-bind statements (local x = 1, y = 2;): no auto-fix (would require partial AST surgery); these are still reported as diagnostics
  • When --fix is used, any diagnostics without a fix are still reported and cause a non-zero exit

Test plan

  • cargo test -p jrsonnet-lint — 30 tests pass (4 new tests for apply_fixes)
  • make test — full suite passes
  • Manual: rtk lint --fix <dir> rewrites files in place
  • Manual: jrsonnet-lint --fix <file> rewrites file in place
  • Manual: jrsonnet-lint --fix - (stdin) warns and reports diagnostics normally

🤖 Generated with Claude Code

Add a --fix flag to both `rtk lint` and the standalone `jrsonnet-lint`
binary. For the unused_locals check, the fix removes the entire local
statement (for single-bind statements) or the object local member,
including surrounding whitespace and trailing commas.

Multi-bind statements (local x = 1, y = 2;) are not auto-fixed since
partial removal would require more complex AST surgery.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link

Benchmark Results

Benchmark Test vs tk vs base
Diff cluster_scoped 48.14x faster equal
Diff configmap_unchanged 29.8x faster equal
Diff deployment_nested_changes 29.36x faster equal
Env List Single Inline Directory (--json) 47.08x faster equal
Env List Single Inline File (--json) 47.33x faster equal
Env List All Environments (--json) 86.16x faster equal
Eval Single Static Environment 15.14x faster equal
Eval Inline Environment File 22.91x faster equal
Eval Eval Expression - Deeply Nested Path 14.98x faster equal
Export (Full) Single Static Environment 5.51x faster equal
Export (Full) Single Inline Environment 14.21x faster equal
Export (Full) All Environments 12.06x faster equal
Export (Replace) Single Static Environment 5.17x faster equal
Export (Replace) Single Inline Environment 14.65x faster equal
Export (Replace) All Environments 19.63x faster equal
Tool Importers Count Single Lib Directory (Non-Recursive) 2.13x faster equal
Tool Importers Count Single Lib Directory (Recursive) 2.12x faster equal
Tool Importers Count Entire Lib Directory (Non-Recursive) 1.43x faster equal
Tool Importers Count Entire Lib Directory (Recursive) 1.07x faster equal
Tool Importers Global Lib File 2.26x faster equal
Tool Importers Single Env-Specific Lib File 1.68x faster equal
Tool Importers All Env-Specific Lib Files 6.77x faster equal
Tool Importers All Lib Files 7.02x faster equal
Tool Importers All Jsonnet Files 11.3x faster equal
Tool Imports Single Static Environment 3.44x faster equal
Tool Imports Single Inline Environment File 3.38x faster equal

Full results available in workflow artifacts.

Benchmark run on commit 5c942dc3efb0a1f03f10529a08a52a900b99d137

@julienduchesne julienduchesne merged commit 90de45f into master Feb 27, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant