~/bend-docscommunity

src/rules/suspicious/prefix.bend checks

raw source on the hub · import bolt@1.41.0.0/src/rules/suspicious/prefix.bend as Prefix

rule prefix: a String.take compared to a literal of its length: String.eq(String.take(s, k), "..") or String.eq("..", String.take(s, k)). Among the significant tokens (spaces, newlines and comments dropped): a dotted name String.eq, (, String.take and (, then the take's first argument s, any tokens up to the first comma outside the brackets they open (a call such as String.drop(m, 4n) is one argument), then a Nat literal k, ), ,, a string literal and ); or the dotted name String.eq, (, the string literal, ,, String.take, (, s, the comma, k, ) and ). Only when k's text is the literal's length (an escape, \n or \u{1B}, one char) written in decimal with an n, as 4n for "ERR ". A bracket that closes the take before any such comma ends the scan with no finding. One finding, on the String.eq token, for each such run, and nothing else: a take of another length is another bug. String.take copies k chars into a new String only to compare them; String.starts_with(s, "ERR ") (Base) walks s in place and gives the same answer for every s, False when s is shorter, s any expression. A LAWS.bend or a PROOF.bend, which never runs, is exempt. A comment is never read, and a string literal is one token, so the shape written inside either is not the shape.

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 Mode source · line 32 · raw

Data

where the length of a string literal's text is: before its opening quote, in plain text, just after a backslash, inside a \u{..}, or past its closing quote

type Skip source · line 93 · raw

Data

where the scan of a take's first argument stands: inside it at a depth of brackets it opened, just past the comma that ends it, or at the bracket that closes the take before any such comma

type Arg source · line 99 · raw

Data

the argument as the finding writes it, and the tokens after its comma

Definitions

def is_cp source · line 40 · raw

@+cc:Char -> @+xx:U32 -> Bool

is the char the code point?

def len.add source · line 45 · raw

@mm:Mode -> @+cc:Char -> Nat

what the char adds to the length: one in plain text, but not the closing quote; nothing anywhere else

def len.next source · line 55 · raw

@mm:Mode -> @+cc:Char -> Mode

where the char leaves the length: a backslash starts an escape, a quote closes the literal, u after a backslash opens a \u{..} and } closes it

def len.go source · line 69 · raw

@tt:String -> @+mm:Mode -> Nat

the length of the rest of a literal's text, from where it is

def len source · line 78 · raw

@tt:String -> Nat

the length of a string literal, its text as the lexer keeps it (quotes and escapes as written): each escape one char

def fits source · line 82 · raw

@+kk:String -> @lit:String -> Bool

is the Nat literal's text the literal's length?

def say source · line 86 · raw

@+ss:String -> @+kk:String -> @+lit:String -> String

what the finding says: the take, and the starts_with that replaces it

def skip.comma source · line 103 · raw

@dd:Nat -> Skip

a comma at the depth: the argument's end at depth zero, else inside it

def skip.close source · line 112 · raw

@dd:Nat -> Skip

a closing bracket at the depth: the take's own at depth zero, else one bracket the argument opened

def skip.step source · line 120 · raw

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

where a token of the kind leaves the scan at the depth

def piece source · line 133 · raw

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

a token as the finding writes it: a comma with a space after it, an operator with one on each side

def skip.piece source · line 144 · raw

@sk:Skip -> @kk:0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.TokKind -> @+tt:String -> String

what a token adds to the argument's text, given where it leaves the scan: nothing for the comma that ends the argument

def skip source · line 155 · raw

@toks:List<&2, 0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.Tok> -> @sk:Skip -> @+acc:List<&2, String> -> Maybe<&2, Arg>

the take's first argument, scanned from where the scan stands: what it says (acc: its pieces so far, last first) and the tokens after the comma that ends it; none when the take closes first or the tokens run out

def dotted source · line 170 · raw

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

a dotted name: what String.eq is

def tail_a.ok source · line 179 · raw

@+kk:String -> @+t7:String -> @+t8:String -> @k9:0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.TokKind -> @+t9:String -> @+t10:String -> Bool

after the argument's comma, k, ), ,, a string literal and ): is k the literal's length?

def tail_a source · line 184 · raw

@ma:Maybe<&2, Arg> -> Maybe<&2, String>

the take-first shape's end, after the argument: what the finding says

def tail_b.ok source · line 197 · raw

@+kk:String -> @+t9:String -> @+t10:String -> @+lit:String -> Bool

after the argument's comma, k, ) and ): is k the literal's length?

def tail_b source · line 201 · raw

@ma:Maybe<&2, Arg> -> @+lit:String -> Maybe<&2, String>

the literal-first shape's end, after the argument: what the finding says

def head_a source · line 213 · raw

@k0:0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.TokKind -> @+t0:String -> @+t1:String -> @+t2:String -> @+t3:String -> Bool

a dotted String.eq, (, String.take and (?

def at_a.go source · line 218 · raw

@ok:Bool -> @r3:List<&2, 0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.Tok> -> Maybe<&2, String>

the take-first shape, its head read: the end, after the argument

def at_a source · line 226 · raw

@k0:0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.TokKind -> @+t0:String -> @toks:List<&2, 0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.Tok> -> Maybe<&2, String>

a token of kind k0 and text t0, then these: the take-first shape?

def head_b source · line 234 · raw

@k0:0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.TokKind -> @+t0:String -> @+t1:String -> @k2:0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.TokKind -> @+t3:String -> @+t4:String -> @+t5:String -> Bool

a dotted String.eq, (, a string literal, ,, String.take and (?

def at_b.go source · line 239 · raw

@ok:Bool -> @r5:List<&2, 0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.Tok> -> @+lit:String -> Maybe<&2, String>

the literal-first shape, its head read: the end, after the argument

def at_b source · line 247 · raw

@k0:0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.TokKind -> @+t0:String -> @toks:List<&2, 0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.Tok> -> Maybe<&2, String>

a token of kind k0 and text t0, then these: the literal-first shape?

def either source · line 256 · raw

@ma:Maybe<&2, String> -> @mb:Maybe<&2, String> -> Maybe<&2, String>

the first message, else the second

def at source · line 265 · raw

@+k0:0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.TokKind -> @+t0:String -> @+toks:List<&2, 0x1e9f02c57861332bf231d26359cc8bed/src/syntax/lex.Tok> -> Maybe<&2, String>

a token of kind k0 and text t0, then these: either shape? what the finding says when they make one

def hit source · line 269 · raw

@mm:Maybe<&2, String> -> @+path:String -> @+ll:U32 -> @+cc:U32 -> @more:List<&2, 0x1e9f02c57861332bf231d26359cc8bed/src/finding.Finding> -> List<&2, 0x1e9f02c57861332bf231d26359cc8bed/src/finding.Finding>

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

def walk source · line 277 · raw

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

every run of either shape among the tokens, in order

def check source · line 285 · raw

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

the rule

def check.role source · line 291 · raw

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

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