Skip to content

Commit 17cec52

Browse files
authored
Merge pull request #8385 from erik-krogh/orderByConst
QL: add query detecting ordering by a constant
2 parents fbe8f75 + 9c759a9 commit 17cec52

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Order by const
3+
* @description Ordering by a constant has no effect, and is an indication is a misplaced order by clause.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/order-by-const
7+
* @tags correctness
8+
* maintainability
9+
* @precision medium
10+
*/
11+
12+
import ql
13+
14+
Expr getAnOrderBy(AstNode parent, string kind) {
15+
result = parent.(FullAggregate).getOrderBy(_) and
16+
kind = parent.(FullAggregate).getKind() + " aggregate"
17+
or
18+
result = parent.(Select).getOrderBy(_) and kind = "select"
19+
or
20+
result = parent.(ExprAggregate).getOrderBy(_) and
21+
kind = parent.(ExprAggregate).getKind() + " aggregate"
22+
}
23+
24+
from Expr orderBy, AstNode parent, string kind
25+
where
26+
orderBy = getAnOrderBy(parent, kind) and
27+
orderBy instanceof Literal
28+
select orderBy, "Ordering $@ by a constant.", parent, kind
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
string foo() {
2+
result = concat(string x | x = [0 .. 10].toString() | x order by x desc, ", ") // BAD
3+
or
4+
result = concat(string x | x = [0 .. 10].toString() | x, ", " order by x desc) // GOOD
5+
}
6+
7+
class Even extends int {
8+
bindingset[this]
9+
Even() { this % 2 = 0 }
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| Foo.qll:2:76:2:79 | String | Ordering $@ by a constant. | Foo.qll:2:12:2:80 | FullAggregate[concat] | concat aggregate |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/bugs/OrderByConst.ql

0 commit comments

Comments
 (0)