94 points · by lima · 4y ago · 19 comments
Saying "it all has to be in the typesystem" can _also_ become a cludge (see languages that have built-in null handling vs languages that just require you to use Option and the like)
[1] https://clang.llvm.org/extra/clang-tidy/checks/list.html [2] https://spotbugs.github.io/ [3] http://mypy-lang.org/ [4] https://github.com/rust-lang/rust-clippy
The closest I can think of is Java, where you can optionally do something during compilation with e.g. an annotation processor, which has a number of rather significant reliability and performance benefits. But that's an optional feature provided by an absurdly mature set of tools, definitely not a requirement - there are tons of java analysis tools that don't plug into javac.
If anything, this one serves as a bad example of Go's simplicity being a benefit. It's using the SSA analysis pass to do much of what a compiler does, because the compiler doesn't give you enough data to do it otherwise. It's an extremely complex workaround with only one implementation which is also built by the language developers, though it does plug into a decent framework (x/tools/go/analysis).
To clear one misconception: some of the comments seem to be taking this as some kind of official Go tool, or using it a data point regarding the language itself. I don’t think this is justified; the analyzer is only used within the gVisor codebase, and is not an outgrowth of Go itself.
On the points raised: I’m not a languages or compiler person; I think it’s pretty neat that I can write a decently powerful static analyzer using the same standardized packages used by vet and other tools. To me, this is the strength of Go: the core is simple. I agree with the idea that it would be cool if Go had some more advanced features in this area, but I also understand why these changes are made slowly, to ensure there’s experience and evidence. In the meantime, we’re free to write tooling that meets our specific needs (and lets one scratch a fun itch).
One last thing: the README is incomplete and out of date (globals are supported, for example), but it was never really for public consumption.
That already ensures safe access at dev / compile-time. One explicitly states on declaration that the variable is accessed atomically by using the wrapper type. A linter could then simply check that sync/atomic isn't used directly anywhere.
// +checkatomic
type atomicint int64
type mystruct struct {
count atomicint
}Also, it doesn’t require you to import a package, it’s completely orthogonal to the consumer of the code. They can opt in if they want, others aren’t forced to import anything.