Skip to content

Commit 431b73a

Browse files
committed
Update go/whitespace-contradicts-precedence to use isParenthesized
1 parent bad3b1e commit 431b73a

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

go/ql/src/InconsistentCode/WhitespaceContradictsPrecedence.ql

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,26 @@ predicate interestingNesting(BinaryExpr inner, BinaryExpr outer) {
7171
not inner instanceof HarmlessNestedExpr
7272
}
7373

74+
private int getNumberOfParentheses(Expr expr) {
75+
isParenthesized(expr, result)
76+
or
77+
not isParenthesized(expr, _) and result = 0
78+
}
79+
7480
/** Gets the number of whitespace characters around the operator `op` of `be`. */
7581
int getWhitespaceAroundOperator(BinaryExpr be, string op) {
76-
exists(Location loc, Location left, Location right |
77-
be.getLocation() = loc and
82+
exists(Location left, Location right, int leftParens, int rightParens |
7883
be.getLeftOperand().getLocation() = left and
7984
be.getRightOperand().getLocation() = right and
8085
left.getFile() = right.getFile() and
8186
left.getStartLine() = right.getStartLine() and
87+
leftParens = getNumberOfParentheses(be.getLeftOperand()) and
88+
rightParens = getNumberOfParentheses(be.getRightOperand())
89+
|
8290
op = be.getOperator() and
8391
result =
84-
(
85-
right.getStartColumn() - left.getEndColumn() - op.length() - 1 -
86-
(left.getStartColumn() - loc.getStartColumn()) -
87-
(loc.getEndColumn() - right.getEndColumn())
88-
) / 2
92+
(right.getStartColumn() - left.getEndColumn() - op.length() - leftParens - rightParens - 1) /
93+
2
8994
)
9095
}
9196

0 commit comments

Comments
 (0)