~/bend-docscommunity

src/rules/suspicious/empty.bend checks

raw source on the hub · import bolt@1.33.0.0/src/rules/suspicious/empty.bend as MEmpty

rule empty: a length compared to zero when the value is kept. Among the significant tokens (spaces, newlines and comments dropped): a (, [, { or ,, then a dotted Nat.is_eq, (, and either a dotted List.length, (, any tokens up to its matching ) whose last two are a , and a plain name x, or a dotted String.length, (, a plain name x and ); then ,, 0n and ); and x again, as a plain name, among the tokens after that ) and before the bracket that closes the group the test sits in (brackets opened on the way are skipped over, not ended on). One finding, on the Nat.is_eq token, for each such test. Base's List.length and String.length count every cell (and are not tail calls); List.is_empty and String.is_empty, which take the same arguments, look at one and give the same Bool. The second read is what makes the fix pay on both lanes: x is kept past the test, so the test's own reference is dropped by a count, never by freeing x. Where the test is x's last use, the native lane frees the cells the walk would have, and freeing a list the program never walked costs more than walking it (bend 2.0.38: a 0-1000 char String tested then dropped ran 0.27x as fast as a length, kept 1.57x; JS 1.65x and 1.31x), so a test with no read after it in its group, a test outside any group, and a read before the test are left alone. A LAWS.bend or a PROOF.bend, which never runs, is exempt, and so is a law and a def that is a proof (no type, def f(x):, or one that returns -> {..}) in any file, from its keyword to the next def, type or import. A comment is never read, and a string literal is one token that is no name.

8 imports
import Base
import ../../src.bend as Src
import ../../paths.bend as Paths
import ../../role.bend as Role
import ../../lazy/lazy.bend as Lazy
import ../../finding.bend as F
import ../../syntax/lex.bend as Lex
import ../tokens.bend as T

Types

type Form source · line 35 · raw

Data

which length a test takes: a list's, a String's, or neither

type Last source · line 42 · raw

Data

what the last tokens of a List.length's arguments were: a comma, a plain name right after a comma, or anything else

type Taken source · line 48 · raw

Data

the name a test takes the length of, and the tokens after its )

type Hit source · line 52 · raw

Data

where a finding goes and what it says

Definitions

def opener source · line 57 · raw

@kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> Bool

a bracket that opens a group, or a comma inside one: what may stand right before a test

def code source · line 68 · raw

@kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @tt:String -> String

a token's text where it is code: a string literal or a comment says nothing the rule reads

def dotted source · line 78 · raw

@kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> Bool

a dotted name

def plain source · line 86 · raw

@kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> Bool

a plain name

def form source · line 94 · raw

@+tt:String -> Form

the length a dotted name takes

def last.step source · line 113 · raw

@+kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @+tt:String -> @ll:Last -> Last

what a token leaves of the last tokens: a comma, a name after a comma, or anything else

def last.name source · line 128 · raw

@ll:Last -> @rest:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> Maybe<&2, Taken>

the name a List.length's arguments end in, when the last two were a comma and a plain name

def close.depth source · line 137 · raw

@kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @dd:Nat -> Nat

a bracket at the depth: the length's own ) at depth zero; else the depth it leaves

def close.here source · line 151 · raw

@kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @dd:Nat -> Bool

is the token the bracket that closes the arguments, at depth zero?

def close.list source · line 165 · raw

@toks:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> @+dd:Nat -> @+ll:Last -> @done:Bool -> @got:Maybe<&2, Taken> -> Maybe<&2, Taken>

a List.length's arguments, scanned to the ) that closes them: the name they end in and the tokens after that ); none when they end otherwise or run out. done carries whether the last token read was that ).

def close.str source · line 179 · raw

@toks:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> Maybe<&2, Taken>

a String.length's argument: a plain name and ), then the tokens after

def taken source · line 188 · raw

@fm:Form -> @+toks:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> Maybe<&2, Taken>

the length's argument, by its form

def same source · line 198 · raw

@+kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @+tt:String -> @+xx:String -> Bool

a plain name with x's text?

def kept source · line 204 · raw

@toks:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> @+xx:String -> @+dd:Nat -> @found:Bool -> @live:Bool -> Bool

is x read among the tokens before the bracket that closes the group they sit in? found carries whether the last token read was x, live whether that bracket is still ahead

def say source · line 220 · raw

@fm:Form -> @+xx:String -> String

what a finding says: the test, its fix and why

def tail.go source · line 230 · raw

@ok:Bool -> @fm:Form -> @xx:String -> @ll:U32 -> @cc:U32 -> Maybe<&2, Hit>

a finding, if the test is whole and x kept

def tail source · line 238 · raw

@mm:Maybe<&2, Taken> -> @+fm:Form -> @+ll:U32 -> @+cc:U32 -> Maybe<&2, Hit>

the test's end, ,, 0n and ), then x kept: the finding, if any

def at.go source · line 252 · raw

@ok:Bool -> @+fm:Form -> @+ll:U32 -> @+cc:U32 -> @r4:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> Maybe<&2, Hit>

the test, its head read: the finding, if any

def at source · line 260 · raw

@k0:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @toks:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> Maybe<&2, Hit>

a token of kind k0, then these: a kept test?

def hit source · line 269 · raw

@mm:Maybe<&2, Hit> -> @+path:String -> @more:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/finding.Finding> -> List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/finding.Finding>

the finding a test makes, if any, before the rest

def proofy.head source · line 278 · raw

@k1:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @+k2:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @+t2:String -> Bool

what the two tokens after a def's parameters say: a : (no type: the def fills a law) or -> then { (it returns a proof)

def proofy.end source · line 288 · raw

@toks:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> Bool

what a def's header says once its parameters close

def proofy.fin source · line 298 · raw

@kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @dd:Nat -> Bool

does the token close the parameters: a bracket closing at depth one?

def proofy source · line 312 · raw

@toks:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> @+dd:Nat -> @fin:Bool -> Bool

is the def these tokens head (after def) a proof? Its tokens are read to the bracket that closes its parameters, at depth zero; fin carries whether the last token read was that bracket

def item.other source · line 325 · raw

@+word:String -> @skip:Bool -> Bool

what is skipped after a token that is no def: inside a law, from law on; code again from type or import; else as before

def item.word source · line 330 · raw

@isdef:Bool -> @+word:String -> @rest:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> @skip:Bool -> Bool

what is skipped after a keyword: a def is skipped when it is a proof

def item source · line 338 · raw

@+kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @+tt:String -> @rest:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> @skip:Bool -> Bool

what is skipped after a token: a law, and a def that is a proof, never run

def found source · line 346 · raw

@skip:Bool -> @kk:0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.TokKind -> @rest:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> Maybe<&2, Hit>

the kept test a token opens, if any, outside what is skipped

def walk source · line 355 · raw

@toks:List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/syntax/lex.Tok> -> @+path:String -> @skip:Bool -> List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/finding.Finding>

every kept test among the tokens, in order; skip says whether the tokens so far sit in a law or a def that is a proof

def check source · line 364 · raw

@ss:0x4fc791b90438820096cc5e02a8022c42/src/src.Src -> List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/finding.Finding>

the rule

def check.role source · line 370 · raw

@rr:0x4fc791b90438820096cc5e02a8022c42/src/role.Role -> @ss:0x4fc791b90438820096cc5e02a8022c42/src/src.Src -> List<&2, 0x4fc791b90438820096cc5e02a8022c42/src/finding.Finding>

the rule, told the file's role (src/role.bend): a law or proof file is skipped