Learn to write and run a simple CodeQL query using Visual Studio Code with the CodeQL extension.
For information about installing the CodeQL extension for Visual Studio code, see “Installing CodeQL for Visual Studio Code.”
The query we’re going to run performs a basic search of the code for if expressions that are redundant, in the sense that they have an empty then branch. For example, code such as:
Before you start writing queries for Rust code, you need a CodeQL database to run them against. The simplest way to do this is to download a database for a repository that uses Rust directly from GitHub.com.
In Visual Studio Code, click the QL icon in the left sidebar to display the CodeQL extension.
Click From GitHub or the GitHub logo at the top of the CodeQL extension to open an entry field.
Copy the URL for the repository into the field and press the keyboard Enter key. For example, https://github.com/rust-lang/rustlings.
Optionally, if the repository has more than one CodeQL database available, select rust to download the database created from the Rust code.
Information about the download progress for the database is shown in the bottom right corner of Visual Studio Code. When the download is complete, the database is shown with a check mark in the Databases section of the CodeQL extension (see screenshot below).
The CodeQL extension for Visual Studio Code adds several CodeQL: commands to the command palette including Quick Query, which you can use to run a query without any set up.
From the command palette in Visual Studio Code, select CodeQL: Quick Query.
After a moment, a new tab quick-query.ql is opened, ready for you to write a query for your currently selected CodeQL database (here a rust database). If you are prompted to reload your workspace as a multi-folder workspace to allow Quick queries, accept or create a new workspace using the starter workflow.
In the quick query tab, delete the content and paste in the following query.
Save the query in its default location (a temporary “Quick Queries” directory under the workspace for GitHub.vscode-codeql/quick-queries).
Right-click in the query tab and select CodeQL: Run Query on Selected Database. (Alternatively, run the command from the Command Palette.)
The query will take a few moments to return results. When the query completes, the results are displayed in a CodeQL Query Results view, next to the main editor view.
The query results are listed in two columns, corresponding to the expressions in the select clause of the query. The first column corresponds to the expression ifExpr and is linked to the location in the source code of the project where ifExpr occurs. The second column is the alert message.
If any matching code is found, click a link in the ifExpr column to open the file and highlight the matching if expression.
Note
If you want to move your experimental query somewhere more permanent, you need to move the whole Quick Queries directory. The directory is a CodeQL pack with a qlpack.yml file that defines the content as queries for Rust CodeQL databases. For more information about CodeQL packs, see “Managing CodeQL query packs and library packs.”
After the initial import statement, this simple query comprises three parts that serve similar purposes to the FROM, WHERE, and SELECT parts of an SQL query.
import rust |
Imports the standard CodeQL AST libraries for Rust. |
Every query begins with one or more import statements. |
from IfExpr ifExpr |
Defines the variables for the query. Declarations are of the form: <type> <variable name> |
We use: an IfExpr variable for if expressions. |
where ifExpr.getThen().(BlockExpr).getStmtList().getNumberOfStmtOrExpr() = 0 |
Defines a condition on the variables. |
ifExpr.getThen(): gets the then branch of the if expression. .(BlockExpr): requires that the then branch is a block expression ({ }). .getStmtList(): gets the list of things in the block. .getNumberOfStmtOrExpr() = 0: requires that there are no statements or expressions in the block. |
select ifExpr, "This 'if' expression is redundant." |
Defines what to report for each match. select statements for queries that are used to find instances of poor coding practice are always in the form: select <program element>, "<alert message>" |
Reports the resulting if expression with a string that explains the problem. |
Query writing is an inherently iterative process. You write a simple query and then, when you run it, you discover examples that you had not previously considered, or opportunities for improvement.
Browsing the results of our basic query shows that it could be improved. Among the results you are likely to find examples of if expressions with an else branch, where an empty then branch does serve a purpose. For example:
In this case, identifying the if expression with the empty then branch as redundant is a false positive. One solution to this is to modify the query to select if expressions where both the then and else branches are missing.
To exclude if expressions that have an else branch:
Add the following to the where clause:
The where clause is now:
Re-run the query.
There are now fewer results because if expressions with an else branch are no longer included.