So what you want is a comma operator, in order to write multiple expressions in one expression position (the if
scruitinee).
Given the fairly universal derision of the C/C++ comma operator, I think it should be clear why you're getting pushback.
The equivalent in Rust for sequencing multiple expressions is to use a block, e.g.
if { rx.read_exact(&mut buf).await?; buf != info_hash } {
// code...
}
If this is what you want to write, then you should write it. Abusing if let
as sequencing is not what if let
should be used for. This is why the irrefutable patterns lint exists: the existence of an irrefutable if let
is a likely bug, because if let
should only be used for refutable patterns.
Rust has some opinionated lints (namely, everything under nonstandard_style
). If your code style disagrees with some lints, #[allow]
them (ideally with a comment explaining why).
This is ultimately a style question. If you ask to disable a lint, it's on you to show that the "false positive" rate of the lint is more problematic than the "true positive" rate is beneficial.
On the other hand, some other newer C-syntax-family languages also see the desire to have a setup statement in if
, and if (setup; test) {}
is somewhat common to see. This logical grouping of a setup and test is clearly desirable, so it's not a completely unreasonable ask.
Most Rust developers will say that the answer is to use a block and/or whitespace to indicate the grouping, though. I see no issue with the logical grouping implied by
// before...
// before...
rx.read_exact(&mut buf).await?;
if buf != info_hash {
// code...
// code...
}
// after...
// after...
that would be improved by writing
// before...
// before...
if do rx.read_exact(&mut buf).await?; buf != info_hash {
// code...
// code...
}
// after...
// after...
(imaginary the least bad syntax I could come up with for a sequencing operator which is not just a block.)
You'll be in good company, too; multiple notable Rust developers are known to write some interesting code that does things like
match match match {
... => ...
} {
... => ...
} {
... => ...
}
It's questionable to use a block in scruitinee position, but it has its merits for grouping in cases like this.