Finding Bugs in Code Programmatically Using Go
Write a static code analyzer to find bugs in code
This is a blog post based on a workshop I gave a year ago at GopherCon on static analysis in Go. The workshop covers much more than I could cover in this article, so you can check out the workshop here.
Motivation
According to Wikipedia, Static code analysis is the analysis of computer programs performed without executing them. You might be already familiar with tools doing static analysis:
- A linter for detecting styling errors and fixing them.
- Built-in IDE analyzers for detecting different issues such as infinite loops, unreachable code, and declaration redundancy.
- The go analyzer staticcheck and the open source platform SonarQube.
Why you should care? Code analyzers are a staple when working in teams with many developers. They are used in the CI/CD process to enforce that no bad practices, bugs, and anti-patterns are pushed to the main repo.
The Go language provides the go/analysis package for creating static code analyzers. Before talking about how to write those tools, we need to introduce a bit of theory.
AST
An abstract syntax tree (AST) is a way of representing the syntax of a programming language as a hierarchical tree-like structure. Let’s take a look at the following program:
This is the AST for the program.
[]ast.Decl contains all the top-level declarations in the file — imports and the main function. Inside ast.FuncDecl, which is the main function declaration, we have a ast.blockStmt. It is a list of the function statements- the actual code of the main function. We can keep going down the tree, but you can easily match the tree node to the actual code. (I removed some nodes from the tree to make it concise).
Go provides the go/ast package. The AST package contains the types used to represent syntax trees in Go, as in the tree above. Let’s take a look at ast.AssignStmt. The following code is from the go/ast package:
The expression a := 5 is an assignment, so it would be represented in AST as an ast.AssignStmt as follows:
Lhsis [a]TokPos[2] (the position of the “:” character)Tok[:=]Rhsis [5]
The list of all the types in the package is long, but it is compliant with what you would actually expect. You can also view the AST tree of a code using this site.
Writing the analyzer
In this part, we will write a static analyzer that detects if function arguments are being overwritten. This is considered a code smell, as it makes it harder to know what arguments were passed to a function during debugging. You can check the full code of the analyzer here.
Get Amit Davidson’s stories in your inbox
Join Medium for free to get updates from this writer.
First, let’s look at a typical structure of an analyzer. We create a directory named passes for each pass. Each pass is an inspection of our code. Every one of them lives in its own package, including its logic and tests. Then we create a main file that executes the passes the module has.
│── README.md
│── cmd
│ └── analyzerName
│ └── main.go
│── go.mod
│── go.sum
└── passes
└── passName
│── analyzer.go
│── analyzer_test.go
└── testdataThe go/analysis package defines an API for modular static analysis tools. To begin with, all passes should create an instance of analysis.Analyzer. It describes an analysis function: its name, documentation, flags, relationship to other analyzers, and of course, its logic.
We will start by adding an analyzer to analyzer.go. We will also add its name, and docs.
Below is the run function inside the analyzer.go file. It contains the actual logic of the analyzer. It receives an argument of type *analysis.Pass. *analysis.Pass provides information and operations for reporting diagnostics to the analyzer's Run function about the analyzed package.
Let’s look at lines 51–53 in the code below. We iterate over all the files and run the ast.Inspect function on each file. This function traverses the AST tree of a given file in depth-first order. The function is also given the visitor function which receives an AST node, and there is where the logic happens.
In lines 3–18, we validate the given node matches our conditions:
- Lines 3–15 make sure the node type is a function — regular function or a lambda function. Also, in lines 13–15, we exclude functions without a body, such as assembly routines.
- In lines 16–18, functions without parameters are excluded.
We iterate over each argument on lines 20-25 and construct a set from them. Lines 27–47 are where the magic happens.
We traverse the tree starting from the body of the function as the root node. Then the function body is searched for statements where variables are modified- AssignStmt (a = 5 for exmaple) and IncDecStmt (i++ for example).
Then we check the identifier participating in the statement, and if it’s one of the arguments in the set (arguments), then it’s considered an error, and we call the report function. This is an auxiliary function I wrote, and you can see its code below. All it does is wrap the call to pass.Report which actually notifies the user about an error.
Finally, we will add a new main file and a main function that uses the analyzer we just wrote. To run an analyzer you have to use single/multi checker for single/multiple passes.
Running our analyzer
Now that our analyzer is ready, we want to run it on an actual file. We can run it using the following commands:
go install path/to/analyzer
go vet -vettool=$(which analyzername) path/to/filesThe first command installs the analyzer as a binary, and the second run it using go vet. This way, you can easily integrate it into your IDE or CI/CD environment.
We can also test our code using the analysistest package. Using analysistest.Run, it is possible to run an analyzer on a package named testdata with test files and check that it reported all the expected diagnostics. Expectations are expressed using "// want ..." comments in the input code, such as the following:
Finally, we have to call analysistest.Run from a test file as below.
analysistest.Run(t, analysistest.TestData(), Analyzer)The first argument is *testing.T , the second is a path to the test files, easily supplied to us by analysistest.TestData() , and the third is the actual analyzer itself.










