AST normalization experiment
Go AST normalization experiment.
THIS IS NOT A PROPER LIBRARY (yet?).
DO NOT USE.
It will probably be completely re-written before it becomes usable.
Before | After |
---|---|
|
|
toRemove+1
.Before | After |
---|---|
|
|
gogrep
and run itPotential workflow for code searching:
grepfunc
against normalized stdlibBasically, instead of stdlib you can use any kind of Go corpus.
Another code search related tasks that can be simplified by astnorm
are code similarity
evaluation and code duplication detection of any kind.
Suppose we have badcode.go
file:
package badpkg
func NotEqual(x1, x2 int) bool {
return (x1) != x1
}
There is an obvious mistake there, x1
used twice, but because of extra parenthesis, linters may not detect this issue:
$ staticcheck badcode.go
# No output
Let's normalize the input first and then run staticcheck
:
go-normalize badcode.go > normalized_badcode.go
staticcheck normalized_badcode.go
normalized_badcode.go:4:9: identical expressions on the left and right side of the '!=' operator (SA4000)
And we get the warning we deserve!
No changes into staticcheck
or any other linter are required.
See also: demo script.