src/rules/suspicious/idle.bend checks
raw source on the hub · import bolt@1.62.0.0/src/rules/suspicious/idle.bend as Idle
rule idle: a parameter a def only hands back to itself. Reported: a
parameter of a def that calls itself, when the def's body reads its name
and every reading of it sits in the parameter's own argument of a
self-call: loop(left, count) whose one use of count is
loop(rest, Nat.add(count, 1n)). The value goes round the recursion and
reaches nothing: no result of the def depends on it, whatever it was
meant to count or carry. One finding, on the parameter's name.
A reading is a name leaf spelled as the parameter, or as it then a dot. A
self-call is the def's name and then a ( group; its arguments are the
group's kids split at their commas, counted from 0, and the parameter's
own argument is the one at its place among the def's parameters. A
reading is in that argument when the nearest self-call around it holds it
there, at any depth below (Nat.add(count, 1n), a lambda's body): inside
a self-call nested in that argument it is judged by the nested call, so
go(t, go(u, acc, 0)) reads acc where the inner call takes it as
another parameter. A reading anywhere else is a use and the parameter is
left alone: a match on it, an argument of another call or in another
slot, a base arm that returns it, a pattern or a let that binds the name
again (the spelling is what is read, so a shadowed name counts as read).
Left alone as well: a parameter with no type or one written -x or ~x
(erased, a template), one whose name is not lowercase (a type) or starts
with _ (there to be ignored), one the signature names again (a later
parameter's type or the return type depends on it), a def that does not
call itself (unused reports a parameter nothing reads), a def that is a
proof (it returns -> {..}, or has no type), and every def of a law file
or a proof file.
The fix is to delete the parameter, with its argument at each call, or to
read it where it was meant to count: a dropped accumulator, a bound never
compared and a counter nobody prints all have this shape, and bend
accepts each of them, since passing a value on is a use to it.
7 imports
import Base import ../../src.bend as Src import ../../finding.bend as F import ../../syntax/lex.bend as Lex import ../../syntax/tree.bend as Tree import ../calls.bend as Calls import ../../lazy/lazy.bend as Lazy
Types
type At source · line 45 · raw
Data
where the walk stands against the self-calls around it: outside the parameter's own argument (AOut), somewhere inside it (AOwn), or among the arguments of a self-call, in the one at this place (AArg)
AOutAt
AOwnAt
AArg@place:Nat -> At
Definitions
def own source · line 51 · raw
@at:At -> @slot:Nat -> Bool
is the walk in the parameter's own argument?
def inner source · line 62 · raw
@at:At -> @slot:Nat -> At
where the walk stands inside a node met here (a group, a statement): in the own argument or out of it, its place among the arguments forgotten
def past.next source · line 66 · raw
@at:At -> At
among a self-call's arguments, the next one
def past source · line 75 · raw
@comma:Bool -> @at:At -> At
where the walk stands past a leaf: past a comma among a self-call's arguments, in the next one
def reads source · line 83 · raw
@k:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/lex.TokKind -> @+t:String -> @+nm:String -> Bool
does a leaf read the parameter: a name spelled as it, or as it then a dot?
def into source · line 88 · raw
@o:String -> @t:String -> @name:String -> @at:At -> @slot:Nat -> At
where the walk stands in a group after a leaf: at the first argument when
they are a self-call (the def's name, then (), else inside
def stray source · line 92 · raw
@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+at:At -> @+slot:Nat -> @+nm:String -> @+name:String -> Bool
is the parameter read anywhere but in its own argument of a self-call?
def spelled source · line 117 · raw
@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+nm:String -> Nat
how many leaves under the node read the name
def named source · line 132 · raw
@p:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> Maybe<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/lex.Tok>
a parameter's name token: its first lowercase name before the colon,
none for a type parameter (-A: Type)
def counts source · line 145 · raw
@p:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @nm:String -> Bool
may a parameter of this name be reported: typed, neither erased nor a template, and not named to be ignored?
def idle source · line 150 · raw
@p:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+slot:Nat -> @sig:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+body:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+nm:String -> @+name:String -> Bool
is the parameter idle: one that counts, named once in the signature, read in the body, and never but in its own argument of a self-call?
def say source · line 157 · raw
@nm:String -> @name:String -> String
what a finding says
def cite source · line 162 · raw
@+nm:String -> @l:U32 -> @c:U32 -> @name:String -> @path:String -> 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding
the finding on the parameter's name
def found source · line 166 · raw
@hit:Bool -> @nm:String -> @l:U32 -> @c:U32 -> @name:String -> @path:String -> @more:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding> -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>
a finding on the parameter when it is idle, before the rest
def param source · line 182 · raw
@m:Maybe<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/lex.Tok> -> @p:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @slot:Nat -> @sig:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @body:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @path:String -> @more:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding> -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>
a parameter with a name, judged; one with none adds nothing
def params source · line 200 · raw
@ps:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node> -> @+slot:Nat -> @+sig:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+body:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> @more:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding> -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>
the idle parameters among these, the first of them at this place, in order, before the rest
def judged source · line 216 · raw
@path:String -> @sig:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @body:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @name:String -> Bool
is the def judged: not exempt, and one that calls itself?
def check.go source · line 220 · raw
@ds:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/rules/calls.Def> -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>
every idle parameter of every def, in order
def check source · line 230 · raw
@s:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/src.Src -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>
the rule