You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It can be necessary to swap two variables. This usually requires an intermediate variable, but with destructuring, Clojure can perform this in a single line. However, without an intermediate variable or destructuring, manually swapping can result in both variables ending up with the same value.
567
+
It can be necessary to swap two variables. This usually requires an intermediate variable, but with destructuring, Clojure can perform this in a single line. If the destructuring is done incorrectly, then the assignment is a no-op, indicating a bug.
564
568
565
569
### Examples
566
570
567
571
```clojure
568
572
; avoid
569
-
(let [a b
570
-
b a] ...)
573
+
(let [[a b] [a b]] ...)
571
574
572
575
; prefer
573
576
(let [[a b] [b a]] ...)
@@ -800,7 +803,7 @@ In interop scenarios, it can be necessary to add a type hint to mark a function'
800
803
801
804
| Enabled by default | Safe | Autocorrect | Version Added | Version Updated |
Instance methods require a target instance. If there's none or it's nil, highly likely there's a bug.
874
+
875
+
This rule ignores when a find is nested in a `doto` or similar form: `(doto (new java.util.HashMap) (.put "a" 1) (.put "b" 2))` will not raise a diagnostic.
876
+
877
+
### Examples
878
+
879
+
```clojure
880
+
; avoid
881
+
(.length)
882
+
(.lengthnil)
883
+
(String/.length)
884
+
(String/.lengthnil)
885
+
886
+
; prefer
887
+
(.length foo)
888
+
(String/.length foo)
889
+
```
890
+
891
+
---
892
+
864
893
## lint/not-empty?
865
894
866
895
| Enabled by default | Safe | Autocorrect | Version Added | Version Updated |
@@ -898,23 +927,21 @@ Skips if the expr is a reader conditional or has a type-hint.
898
927
899
928
| Enabled by default | Safe | Autocorrect | Version Added | Version Updated |
Uniform qualified method values are a new syntax for calling into java code. They must resolve to a single static or instance method and to help with that, a new metadata syntax can be used: `^[]` aka `^{:param-tags []}`. Types are specified with classes, each corrosponding to an argument in the target method: `(^[long String] SomeClass/.someMethod 1 "Hello world!")`. It compiles to a direct call without any reflection, guaranteeing optimal performance.
906
-
907
-
Given that, it is preferable to exclusively use method values.
934
+
Uniform qualified method values are a new syntax for calling into java code. Instead of type hints, methods are qualified with their class (and with the new `:param-tags` aka `^[]` metadata where ambiguous). This will be compiled into direct calls, making them both the most clear and the highest performing way to execute Java interop.
908
935
909
936
### Examples
910
937
911
938
```clojure
912
939
; avoid
913
-
(.toUpperCase"noah")
914
-
(."noah" toUpperCase)
940
+
(.toUpperCasename-string)
941
+
(.name-string toUpperCase)
915
942
916
943
; prefer
917
-
(^[]String/toUpperCase "noah")
944
+
(String/toUpperCasename-string)
918
945
```
919
946
920
947
### Reference
@@ -963,11 +990,9 @@ In the `ns` form prefer `:require :as` over `:require :refer` over `:require :re
963
990
964
991
| Enabled by default | Safe | Autocorrect | Version Added | Version Updated |
`clojure.core/rand-int` returns an integer between `0` (inclusive) and `n` (exclusive), meaning that a call to `(rand-int 1)` will always return `0`.
993
+
| true | true | false | 1.21.0 | 1.22.0 |
969
994
970
-
Checks the following numbers: `0`, `0.0`, `1`, `1.0`, `-1`, `-1.0`
995
+
`clojure.core/rand-int` generates a float between `0` and `n` (exclusive) and then casts it to an integer. When given `1` (or a number less than `1`), `rand-int` will always return `0`.
971
996
972
997
### Examples
973
998
@@ -978,7 +1003,6 @@ Checks the following numbers: `0`, `0.0`, `1`, `1.0`, `-1`, `-1.0`
978
1003
(rand-int1)
979
1004
(rand-int1.0)
980
1005
(rand-int-1.0)
981
-
(rand-int1.5)
982
1006
```
983
1007
984
1008
---
@@ -987,11 +1011,9 @@ Checks the following numbers: `0`, `0.0`, `1`, `1.0`, `-1`, `-1.0`
987
1011
988
1012
| Enabled by default | Safe | Autocorrect | Version Added | Version Updated |
A number of core functions take any number of arguments and return the arg
993
-
if given only one. These calls are effectively no-ops, redundant, so they
994
-
should be avoided.
1016
+
A number of core functions take any number of arguments and return the arg if given only one. These calls are effectively no-ops, redundant, so they should be avoided.
995
1017
996
1018
Current list of clojure.core functions this linter checks:
997
1019
@@ -1001,6 +1023,8 @@ Current list of clojure.core functions this linter checks:
1001
1023
*`comp`, `partial`, `merge`
1002
1024
*`min`, `max`, `distinct?`
1003
1025
1026
+
This list can be expanded with the configuration `:fn-names`.
1027
+
1004
1028
### Examples
1005
1029
1006
1030
```clojure
@@ -1018,10 +1042,19 @@ Current list of clojure.core functions this linter checks:
|`:fn-names`|`#{cond->> comp min -> some->> cond-> merge some-> partial distinct? max ->>}`| Set |
1057
+
1025
1058
---
1026
1059
1027
1060
## lint/redundant-str-call
@@ -1056,9 +1089,7 @@ x
1056
1089
1057
1090
Uniform qualified method values are a new syntax for calling into java code. They must resolve to a single static or instance method and to help with that, a new metadata syntax can be used: `^[]` aka `^{:param-tags []}`. Types are specified with classes, each corrosponding to an argument in the target method: `(^[long String] SomeClass/someMethod 1 "Hello world!")`
1058
1091
1059
-
If `:param-tags` is left off of a method value, then the compiler treats it as taking no arguments (a 0-arity static method or a 1-arity instance method with the instance being the first argument). And an `_` can be used as a wild-card in the cases where there is only a single applicable method (no overloads).
1060
-
1061
-
These last two features are where there can be trouble. If, for whatever reason, the Java library adds an overload on type, then both the lack of `:param-tags` and a wild-card can lead to ambiguity. This is a rare occurence but risky/annoying enough that it's better to be explicit overall.
1092
+
An `_` can be used as a wild-card in the cases where there is only a single applicable method (no overloads). If, for whatever reason, the Java library adds an overload on type, then both the lack of `:param-tags` and a wild-card can lead to ambiguity. This is a rare occurence but risky/annoying enough that it's better to be explicit overall.
1062
1093
1063
1094
The styles are named after what they're looking for:
1064
1095
@@ -1208,6 +1239,31 @@ Due to munging rules, underscores in namespaces can confuse tools and libraries
1208
1239
1209
1240
---
1210
1241
1242
+
## lint/update-with-swap
1243
+
1244
+
| Enabled by default | Safe | Autocorrect | Version Added | Version Updated |
If an atom is stored inside if a map, the atom can be changed using `swap!` within an `update` or `update-in` call. However, `swap!` returns the new value, not the atom itself, so the container map will hold the deref'ed value of the atom, not the original atom. If the result of the `update` call is stored/used, this can lead to bugs.
1249
+
1250
+
Additionally, if the return value of the `update` call is ignored, then the `update` form will work as expected (because the return value won't overwrite the existing map and the atom will be updated in place). This should be avoided as it breaks expectations about the value of values and normal behavior.
1251
+
1252
+
### Safety
1253
+
If the `update` call's return value isn't ignored (it's used in an assignment or passed to another call), switching to `swap!` will break the expected return value. Care must be exercised when switching.
1254
+
1255
+
### Examples
1256
+
1257
+
```clojure
1258
+
; avoid
1259
+
(update state :counter swap! + 5)
1260
+
1261
+
; prefer
1262
+
(swap! (:counter state) + 5)
1263
+
```
1264
+
1265
+
---
1266
+
1211
1267
## lint/warn-on-reflection
1212
1268
1213
1269
| Enabled by default | Safe | Autocorrect | Version Added | Version Updated |
0 commit comments