src/rules/style/reach.bend checks
raw source on the hub · import bolt@1.61.0.0/src/rules/style/reach.bend as Reach
rule reach: a one-character name read far below where it is bound. A
reader who meets n forty lines under its binder has only the letter to go
on; within a few lines the binder is in sight and the letter is enough.
Reported: a parameter, a let, do-bind or lambda name, or a pattern name,
that some use reads from more than max lines below the line that binds
it, by a name spelled with one character. One finding, on the binder.
max is 20 unless the file's bolt.bend sets def reach_max() -> U32.
A name is spelled with one character when, its _ aside, it holds one
letter or digit, however many times it is written: n, nn, n_ and
ttt are one character each, xs, n2 and c0 are two. A letter written
twice says what the letter said, so it is read as the letter.
Left alone: one capital letter (A, T: a type), a quantity parameter
(bare, or typed Quant), a law's for names, an item, a field and a
type-level binder, and every binder of a def that is a proof (no type,
def f(x):, or one that returns -> {..}:
src/rules/suspicious/empty.bend's reading of one), whose names are the
law's. A PROOF.bend is exempt.
The fix is a name that says what the value is, or a def short enough that
the binder stays in sight. The 20: in public Bend 2 code a one-character
name is read a median of 1 line below its binder, and 99 in 100 within 19
(parameters; lets within 12, patterns within 9).
A letter that is a convention (x and y of a position, the a to h
of a hash round) reads no worse far away, and no token tells it from one
that is a shrug: of 30 findings on public code read by hand, about 6 in
10 were names a word plainly helps. The rule is on with style all the
same, as part of a house style; a # noqa: S008 keeps a letter that is
the word.
This rule replaced param (S004), a two-character minimum on parameter
names: under it n became nn one for one (30% of public parameters are
one letter and 0.2% a doubled one; in trees linted by it, 1% and 22 to
33%), which is why a repeated letter counts once here.
10 imports
import Base import ../../paths.bend as Paths import ../../role.bend as Role import ../../src.bend as Src import ../../finding.bend as F import ../../syntax/bind.bend as Bind import ../../syntax/lex.bend as Lex import ../../lazy/lazy.bend as Lazy import ../tokens.bend as T import ../suspicious/empty.bend as Empty
Definitions
def limit source · line 45 · raw
U32
how many lines below its binder a one-character name may be read, unless a bolt.bend says otherwise
def one.under source · line 49 · raw
@ch:Char -> Bool
is the char _?
def one.all source · line 53 · raw
@cs:List<&2, Char> -> @+ch:Char -> Bool
is every char the one given, or _?
def one.go source · line 61 · raw
@cs:List<&2, Char> -> Bool
the chars, their _ aside, are one char written one or more times
def one source · line 69 · raw
@name:String -> Bool
is the name spelled with one character?
def far.hit source · line 74 · raw
@name:String -> @line:U32 -> @t:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Target -> @max:U32 -> Maybe<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos>
the binder a use reads from more than max lines below it, by a one-character name: a use of anything but a binder of the file reads none
def far.keep source · line 83 · raw
@m:Maybe<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos> -> @more:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos> -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos>
a binder read from afar, when there is one, onto the rest
def far source · line 92 · raw
@uses:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Use> -> @+max:U32 -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos>
the binders the uses read from afar by a one-character name, one for each such use
def reaches.at source · line 100 · raw
@p:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos -> @line:U32 -> @col:U32 -> Bool
is the position this line and column?
def reaches source · line 105 · raw
@fr:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos> -> @+line:U32 -> @+col:U32 -> Bool
is the binder at this line and column among those read from afar?
def kind source · line 113 · raw
@k:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.BindKind -> Bool
a parameter, a let, do-bind or lambda name, or a pattern name
def is_param source · line 125 · raw
@k:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.BindKind -> Bool
a parameter
def letter source · line 133 · raw
@cs:List<&2, Char> -> Bool
one character, and a capital: a type parameter
def upper source · line 141 · raw
@nm:String -> Bool
A, T, H
def quant source · line 147 · raw
@+note:String -> Bool
no type, or : Quant: a quantity parameter (a in List<a, A>). The
note is read with its string literals cut to their quote: a : inside a
string is no type
def allowed source · line 153 · raw
@k:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.BindKind -> @nm:String -> @note:String -> Bool
a type parameter, or a parameter that is a quantity: left alone. Only a parameter's note is its own declaration; a let's is its whole line
def has source · line 157 · raw
@ls:List<&2, U32> -> @+line:U32 -> Bool
is the line among these?
def proofs.is_def source · line 165 · raw
@k:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/lex.TokKind -> @t:String -> Bool
is the keyword def?
def proofs.line source · line 173 · raw
@toks:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/lex.Tok> -> U32
the line of the first token, where a def's name is
def proofs source · line 182 · raw
@toks:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/lex.Tok> -> List<&2, U32>
the lines of the names of the defs that are proofs, among the significant
tokens: the token after each def whose header Empty.proofy reads as one
def at_item source · line 192 · raw
@k:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.BindKind -> @line:U32 -> @item:U32 -> U32
the line of the item a binder sits in: its own when it is the item
def flagged source · line 202 · raw
@+fr:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos> -> @+line:U32 -> @+col:U32 -> @+k:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.BindKind -> @nm:String -> @note:String -> @ps:List<&2, U32> -> @item:U32 -> Bool
is the binder reported? Read from afar, of a kind that counts, not left alone, and in no def that is a proof. The first test fails for nearly every binder, and nothing after it is read then
def say source · line 217 · raw
@nm:String -> @max:U32 -> String
what a finding says
def found source · line 222 · raw
@hit:Bool -> @+nm:String -> @line:U32 -> @col:U32 -> @path:String -> @max:U32 -> @more:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding> -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
a finding on the binder when it is reported, before the rest
def check.go source · line 239 · raw
@binds:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Bind> -> @+fr:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos> -> @+ps:List<&2, U32> -> @+path:String -> @+item:U32 -> @+max:U32 -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
every reported binder, in order. item: the line of the item the binders so far sit in
def check.far source · line 257 · raw
@fr:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Pos> -> @binds:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Bind> -> @toks:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/lex.Tok> -> @+path:String -> @max:U32 -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
the findings, given the binders read from afar: with none there is nothing to report, and the tokens are not read for the defs that are proofs
def check.on source · line 272 · raw
@binds:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Bind> -> @uses:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/bind.Use> -> @toks:List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/syntax/lex.Tok> -> @+path:String -> @+max:U32 -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
the rule over a file's binders, uses and tokens; a PROOF.bend's names are the law's
def check source · line 283 · raw
@s:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/src.Src -> @max:U32 -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
the rule, at a limit
def check.role source · line 290 · raw
@r:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/role.Role -> @s:0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/src.Src -> @+max:U32 -> List<&2, 0xf1ae3e6f66de0ffbb5a023d9ed65c91a/src/finding.Finding>
the rule at a limit, told the file's role (src/role.bend): a proof file is skipped