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
This opens the query console. (For information about using this, see `Using the query console <https://lgtm.com/help/lgtm/using-query-console>`__.)
44
-
45
-
.. pull-quote::
46
-
47
-
Note
48
-
49
-
Alternatively, you can go straight to the query console by clicking **Query console** (at the top of any page), selecting **Java** from the **Language** drop-down list, then choosing one or more projects to query from those displayed in the **Project** drop-down list.
50
-
51
-
#. Copy the following query into the text box in the query console:
41
+
#. In the quick query tab, delete ``select ""`` and paste the following query beneath the import statement ``import java``.
52
42
53
43
.. code-block:: ql
54
44
55
-
import java
56
-
57
-
from MethodAccess ma
58
-
where
59
-
ma.getMethod().hasName("equals") and
60
-
ma.getArgument(0).(StringLiteral).getValue() = ""
61
-
select ma, "This comparison to empty string is inefficient, use isEmpty() instead."
45
+
from MethodAccess ma
46
+
where
47
+
ma.getMethod().hasName("equals") and
48
+
ma.getArgument(0).(StringLiteral).getValue() = ""
49
+
select ma, "This comparison to empty string is inefficient, use isEmpty() instead."
62
50
63
51
Note that CodeQL treats Java and Kotlin as part of the same language, so even though this query starts with ``import java``, it will work for both Java and Kotlin code.
64
52
65
-
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.
66
-
67
-
#. Click **Run**.
68
-
69
-
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:
Your query is always run against the most recently analyzed commit to the selected project.
58
+
If any matching code is found, click a link in the ``ma`` column to view the ``.equals`` expression in the code viewer.
79
59
80
-
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 ``ma`` and is linked to the location in the source code of the project where ``ma`` occurs. The second column is the alert message.
An ellipsis (…) at the bottom of the table indicates that the entire list is not displayed—click it to show more results.
89
-
90
-
#. If any matching code is found, click a link in the ``ma`` column to view the ``.equals`` expression in the code viewer.
91
-
92
-
The matching ``.equals`` 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.
.. |result-col-1| replace:: The first column corresponds to the expression ``ma`` and is linked to the location in the source code of the project where ``ma`` occurs.
@@ -12,62 +14,33 @@ In JavaScript, any expression can be turned into an expression statement. While
12
14
13
15
The query you will run finds instances of this problem. The query searches for expressions ``e`` that are pure—that is, their evaluation does not lead to any side effects—but appear as an expression statement.
14
16
15
-
Running the query
16
-
-----------------
17
-
18
-
#. 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>`__.
19
-
20
-
#. Click the project in the search results.
21
-
22
-
#. Click **Query this project**.
23
-
24
-
This opens the query console. (For information about using this, see `Using the query console <https://lgtm.com/help/lgtm/using-query-console>`__.)
Alternatively, you can go straight to the query console by clicking **Query console** (at the top of any page), selecting **JavaScript** from the **Language** drop-down list, then choosing one or more projects to query from those displayed in the **Project** drop-down list.
#. Copy the following query into the text box in the query console:
24
+
#. In the quick query tab, delete ``select ""`` and paste the following query beneath the import statement ``import javascript``.
33
25
34
26
.. code-block:: ql
35
27
36
-
import javascript
37
-
38
28
from Expr e
39
29
where e.isPure() and
40
30
e.getParent() instanceof ExprStmt
41
31
select e, "This expression has no effect."
42
32
43
-
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.
44
-
45
-
#. Click **Run**.
46
-
47
-
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:
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 ``e`` and is linked to the location in the source code of the project where ``e`` occurs. The second column is the alert message.
38
+
If any matching code is found, click one of the links in the ``e`` column to view the expression in the code viewer.
An ellipsis (…) at the bottom of the table indicates that the entire list is not displayed—click it to show more results.
67
-
68
-
#. If any matching code is found, click one of the links in the ``e`` column to view the expression in the code viewer.
69
-
70
-
The matching statement 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.
@@ -119,11 +92,14 @@ To remove directives from the results:
119
92
e.getParent() instanceof ExprStmt and
120
93
not e.getParent() instanceof Directive
121
94
122
-
#. Click **Run**.
95
+
#. Re-run the query.
123
96
124
97
There are now fewer results as ``use strict`` directives are no longer reported.
125
98
126
-
The improved query finds several results on the example project including `this result <https://lgtm.com/projects/g/ajaxorg/ace/rev/ad50673d7137c09d1a5a6f0ef83633a149f9e3d1/files/lib/ace/keyboard/vim.js#L320>`__:
99
+
The improved query finds several results on the example project including the result below:
.. |result-col-1| replace:: The first column corresponds to the expression ``e`` and is linked to the location in the source code of the project where ``e`` occurs.
Alternatively, you can go straight to the query console by clicking **Query console** (at the top of any page), selecting **Python** from the **Language** drop-down list, then choosing one or more projects to query from those displayed in the **Project** drop-down list.
33
-
34
-
#. Copy the following query into the text box in the query console:
24
+
#. In the quick query tab, delete ``select ""`` and paste the following query beneath the import statement ``import python``.
35
25
36
26
.. code-block:: ql
37
27
38
-
import python
39
-
40
28
from If ifstmt, Stmt pass
41
29
where pass = ifstmt.getStmt(0) and
42
30
pass instanceof Pass
43
31
select ifstmt, "This 'if' statement is redundant."
44
32
45
-
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.
46
-
47
-
#. Click **Run**.
48
-
49
-
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:
Your query is always run against the most recently analyzed commit to the selected project.
38
+
If any matching code is found, click a link in the ``ifstmt`` column to view the ``if`` statement in the code viewer.
59
39
60
-
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 ``ifstmt`` and is linked to the location in the source code of the project where ``ifstmt`` occurs. The second column is the alert message.
An ellipsis (…) at the bottom of the table indicates that the entire list is not displayed—click it to show more results.
69
-
70
-
#. If any matching code is found, click a link in the ``ifstmt`` column to view the ``if`` statement in the code viewer.
71
-
72
-
The matching ``if`` statement 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.
.. |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.
0 commit comments