I'm very much in favor of mandatory braces, but I do think there's a third path here. Rust has augmented if
and while
statements with if let Some(x) = x {}
and while let Some(x) = x {}
, taking advantage of reserved keywords to avoid the ambiguity and accidental assignments that are common in C code.
We could do something similar and augment for
loops to have an optional match
keyword:
for match x in xs {
X::Foo => { /* ... */ },
X::Bar => { /* ... */ },
}
This removes the "redundant" braces in the original code without introducing goto fail;
-type risk. It's probably a fair amount of work, and it's probably not worth the effort it takes to build, but I don't see any reason why we wouldn't welcome this if the work were already done.