Remove redundant operation in derive[PartialEq]

This commit is contained in:
varkor 2018-04-11 16:06:56 +01:00
parent 3238e0d098
commit 1ce06d0932
1 changed files with 58 additions and 28 deletions

View File

@ -27,7 +27,7 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
// structures are equal if all fields are equal, and non equal, if
// any fields are not equal or if the enum variants are different
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
cs_fold(true, // use foldl
cs_fold1(true, // use foldl
|cx, span, subexpr, self_f, other_fs| {
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
@ -38,14 +38,29 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
cx.expr_binary(span, BinOpKind::And, subexpr, eq)
},
cx.expr_bool(span, true),
|cx, args| {
match args {
Some((span, self_f, other_fs)) => {
// Special-case the base case to generate cleaner code.
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
_ => {
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
}
};
cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone())
}
None => cx.expr_bool(span, true),
}
},
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
cx,
span,
substr)
}
fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
cs_fold(true, // use foldl
cs_fold1(true, // use foldl
|cx, span, subexpr, self_f, other_fs| {
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
@ -56,7 +71,22 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
cx.expr_binary(span, BinOpKind::Or, subexpr, eq)
},
cx.expr_bool(span, false),
|cx, args| {
match args {
Some((span, self_f, other_fs)) => {
// Special-case the base case to generate cleaner code.
let other_f = match (other_fs.len(), other_fs.get(0)) {
(1, Some(o_f)) => o_f,
_ => {
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
}
};
cx.expr_binary(span, BinOpKind::Ne, self_f, other_f.clone())
}
None => cx.expr_bool(span, false),
}
},
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
cx,
span,