def work
bolt@1.27.0.0 · src/rules/suspicious/eager.bend:102
import bolt@1.27.0.0/src/rules/suspicious/eager.bend as Eager
import Base
import bolt@1.27.0.0/src/finding.bend as F
import bolt@1.27.0.0/src/syntax/lex.bend as Lex
import bolt@1.27.0.0/src/syntax/tree.bend as Tree
102
103
104
105
106
107
108
109
110def work(
111 nn: Tree.Node,
112 +ns: List<&2, String>,
113 +self: String,
114 +path: String,
115 +hot: Bool,
116 +seg: U32,
117 +keep: Bool
118) -> List<&2, F.Finding>:
119 match nn:
120 case Tree.NCons{Tree.Leaf{Lex.Tok{+k, +t, +l, +c}}, Tree.NCons{Tree.Group{Lex.Tok{_, +o, _, _}, +kids, _}, rest}}:
121 +now = work.hot(k, hot, seg, keep)
122 +call = Bool.and(work.name(k), String.eq(o, "("))
123 +is_pick = Bool.and(call, String.eq(t, "Bool.pick"))
124 +known = List.contains(~String, ~String.eq, ns, t)
125 +mine = Bool.and(Bool.not(String.eq(t, self)), known)
126 +report = Bool.and(now, Bool.and(call, mine))
127 +inner = Bool.and(now, Bool.not(is_pick))
128 +more = List.concat(&2, F.Finding,
129 [work(kids, ns, self, path, inner, Bool.pick(U32, is_pick, 0, 9), inner),
130 work(rest, ns, self, path, now, work.seg(k, seg), keep)])
131 Bool.pick(List<&2, F.Finding>, report,
132 F.Finding{path, l, c, U32.from_nat(String.length(t)), "eager",
133 t ++ " is called in a branch of Bool.pick, which evaluates both branches, so it runs whatever the condition; bind it above the pick, or branch with `match` on the condition."}
134 <> more,
135 more)
136 case Tree.NCons{Tree.Leaf{Lex.Tok{+k, t, l, c}}, rest}:
137 work(rest, ns, self, path, work.hot(k, hot, seg, keep), work.seg(k, seg), keep)
138 case Tree.NCons{Tree.Group{open, +kids, close}, rest}:
139 List.concat(&2, F.Finding, [work(kids, ns, self, path, hot, 9, hot), work(rest, ns, self, path, hot, seg, keep)])
140 case Tree.NCons{Tree.Stmt{kind, +kids, body}, rest}:
141 List.concat(&2, F.Finding, [work(kids, ns, self, path, False{}, 9, False{}),
142 work(body, ns, self, path, False{}, 9, False{}),
143 work(rest, ns, self, path, hot, seg, keep)])
144 case Tree.NCons{h, rest}:
145 work(rest, ns, self, path, hot, seg, keep)
146 case other:
147 Nil{}