From 340ad50004927347f55ad8a5b585c9a91e75bbe1 Mon Sep 17 00:00:00 2001 From: Tom Dalziel <33435574+tomdl89@users.noreply.github.com> Date: Thu, 28 Nov 2024 13:41:04 +0000 Subject: [PATCH] Include if-let etc in :condition-always-true linter (#2441) Include if-let, when-let, if-some, when-some, when-first Adds to functionality of #2272 Co-authored-by: Michiel Borkent --- CHANGELOG.md | 1 + src/clj_kondo/impl/analyzer.clj | 3 ++- test/clj_kondo/condition_always_true_test.clj | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ad40f445e..6cabcbdbb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ For a list of breaking changes, check [here](#breaking-changes). ## Unreleased +- [#2272](https://github.com/clj-kondo/clj-kondo/issues/2272): Report var usage in `if-let` etc condition as always truthy - [#2272](https://github.com/clj-kondo/clj-kondo/issues/2272): Report var usage in `if-not` condition as always truthy - [#2433](https://github.com/clj-kondo/clj-kondo/issues/2433): false positive redundant ignore with hook - Document `:cljc` config option. ([@NoahTheDuke](https://github.com/NoahTheDuke)) diff --git a/src/clj_kondo/impl/analyzer.clj b/src/clj_kondo/impl/analyzer.clj index cc3acc133d..557c6b2d8f 100644 --- a/src/clj_kondo/impl/analyzer.clj +++ b/src/clj_kondo/impl/analyzer.clj @@ -958,7 +958,8 @@ :analyzed))] (lint-two-forms-binding-vector! ctx call bv) (concat (:analyzed bindings) - (analyze-expression** (update ctx :callstack conj [:vector]) condition) + (analyze-expression** (update ctx :callstack conj [:vector]) + (assoc condition :condition true)) (if if? ;; in the case of if, the binding is only valid in the first expression (concat diff --git a/test/clj_kondo/condition_always_true_test.clj b/test/clj_kondo/condition_always_true_test.clj index ce5f08fcdd..108a768a61 100644 --- a/test/clj_kondo/condition_always_true_test.clj +++ b/test/clj_kondo/condition_always_true_test.clj @@ -32,4 +32,12 @@ :level :warning, :message "Condition always true"}] (lint! "(if-not odd? 1 2)" + '{:linters {:condition-always-true {:level :warning}}})) + (assert-submaps2 + [{:file "", + :row 1, + :col 12, + :level :warning, + :message "Condition always true"}] + (lint! "(if-let [a odd?] 1 2)" '{:linters {:condition-always-true {:level :warning}}})))