~/bend-docscommunity

src/rules/correctness/strings.bend checks

raw source on the hub · import 0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/rules/correctness/strings.bend as Strings

rule strings: a match whose arms are string literals (case "foo":) with more than 64 literal characters in all. Each string arm lowers into nested per-char tests, and the checker's time and memory grow with the characters (bend 2.0.16: 45 characters take 0.8 s and 0.35 GB, 100 take 2 s and 1 GB, 480 take 12 s and 4.8 GB; pi-bend BEND-001/016 was 16 long event names). The number of arms barely matters, so only the characters count. Classify the string once through a lookup table into a sum type, and match on that. Only a case's first match column is read: string literals in a later column of match a b: do not count. A literal with no closing quote (an unterminated string) is not a string arm.

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 ../../lazy/lazy.bend as Lazy
import ../tokens.bend as T

Types

type Tally source · line 20 · raw

Data

the string arms of a match, and their characters

Definitions

def closes source · line 26 · raw

@cs:List<&2, Char> -> @+esc:Bool -> @+shut:Bool -> Bool

does the text end on a closing quote, its escapes read as a backslash and the one char after it? esc: the char before was an unescaped backslash; shut: it was an unescaped quote

def opened source · line 35 · raw

@cs:List<&2, Char> -> Bool

past its opening char, do the chars end on a closing quote?

def terminated source · line 44 · raw

@+tt:String -> Bool

is a string token closed: past its opening quote, does it end on a quote no backslash escapes? "\", an unclosed string at the end of the file, is not

def literal source · line 49 · raw

@kids:0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/syntax/tree.Node -> Maybe<&2, U32>

the characters of the string a case pattern opens with, if it does and the string is closed

def one source · line 58 · raw

@mm:Maybe<&2, U32> -> U32

one for a string arm, else zero

def size source · line 66 · raw

@mm:Maybe<&2, U32> -> U32

a string arm's characters, else zero

def tally source · line 74 · raw

@body:0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/syntax/tree.Node -> @+nn:U32 -> @+chars:U32 -> Tally

the string arms among a match's statements

def report source · line 87 · raw

@tt:Tally -> @+path:String -> @+ll:U32 -> @+cc:U32 -> List<&2, 0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/finding.Finding>

a finding when the match's strings are too long in all

def on_stmt source · line 95 · raw

@+kids:0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/syntax/tree.Node -> @+body:0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/syntax/tree.Node -> @+path:String -> List<&2, 0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/finding.Finding>

a statement: a match is tallied

def walk source · line 101 · raw

@nn:0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/syntax/tree.Node -> @+path:String -> List<&2, 0x582b4b0fdf3dafdeecc8c3bfddc5e4db/src/finding.Finding>

every statement, at any depth

def check source · line 111 · raw

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

the rule