From 3f4554723985e127486079d3a2d1705c53926969 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 19 Oct 2023 08:20:35 -0700 Subject: [PATCH] Improve OperatorPrecedence docs https://github.com/google/error-prone/issues/4153 PiperOrigin-RevId: 574878098 --- docs/bugpattern/OperatorPrecedence.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/bugpattern/OperatorPrecedence.md b/docs/bugpattern/OperatorPrecedence.md index 9d04a4b8ec97..0a92d0cceeb4 100644 --- a/docs/bugpattern/OperatorPrecedence.md +++ b/docs/bugpattern/OperatorPrecedence.md @@ -8,5 +8,21 @@ The [Google Java Style Guide ยง4.7][style] states: [style]: https://google.github.io/styleguide/javaguide.html#s4.7-grouping-parentheses -Use grouping parentheses to disambiguate expressions that contain both `||` and -`&&`, or both shift and arithmetic operators. +Use grouping parentheses to disambiguate expressions that could be +misinterpreted. + +For example, consider this: + +```java +boolean d = (a && b) || c;", +boolean e = (a || b) ? c : d;", +int z = (x + y) << 2;", +``` + +Instead of this: + +```java +boolean r = a && b || c;", +boolean e = a || b ? c : d;", +int z = x + y << 2;", +```