2
2
* Finds assignments with a plus or minus expression on the right side
3
3
* where it appears the intentation was to use a compound assignment
4
4
* instead, e.g.:
5
- * ```
5
+ * ```java
6
6
* // Developer made a typo and wrote `=+` instead of `+=`
7
7
* for (int i = 0; i < x; i =+ 2) {
8
8
* ...
9
9
* }
10
10
* ```
11
+ *
12
+ * @kind problem
11
13
*/
12
14
13
15
import java
@@ -32,12 +34,15 @@ where
32
34
and destLoc .getEndLine ( ) = plusOrMinusLoc .getStartLine ( )
33
35
// Check if there is no space on one side of `=`, e.g. `a =- 1`
34
36
// 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
36
38
and if plusOrMinusExpr .isParenthesized ( ) then (
37
- plusOrMinusLoc .getStartColumn ( ) - destLoc .getEndColumn ( ) < 5
39
+ plusOrMinusLoc .getStartColumn ( ) - destLoc .getEndColumn ( ) <= 4
38
40
) else (
39
- plusOrMinusLoc .getStartColumn ( ) - destLoc .getEndColumn ( ) < 4
41
+ plusOrMinusLoc .getStartColumn ( ) - destLoc .getEndColumn ( ) <= 3
40
42
)
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 ( )
41
46
and plusOrMinusLoc = plusOrMinusExpr .getLocation ( )
42
47
// There is a space between plus/minus and its expr, e.g. `- 1`
43
48
and exists ( Expr rhs , Location rhsLoc | rhs = plusOrMinusExpr .getExpr ( ) and rhsLoc = rhs .getLocation ( ) |
48
53
rhsLoc .getStartColumn ( ) - plusOrMinusLoc .getStartColumn ( ) > 1
49
54
)
50
55
)
51
- select assignExpr , plusOrMinusExpr
56
+ select assignExpr , "Accidental switched compound operator; $@ is treated as separate unary expression" , plusOrMinusExpr , "this"
0 commit comments