I was writing Rust lint plugin when I found that Rust uses different types to represent the then (Block
) and else (Expr
) parts of an if
statement in both libsyntax::ast::ExprKind
and librustc::hir::Expr_
.
I need them both to have common types.
Should I just use an explicit match over hir::Expr_::ExprBlock
, or else part could be something else, and I need to make more smart comparison?
From the source:
// ...
pub enum Expr_ {
// ...
/// An `if` block, with an optional else block
///
/// `if expr { block } else { expr }`
ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
// ...
}
// ...
This is so we can distinguish
if x {
foo();
} else if y {
bar();
}
from
if x {
foo();
} else { // note the block
if y {
bar();
}
}
The first has an ExprIf
in the node of the else-Expr
whereas the second has an ExprBlock
containing a single ExprIf
-expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With