Skip to content

Commit db3d177

Browse files
committed
Rust: Port the code examples to Rust / CodeQL for Rust.
1 parent 1211dc8 commit db3d177

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

docs/codeql/codeql-language-guides/basic-query-for-rust-code.rst

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ Running a quick query
3131
3232
import rust
3333
34-
from IfStmt ifStmt
35-
where ifStmt.getThen().(BraceStmt).getNumberOfElements() = 0
36-
select ifStmt, "This 'if' statement is redundant."
34+
from IfExpr ifExpr
35+
where ifExpr.getThen().(BlockExpr).getStmtList().getNumberOfStmtOrExpr() = 0
36+
select ifExpr, "This 'if' statement is redundant."
3737
3838
.. include:: ../reusables/vs-code-basic-instructions/run-quick-query-2.rst
3939

4040
.. image:: ../images/codeql-for-visual-studio-code/basic-rust-query-results-1.png
4141
:align: center
4242

43-
If any matching code is found, click a link in the ``ifStmt`` column to open the file and highlight the matching ``if`` statement.
43+
If any matching code is found, click a link in the ``ifExpr`` column to open the file and highlight the matching ``if`` expression.
4444

4545
.. image:: ../images/codeql-for-visual-studio-code/basic-rust-query-results-2.png
4646
:align: center
@@ -52,24 +52,25 @@ About the query structure
5252

5353
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.
5454

55-
+------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------+
56-
| Query part | Purpose | Details |
57-
+==================================================================+===================================================================================================================+=================================================================================================+
58-
| ``import rust`` | Imports the standard CodeQL AST libraries for Rust. | Every query begins with one or more ``import`` statements. |
59-
+------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------+
60-
| ``from IfStmt ifStmt`` | Defines the variables for the query. | We use: an ``IfStmt`` variable for ``if`` statements. |
61-
| | Declarations are of the form: | |
62-
| | ``<type> <variable name>`` | |
63-
+------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------+
64-
| ``where ifStmt.getThen().(BraceStmt).getNumberOfElements() = 0`` | Defines a condition on the variables. | ``ifStmt.getThen()``: gets the ``then`` branch of the ``if`` expression. |
65-
| | | ``.(BraceStmt)``: requires that the ``then`` branch is a brace statement (``{ }``). |
66-
| | | ``.getNumberOfElements() = 0``: requires that the brace statement contains no child statements. |
67-
+------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------+
68-
| ``select ifStmt, "This 'if' statement is redundant."`` | Defines what to report for each match. | Reports the resulting ``if`` statement with a string that explains the problem. |
69-
| | | |
70-
| | ``select`` statements for queries that are used to find instances of poor coding practice are always in the form: | |
71-
| | ``select <program element>, "<alert message>"`` | |
72-
+------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------+
55+
+----------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
56+
| Query part | Purpose | Details |
57+
+==================================================================================+===================================================================================================================+======================================================================================================+
58+
| ``import rust`` | Imports the standard CodeQL AST libraries for Rust. | Every query begins with one or more ``import`` statements. |
59+
+----------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
60+
| ``from IfExpr ifExpr`` | Defines the variables for the query. | We use: an ``IfExpr`` variable for ``if`` expressions. |
61+
| | Declarations are of the form: | |
62+
| | ``<type> <variable name>`` | |
63+
+----------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
64+
| ``where ifExpr.getThen().(BlockExpr).getStmtList().getNumberOfStmtOrExpr() = 0`` | Defines a condition on the variables. | ``ifExpr.getThen()``: gets the ``then`` branch of the ``if`` expression. |
65+
| | | ``.(BlockExpr)``: requires that the ``then`` branch is a block expression (``{ }``). |
66+
| | | ``.getStmtList()``: gets the list of things in the block. |
67+
| | | ``.getNumberOfStmtOrExpr() = 0``: requires that there are no statements or expressions in the block. |
68+
+----------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
69+
| ``select ifExpr, "This 'if' expression is redundant."`` | Defines what to report for each match. | Reports the resulting ``if`` expression with a string that explains the problem. |
70+
| | | |
71+
| | ``select`` statements for queries that are used to find instances of poor coding practice are always in the form: | |
72+
| | ``select <program element>, "<alert message>"`` | |
73+
+----------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
7374

7475
Extend the query
7576
----------------
@@ -79,7 +80,7 @@ Query writing is an inherently iterative process. You write a simple query and t
7980
Remove false positive results
8081
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8182

82-
Browsing the results of our basic query shows that it could be improved. Among the results you are likely to find examples of ``if`` statements with an ``else`` branch, where an empty ``then`` branch does serve a purpose. For example:
83+
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:
8384

8485
.. code-block:: rust
8586
@@ -89,23 +90,23 @@ Browsing the results of our basic query shows that it could be improved. Among t
8990
handleError("unrecognized option")
9091
}
9192
92-
In this case, identifying the ``if`` statement with the empty ``then`` branch as redundant is a false positive. One solution to this is to modify the query to select ``if`` statements where both the ``then`` and ``else`` branches are missing.
93+
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.
9394

94-
To exclude ``if`` statements that have an ``else`` branch:
95+
To exclude ``if`` expressions that have an ``else`` branch:
9596

9697
#. Add the following to the where clause:
9798

9899
.. code-block:: ql
99100
100-
and not exists(ifStmt.getElse())
101+
and not exists(ifExpr.getElse())
101102
102103
The ``where`` clause is now:
103104

104105
.. code-block:: ql
105106
106107
where
107-
ifStmt.getThen().(BraceStmt).getNumberOfElements() = 0 and
108-
not exists(ifStmt.getElse())
108+
ifExpr.getThen().(BlockExpr).getStmtList().getNumberOfStmtOrExpr() = 0 and
109+
not exists(ifExpr.getElse())
109110
110111
#. Re-run the query.
111112

@@ -123,8 +124,8 @@ Further reading
123124

124125
.. |language-code| replace:: ``rust``
125126

126-
.. |example-url| replace:: https://github.com/alamofire/alamofire
127+
.. |example-url| replace:: https://github.com/rust-lang/rustlings
127128

128129
.. |image-quick-query| image:: ../images/codeql-for-visual-studio-code/quick-query-tab-rust.png
129130

130-
.. |result-col-1| replace:: The first column corresponds to the expression ``ifStmt`` and is linked to the location in the source code of the project where ``ifStmt`` occurs.
131+
.. |result-col-1| replace:: The first column corresponds to the expression ``ifExpr`` and is linked to the location in the source code of the project where ``ifExpr`` occurs.

0 commit comments

Comments
 (0)