Skip to content

Commit

Permalink
Add redundant-nesting linter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdl89 committed Nov 6, 2023
1 parent 8034c65 commit dce2aad
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/clj_kondo/impl/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
:unused-import {:level :warning}
:single-operand-comparison {:level :warning}
:single-logical-operand {:level :warning}
:redundant-nesting {:level :warning}
:single-key-in {:level :off}
:missing-clause-in-try {:level :warning}
:missing-body-in-when {:level :warning}
Expand Down
3 changes: 2 additions & 1 deletion src/clj_kondo/impl/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@
(and (not= :redundant-do type)
(not= :redundant-call type)
(not= :redundant-let type)
(not= :single-logical-operand type))
(not= :single-logical-operand type)
(not= :redundant-nesting type))
;; but if we get here, then the amount of findings has to be bigger than 1
(> (count findings) 1))
f (collapse-cljc-findings findings)
Expand Down
23 changes: 22 additions & 1 deletion src/clj_kondo/impl/linters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@
:single-logical-operand
(format "Single arg use of %s always returns the arg itself" call-name))))))

(defn lint-redundant-nesting
"Lints calls of variadic functions/macros when nested."
[call]
(let [[[call-ns call-name] parent] (:callstack call)]
(when (and (utils/one-of call-ns [clojure.core cljs.core])
(utils/one-of call-name [* *' + +' and comp concat every-pred
lazy-cat max merge min or some-fn str])
(= [call-ns call-name] parent))
(node->line
(:filename call)
(:expr call)
:redundant-nesting
(format "Nested use of %s is redunant" call-name) ))))

#_(require 'clojure.pprint)

(defn lint-var-usage
Expand Down Expand Up @@ -394,7 +408,11 @@
single-logical-operand-error
(and call?
(not (utils/linter-disabled? call :single-logical-operand))
(lint-single-logical-operand call))]]
(lint-single-logical-operand call))
redundant-nesting-error
(and call?
(not (utils/linter-disabled? call :redundant-nesting))
(lint-redundant-nesting call))]]
(when (and (not call?)
(identical? :fn (:type called-fn)))
(when (:condition call)
Expand All @@ -415,6 +433,9 @@
(when single-logical-operand-error
(findings/reg-finding! ctx
single-logical-operand-error))
(when redundant-nesting-error
(findings/reg-finding! ctx
redundant-nesting-error))
(when (and (:private called-fn)
(not= caller-ns-sym
fn-ns)
Expand Down

0 comments on commit dce2aad

Please sign in to comment.