-
Notifications
You must be signed in to change notification settings - Fork 888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected formatting of if-else inside closure #1791
Comments
This is an intended behaviour (see rust-lang/style-team#35 (comment)), although I agree that the former looks bettern in this case. |
I have an issue with a formatting change in the same vein, but I think it's even worse with @@ -94,12 +86,10 @@ fn zip_default_zip3(b: &mut test::Bencher)
let ys = black_box(ys);
let zs = black_box(zs);
- b.iter(|| {
- for ((&x, &y), &z) in xs.iter().zip(&ys).zip(&zs) {
- test::black_box(x);
- test::black_box(y);
- test::black_box(z);
- }
+ b.iter(|| for ((&x, &y), &z) in xs.iter().zip(&ys).zip(&zs) {
+ test::black_box(x);
+ test::black_box(y);
+ test::black_box(z);
})
} configuration: This happens with default configuration (haven't found something to switch it off) |
I've also found this to be a problem. Instead of this (rustfmt): s.spawn(move || for i in 0..COUNT {
tx.send(i).unwrap();
}); ...the following might look better: s.spawn(move || {
for i in 0..COUNT {
tx.send(i).unwrap();
}
}); IME, the only situation where putting something in between s.spawn(move || unsafe {
// ...
}); |
This definitely shouldn't happen with As an aside, I'm also wondering (outside the context of rustfmt) if we should have a lint for conditionals of the form |
Another example: fn transition(&mut self, len: usize, deadline: Option<Instant>) {
match *self {
State::TryOnce { closed_count } => if closed_count < len {
*self = State::Blocked;
} else {
*self = State::Disconnected;
},
State::SpinTry { closed_count } => if closed_count < len {
actor::current().reset();
*self = State::Promise { closed_count: 0 };
} else {
*self = State::Disconnected;
},
}
} I would prefer: fn transition(&mut self, len: usize, deadline: Option<Instant>) {
match *self {
State::TryOnce { closed_count } => {
if closed_count < len {
*self = State::Blocked;
} else {
*self = State::Disconnected;
}
}
State::SpinTry { closed_count } => {
if closed_count < len {
actor::current().reset();
*self = State::Promise { closed_count: 0 };
} else {
*self = State::Disconnected;
}
}
}
} |
And in some cases I think this style might be even sort of dangerous. When glancing over following example, the lack of indentation makes it easy to miss that block is in fact a loop. epoch::pin(|scope| loop {
let head = self.head.load(SeqCst, scope);
let next = head.deref().next.load(SeqCst, scope);
if next.is_null() {
if self.tail.load(SeqCst, scope).as_raw() == head.as_raw() {
return None;
}
} else {
if self.head
.compare_and_swap(head, next.with_tag(MULTI), SeqCst, scope)
.is_ok()
{
self.recv_count.fetch_add(1, SeqCst);
scope.defer_free(head);
return Some(ptr::read(&next.deref().value));
}
}
}) Now removing the keyword |
Sorry for spamming this thread, but I've just got bitten by this formatting for real. Here's a concrete scenario where I spent 15 minutes debugging the code and not realizing what went wrong: crossbeam::scope(|s| for i in 0..555 {
s.spawn(move || loop {
if let Ok(x) = rx.select() {
assert_ne!(i, x);
break;
}
if let Ok(()) = tx.select(i) {
break;
}
});
tx.send(!0).unwrap(); // I added this line, expecting it to be run once after the loop ends.
}); It took me way too long to realize that I wanted to do the following instead: crossbeam::scope(|s| {
for i in 0..555 {
s.spawn(move || loop {
if let Ok(x) = rx.select() {
assert_ne!(i, x);
break;
}
if let Ok(()) = tx.select(i) {
break;
}
});
}
tx.send(!0).unwrap(); // Now that's better!
}); Doh! :) I wanted to add that line to end of the lambda, but it actually ended up being in the loop. |
I commented on this over on fmt-rfcs at rust-lang/style-team#35 (comment) |
Is there any way to turn this off? I really can't stand it, it reminds me of braceless if statements. |
Fixed by #1877 |
This seems to be related to #1771. Not sure if it's intended behaviour. I find it much easier to understand what's going on when
if
andelse
are aligned:The text was updated successfully, but these errors were encountered: