Warn when whitespace is misleading when compared to operator precedence #4339
Labels
closed-not-planned
Closed as we don't intend to take action on the reported issue
type-enhancement
A request for a change that isn't a bug
Milestone
I have not been convinced by some of the puzzlers. They rely to some degree on sleight-of-hand. Consider:
main() {
print("A simple sum: 2 + 2 = " +
2 + 2);
}
In Java this prints "A simple sum: 2 + 2 = 22".
Why do I say 'sleight-of-hand'? Operator + groups from the left. In the expression a+b+c, the first + binds tighter, giving an evaluation equivalent to (a+b)+c. In the puzzler, there is more whitespace following the first operator, even though it binds tighter due to grouping from the left. The choice of whitespace is misleading in the same way that
print(1 + 2 *
3 + 4);
might mislead some to think it prints 21.
The 'Animal farm' puzzler does the same:
main() {
final String pig = "length: 10";
final String dog = "length: " + pig.length;
print("Animals are equal: "
+ pig == dog);
}
We might be able to do something about misleading whitespace. The editor could flag expressions where the whitespace suggests operator binding that is contrary to the actual binding.
Suggested implementation:
Attach to each binary operation node the whitespace to the left of the operator and to the right of the operator.
Warn when the whitespace in a subtree is looser than the whitespace on the binary operation node.
One whitespace string is looser than another if it has more newlines, or if it has more spaces when both have no newlines.
Although Dart no longer has + for strings, this is still a useful warning - see Issue #114 for a precedence issue inherited from C/C++/Java that has not been fixed.
The text was updated successfully, but these errors were encountered: