|
| 1 | +.. _basic-query-for-ruby-code: |
| 2 | + |
| 3 | +Basic query for Ruby code |
| 4 | +========================= |
| 5 | + |
| 6 | +Learn to write and run a simple CodeQL query. |
| 7 | + |
| 8 | +About the query |
| 9 | +--------------- |
| 10 | + |
| 11 | +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: |
| 12 | + |
| 13 | +.. code-block:: ruby |
| 14 | +
|
| 15 | + if error |
| 16 | + # Handle the error |
| 17 | +
|
| 18 | +Running the query |
| 19 | +----------------- |
| 20 | + |
| 21 | +#. In the main search box on LGTM.com, search for the project you want to query. For tips, see `Searching <https://lgtm.com/help/lgtm/searching>`__. |
| 22 | + |
| 23 | +#. Click the project in the search results. |
| 24 | + |
| 25 | +#. Click **Query this project**. |
| 26 | + |
| 27 | + This opens the query console. (For information about using this, see `Using the query console <https://lgtm.com/help/lgtm/using-query-console>`__.) |
| 28 | + |
| 29 | + .. pull-quote:: |
| 30 | + |
| 31 | + Note |
| 32 | + |
| 33 | + Alternatively, you can go straight to the query console by clicking **Query console** (at the top of any page), selecting **Ruby** from the **Language** drop-down list, then choosing one or more projects to query from those displayed in the **Project** drop-down list. |
| 34 | + |
| 35 | +#. Copy the following query into the text box in the query console: |
| 36 | + |
| 37 | + .. code-block:: ql |
| 38 | +
|
| 39 | + import ruby |
| 40 | +
|
| 41 | + from IfExpr ifexpr |
| 42 | + where |
| 43 | + not exists(ifexpr.getThen()) |
| 44 | + select ifexpr, "This 'if' expression is redundant." |
| 45 | +
|
| 46 | + LGTM checks whether your query compiles and, if all is well, the **Run** button changes to green to indicate that you can go ahead and run the query. |
| 47 | + |
| 48 | +#. Click **Run**. |
| 49 | + |
| 50 | + The name of the project you are querying, and the ID of the most recently analyzed commit to the project, are listed below the query box. To the right of this is an icon that indicates the progress of the query operation: |
| 51 | + |
| 52 | + .. image:: ../images/query-progress.png |
| 53 | + :align: center |
| 54 | + |
| 55 | + .. pull-quote:: |
| 56 | + |
| 57 | + Note |
| 58 | + |
| 59 | + Your query is always run against the most recently analyzed commit to the selected project. |
| 60 | + |
| 61 | + The query will take a few moments to return results. When the query completes, the results are displayed below the project name. The query results are listed in two columns, corresponding to the two 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. |
| 62 | + |
| 63 | + ➤ `Example query results <https://lgtm.com/query/1214010107827821393/>`__ |
| 64 | + |
| 65 | + .. pull-quote:: |
| 66 | + |
| 67 | + Note |
| 68 | + |
| 69 | + An ellipsis (…) at the bottom of the table indicates that the entire list is not displayed—click it to show more results. |
| 70 | + |
| 71 | +#. If any matching code is found, click a link in the ``ifexpr`` column to view the ``if`` statement in the code viewer. |
| 72 | + |
| 73 | + The matching ``if`` expression is highlighted with a yellow background in the code viewer. If any code in the file also matches a query from the standard query library for that language, you will see a red alert message at the appropriate point within the code. |
| 74 | + |
| 75 | +About the query structure |
| 76 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | ++---------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------+ |
| 81 | +| Query part | Purpose | Details | |
| 82 | ++===============================================================+===================================================================================================================+========================================================================================================================+ |
| 83 | +| ``import ruby`` | Imports the standard CodeQL libraries for Ruby. | Every query begins with one or more ``import`` statements. | |
| 84 | ++---------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------+ |
| 85 | +| ``from IfExpr ifexpr`` | Defines the variables for the query. | We use: an ``IfExpr`` variable for ``if`` expressions. | |
| 86 | +| | Declarations are of the form: | | |
| 87 | +| | ``<type> <variable name>`` | | |
| 88 | ++---------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------+ |
| 89 | +| ``where not exists(ifexpr.getThen())`` | Defines a condition on the variables. | ``ifexpr.getThen()``: gets the ``then`` branch of the ``if`` expression. | |
| 90 | +| | | | |
| 91 | +| | | ``exists(...)``: requires that there is a matching element, in this case a ``then`` branch. | |
| 92 | ++---------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------+ |
| 93 | +| ``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. | |
| 94 | +| | | | |
| 95 | +| | ``select`` statements for queries that are used to find instances of poor coding practice are always in the form: | | |
| 96 | +| | ``select <program element>, "<alert message>"`` | | |
| 97 | ++---------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------+ |
| 98 | + |
| 99 | +Extend the query |
| 100 | +---------------- |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +Remove false positive results |
| 105 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 106 | + |
| 107 | +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: |
| 108 | + |
| 109 | +.. code-block:: ruby |
| 110 | +
|
| 111 | + if option == "-verbose" |
| 112 | + # nothing to do - handled earlier |
| 113 | + else |
| 114 | + error "unrecognized option" |
| 115 | +
|
| 116 | +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. |
| 117 | + |
| 118 | +To exclude ``if`` statements that have an ``else`` branch: |
| 119 | + |
| 120 | +#. Add the following to the where clause: |
| 121 | + |
| 122 | + .. code-block:: ql |
| 123 | +
|
| 124 | + and not exists(ifstmt.getElse()) |
| 125 | +
|
| 126 | + The ``where`` clause is now: |
| 127 | + |
| 128 | + .. code-block:: ql |
| 129 | +
|
| 130 | + where |
| 131 | + not exists(ifexpr.getThen()) and |
| 132 | + not exists(ifexpr.getElse()) |
| 133 | +
|
| 134 | +#. Click **Run**. |
| 135 | + |
| 136 | + There are now fewer results because ``if`` expressions with an ``else`` branch are no longer included. |
| 137 | + |
| 138 | +➤ `See this in the query console <https://lgtm.com/query/6233102733683510530/>`__ |
| 139 | + |
| 140 | +Further reading |
| 141 | +--------------- |
| 142 | + |
| 143 | +.. include:: ../reusables/ruby-further-reading.rst |
| 144 | +.. include:: ../reusables/codeql-ref-tools-further-reading.rst |
0 commit comments