Skip to content

Commit f40fe71

Browse files
committed
Improve switched-compound-assignment-operator.ql
Tested on local database and seems to work as expected, but did not find any results for Variant Analysis.
1 parent 1a7ddfb commit f40fe71

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

codeql-custom-queries-java/not-tested-queries/switched-compound-assignment-operator.ql renamed to codeql-custom-queries-java/queries/likely-bugs/switched-compound-assignment-operator.ql

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
* Finds assignments with a plus or minus expression on the right side
33
* where it appears the intentation was to use a compound assignment
44
* instead, e.g.:
5-
* ```
5+
* ```java
66
* // Developer made a typo and wrote `=+` instead of `+=`
77
* for (int i = 0; i < x; i =+ 2) {
88
* ...
99
* }
1010
* ```
11+
*
12+
* @kind problem
1113
*/
1214

1315
import java
@@ -32,12 +34,15 @@ where
3234
and destLoc.getEndLine() = plusOrMinusLoc.getStartLine()
3335
// Check if there is no space on one side of `=`, e.g. `a =- 1`
3436
// Could cause false positives if there is no space between dest
35-
// and `=` (e.g. `a= -1`), but there is not way to detect that
37+
// and `=` (e.g. `a= -1`), but there is no way to detect that
3638
and if plusOrMinusExpr.isParenthesized() then (
37-
plusOrMinusLoc.getStartColumn() - destLoc.getEndColumn() < 5
39+
plusOrMinusLoc.getStartColumn() - destLoc.getEndColumn() <= 4
3840
) else (
39-
plusOrMinusLoc.getStartColumn() - destLoc.getEndColumn() < 4
41+
plusOrMinusLoc.getStartColumn() - destLoc.getEndColumn() <= 3
4042
)
43+
// Ignore false positives when destination is special construct such as local variable declaration
44+
// where apparently end column is behind start column of right operand
45+
and plusOrMinusLoc.getStartColumn() > destLoc.getEndColumn()
4146
and plusOrMinusLoc = plusOrMinusExpr.getLocation()
4247
// There is a space between plus/minus and its expr, e.g. `- 1`
4348
and exists (Expr rhs, Location rhsLoc | rhs = plusOrMinusExpr.getExpr() and rhsLoc = rhs.getLocation() |
@@ -48,4 +53,4 @@ where
4853
rhsLoc.getStartColumn() - plusOrMinusLoc.getStartColumn() > 1
4954
)
5055
)
51-
select assignExpr, plusOrMinusExpr
56+
select assignExpr, "Accidental switched compound operator; $@ is treated as separate unary expression", plusOrMinusExpr, "this"

0 commit comments

Comments
 (0)