Skip to content

Commit d864d6f

Browse files
committed
Add a command that removes unused destructured keys
clojure-remove-unused-keys (C-c C-r k) removes what the top-level form at point destructures from maps but never uses: names in :keys, :strs and :syms, :as names and names bound to keys, along with their :or defaults. It covers the arglists of fn, defn, defmethod, letfn and methods, and the bindings of let-like forms. A map with nothing left becomes _. Comments and #_ forms among the names stay, and a map that still has one isn't empty. A name counts as used when any unqualified symbol with that name appears in its scope. That ignores shadowing, so it can only miss a removal. Names starting with _ stay, as do positional args and vector destructuring. A map left with only :as stays a map, as map destructuring turns a seq into a map and the bare name wouldn't. Quoted and syntax-quoted forms are left alone, being data or a macro's template. With no syntax tree to go by, a small reader turns the form into one, which keeps the rest the same as in clojure-ts-mode.
1 parent 370c7ff commit d864d6f

4 files changed

Lines changed: 871 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### New features
6+
7+
* [#708](https://github.com/clojure-emacs/clojure-mode/pull/708): Add `clojure-remove-unused-keys` (`C-c C-r k`), which removes the names the top-level form at point destructures from maps but never uses, along with their `:or` defaults.
8+
59
### Changes
610

711
* [#705](https://github.com/clojure-emacs/clojure-mode/pull/705): Rename the `#_` toggle commands to `clojure-toggle-discard`, `clojure-toggle-discard-surrounding-form` and `clojure-toggle-discard-defun`, matching Clojure's "discard" terminology (the old `clojure-toggle-ignore*` names remain as obsolete aliases).

‎README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ Here's a summary of the keybindings available in `clojure-mode`:
489489
| `C-c C-r _` | `clojure-toggle-discard-surrounding-form` |
490490
| `C-c C-r P` | `clojure-promote-fn-literal` |
491491
| `C-c C-r a` | `clojure-add-arity` |
492+
| `C-c C-r k` | `clojure-remove-unused-keys` |
492493
| `C-c C-r (` | `clojure-convert-collection-to-list` |
493494
| `C-c C-r '` | `clojure-convert-collection-to-quoted-list` |
494495
| `C-c C-r {` | `clojure-convert-collection-to-map` |
@@ -645,6 +646,30 @@ without affecting the namespace declaration.
645646

646647
<img width="512" src="/doc/clojure-add-arity.gif">
647648

649+
### Remove unused destructured keys
650+
651+
`clojure-remove-unused-keys`: Remove what the top-level form at point
652+
destructures from maps but doesn't use: names in `:keys`, `:strs` and `:syms`,
653+
`:as` names and names bound to keys, along with their `:or` defaults. That
654+
covers arglists (`defn`, `fn`, `defmethod`, `letfn`, `reify` and the like) and
655+
the bindings of `let`, `loop`, `for`, `doseq` and the like. A map with nothing
656+
left becomes `_`, and one with only its `:as` name left becomes that name.
657+
658+
```clojure
659+
(defn handle [{:keys [id user] :or {user "anon"} :as req}]
660+
(process id))
661+
662+
;; becomes
663+
664+
(defn handle [{:keys [id]}]
665+
(process id))
666+
```
667+
668+
A name stays when a symbol with that name appears anywhere in its scope, even
669+
if it's a local that shadows it, and so does one starting with `_`, which marks
670+
it unused on purpose. Positional args and vector destructuring are left alone,
671+
as removing one would change what the others bind.
672+
648673
## Related packages
649674

650675
- [clojure-mode-extra-font-locking][] provides additional font-locking

0 commit comments

Comments
 (0)