Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support :exclude-pattern in :unused-binding #2045

Merged
merged 1 commit into from Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -27,6 +27,7 @@ For a list of breaking changes, check [here](#breaking-changes).
- [#2025](https://github.com/clj-kondo/clj-kondo/issues/2025): support namespace groups with `:unresolved-namespace` linter
- [#2039](https://github.com/clj-kondo/clj-kondo/issues/2039): :analysis `:symbols` + `:aliased-namespace-symbol` linter gives false positive in quoted symbol
- [#2043](https://github.com/clj-kondo/clj-kondo/issues/2043): support ignore annotation on private calls
- Support `:exclude-pattern` in `:unused-binding`

## 2023.03.17

Expand Down
10 changes: 9 additions & 1 deletion doc/linters.md
Expand Up @@ -1288,7 +1288,7 @@ You can add or override type annotations. See
*Config:*

To exclude unused bindings from being reported, start their names with
underscores: `_x`.
underscores: `_x` or add regex patterns to `:exclude-patterns []`.

To exclude warnings about key-destructured function arguments, use:

Expand Down Expand Up @@ -1329,6 +1329,14 @@ This will disable the warning in:
(defmulti f (fn [a b] a))
```

To exclude bindings named "this" use:

``` clojure
{:linters {:unused-binding {:exclude-patterns ["^this"]}}}
```

Patterns are matched via `re-find`.

### Unused value

*Keyword*: `:unused-value`
Expand Down
11 changes: 11 additions & 0 deletions src/clj_kondo/impl/config.clj
Expand Up @@ -450,6 +450,17 @@
(let [excluded (delayed-cfg config)]
(contains? excluded sym-ns))))

(let [delayed-cfg (fn [config]
(let [excluded (get-in config [:linters :unused-binding :exclude-patterns])
regexes (map re-pattern (filter string? excluded))]
{:regexes regexes}))
delayed-cfg (memoize delayed-cfg)]
(defn unused-binding-excluded? [config binding-sym]
(let [{:keys [:regexes]} (delayed-cfg config)
binding-str (str binding-sym)]
(boolean (some (fn [regex]
(re-find regex binding-str)) regexes)))))

(let [delayed-cfg (fn [config]
(let [excluded (get-in config [:linters :used-underscored-binding :exclude])
syms (set (filter symbol? excluded))
Expand Down
4 changes: 2 additions & 2 deletions src/clj_kondo/impl/linters.clj
Expand Up @@ -618,8 +618,8 @@
defaults (:destructuring-defaults ns)]
(doseq [binding diff]
(let [nm (:name binding)]
(when-not (str/starts-with? (str nm) "_")
;; (prn binding)
(when-not (or (str/starts-with? (str nm) "_")
(config/unused-binding-excluded? (:config ctx) nm))
(findings/reg-finding!
ctx
{:type :unused-binding
Expand Down
5 changes: 4 additions & 1 deletion test/clj_kondo/bindings_test.clj
Expand Up @@ -241,7 +241,10 @@
(is (empty? (lint! "(let [_x 0 {:keys [a b] :as _c} v] [a b _x _c])"
'{:linters {:used-underscored-binding {:level :off}}})))
(is (empty? (lint! "(doto (Object.) (.method))"
'{:linters {:used-underscored-binding {:level :warning}}}))))
'{:linters {:used-underscored-binding {:level :warning}}})))
(is (empty? (lint! "(defmulti foo (fn [this x] x))"
'{:linters {:unused-binding {:level :warning :exclude-patterns ["this"]}}}))))



(deftest unused-destructuring-default-test
Expand Down