rsc-memo-candidate
a function of no node that walks the whole file, asked at each node it is called at.
- Group
- Code
- Runs
- Optional: run it with
ur lint --rule rsc-memo-candidate - Level
- warning
- Reads
- Every file
- Fix
- None: a project rule that asks the function about several files would keep the first file's answer, so whether to memoize it is the author's to decide
Why
A function whose formals are no nodes and that reads root() gives the same answer for the same arguments anywhere in a file, yet a function of a node that calls it asks again at each node, walking the file each time. @memo makes it a goal keyed by its arguments, worked out once while the file's evaluator lives. A project rule's evaluator lives for the project, though, so where a project rule asks the function about several files, the first file's answer would stand for the others.
The fix, as Ur suggests it: tag it @memo, unless a project rule asks it.
For agents
Tag @memo a function of no node that walks the whole file and is asked at every node, unless a project rule asks it about several files.
Example
checks/Borrows.rscmodule checks::Borrows
bool mutBorrows(str m) = any(/Function g := root(), g.name == m);
bool mutCall(MethodCall c) = mutBorrows(unparse(c.method));
$ ur lint --rule rsc-memo-candidate
warning[rsc-memo-candidate]: `mutBorrows` takes no node and walks the whole file, and `mutCall` asks it at each node it is called at; `@memo` works it out once a file
--> ./checks/Borrows.rsc:3:1 `bool mutBorrows(str m) = any(/Function g := root(), g.name =...`
= hint: tag it `@memo`, unless a project rule asks it
0 clone classes and 1 `rsc-memo-candidate` finding in 1 file