For an operation represented as `a child b` with a surrounding parent operation (e.g., `(a child b) parent c` or `a parent (b child c)`): 1. When the child operator has higher precedence than the parent, parentheses *are not* required. 2. When the child operator has lower precedence than the parent, parentheses *are* required. 3. When the child and parent operators have the same precedence, the
(
expr: &ExprOrSource,
is_left: bool,
parent_strength: i32,
parent_associativity: Associativity,
)
| 1010 | // please do not make the code terser without being confident that it's easier |
| 1011 | // to understand. |
| 1012 | fn needs_parentheses( |
| 1013 | expr: &ExprOrSource, |
| 1014 | is_left: bool, |
| 1015 | parent_strength: i32, |
| 1016 | parent_associativity: Associativity, |
| 1017 | ) -> bool { |
| 1018 | let rule_3a = matches!(parent_associativity, Associativity::Both); |
| 1019 | let rule_3b_left = is_left && parent_associativity.left_associative(); |
| 1020 | let rule_3b_right = !is_left && parent_associativity.right_associative(); |
| 1021 | |
| 1022 | match expr.binding_strength().cmp(&parent_strength) { |
| 1023 | // Rule 1 |
| 1024 | Ordering::Greater => false, |
| 1025 | // Rule 2 |
| 1026 | Ordering::Less => true, |
| 1027 | // Rule 3 |
| 1028 | Ordering::Equal => !(rule_3a || rule_3b_left || rule_3b_right), |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | /// Associativity of an expression's operator. |
| 1033 | /// Note that there's no exponent symbol in SQL, so we don't seem to require a `Right` variant. |
no test coverage detected