Skip to main content

Add parentheses in nested expression

ZPA rule for PL/SQL and Oracle SQL · AddParenthesesInNestedExpression

AND binds tighter than OR: a AND b OR c means (a AND b) OR c. Read without that rule in mind, the condition looks like a AND (b OR c), and a query or a decision that seems to require a silently accepts every row where c holds.

This rule reports an AND written directly as an operand of an OR, without parentheses. Parentheses keep the meaning and make it visible; when the other reading was meant, they fix the condition.

Examples​

Noncompliant Code Example
select c.code
from crate c
where c.status = 'OPEN'
and c.weight > 100 -- Noncompliant
or c.fragile = 'Y';
Compliant Solution
select c.code
from crate c
where c.status = 'OPEN'
and (c.weight > 100 or c.fragile = 'Y');