Fork me on GitHub
#clj-kondo
<
2022-01-06
>
ghadi21:01:14

linter idea: (update x :foo #(bar % a b)) -> (update x :foo bar a b)

dpsutton21:01:27

sounds tough to discern the good case from (update x :foo #(bar a % b)) but i like that idea

dpsutton21:01:49

or maybe i’m inventing complexity and its not that hard

ghadi21:01:03

probalby not hard for borkdude 🙂

ghadi21:01:10

I guess any such function (like swap!) where the update fn uses its argument in the second position of its singular form

borkdude21:01:26

This is probably doable. Up till 3 args this seems ok, but more than that it's going to use apply which in some cases would affect performance somewhat? Depends on hot path of course

ghadi21:01:40

(swap! atom (fn [x] (something x anything)))

borkdude21:01:08

it's always a single arg function in the first case

ghadi21:01:11

the function will be arity (+ 1 (count trailing-args-to-swap))

ghadi21:01:39

but the 99% common case is that beginners don't know about the update function taking multiple args

ghadi21:01:11

that case: (update-in m [:foo] #(inc %))

ghadi21:01:52

rather than (update-in m [:foo] inc)

ghadi21:01:27

in fact a more generic lint would be if someone passes a function #(inc %) rather than inc

ghadi21:01:50

anywhere (like map), not just update-in

ghadi21:01:01

I didn't try that, maybe it's already linted

dpsutton21:01:03

I’m trying to think of corner cases where it might be difficult to realize you can’t use this

(update x :foo #(bar a (merge % {:a :b}) b))
(update x :foo #(bar a (let [x %] (inc x)) b))
(update x :foo #(bar a (inc %) b))
(update x :foo #(bar a % %))

ghadi21:01:35

those are all fine

ghadi21:01:01

the rule would be: "anonymous arity-1 function of a single expression passed to swap/update, where the singular arg appears only in the second position in the form"

ghadi21:01:12

but I think I'd be ok with a more general rule

borkdude21:01:48

the hardest part would be a name for the linter

ghadi21:01:53

anonymous function that just forwards to a var: (fn [x] (foo x)) or #(foo %) rather than foo

👏 1
ghadi21:01:56

unnecessary-anonymous-fn-expr

ghadi21:01:14

that would catch 99% of the update/swap cases too

borkdude21:01:34

ok, that's a good one to start with.