You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
| ``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. |
| ``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: ||
| ``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. |
| ``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: ||
@@ -79,7 +80,7 @@ Query writing is an inherently iterative process. You write a simple query and t
79
80
Remove false positive results
80
81
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81
82
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:
83
84
84
85
.. code-block:: rust
85
86
@@ -89,23 +90,23 @@ Browsing the results of our basic query shows that it could be improved. Among t
89
90
handleError("unrecognized option")
90
91
}
91
92
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.
93
94
94
-
To exclude ``if`` statements that have an ``else`` branch:
95
+
To exclude ``if`` expressions that have an ``else`` branch:
95
96
96
97
#. Add the following to the where clause:
97
98
98
99
.. code-block:: ql
99
100
100
-
and not exists(ifStmt.getElse())
101
+
and not exists(ifExpr.getElse())
101
102
102
103
The ``where`` clause is now:
103
104
104
105
.. code-block:: ql
105
106
106
107
where
107
-
ifStmt.getThen().(BraceStmt).getNumberOfElements() = 0 and
108
-
not exists(ifStmt.getElse())
108
+
ifExpr.getThen().(BlockExpr).getStmtList().getNumberOfStmtOrExpr() = 0 and
.. |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