rsc-built-replacement

a rewrite whose replacement joins a node's text with literal text, as unparse(e) + ".opts()".

Group
Code
Runs
Optional: run it with ur lint --rule rsc-built-replacement
Level
warning
Reads
Every file
Fix
None: a template parses only where its sort stands, and an insertion lays its element out as its neighbours are, so either can differ from the text; the author writes it

Why

A rewrite that gives a str gives text the command parses no further: unparse(e) + ".opts()" keeps e's source but not the brackets a lower operator needs once .opts() follows it, and unparse(d) + ", pub krate: CrateNum" writes the separator a neighbour already has. A template of the rewritten sort, (Expr)<e>.opts(), holds e as a phrase and adds the brackets its place needs; and where the rewrite keeps the node and adds an element beside it, a function tagged @insert with @place gives the element alone, laid out as its neighbours are.

The fix, as Ur suggests it: write a template, or @insert the element.

For agents

Give a rewrite's replacement as a template of its sort, with the nodes it keeps as holes, not as unparse joined with text; to add an element beside a node, write an @insert function with @place instead of rewriting the node into itself and the element.

Example

checks/Fields.rsc
module checks::Fields

@rewrite="a struct gains `krate`"
str krateField(StructField d) = unparse(d) + ", pub krate: CrateNum";
What Ur prints
$ ur lint --rule rsc-built-replacement
warning[rsc-built-replacement]: `krateField` keeps the node it rewrites and adds an element beside it as text; `@insert` with `@place` adds the element alone
  --> ./checks/Fields.rsc:4:33 `unparse(d)`
  = hint: write a template, or `@insert` the element
0 clone classes and 1 `rsc-built-replacement` finding in 1 file

Try it in the playground →