about summary refs log tree commit diff
path: root/src/libsyntax_ext/deriving
diff options
context:
space:
mode:
authorUlrik Sverdrup <bluss@users.noreply.github.com>2016-02-29 22:15:51 +0100
committerUlrik Sverdrup <bluss@users.noreply.github.com>2016-02-29 22:31:39 +0100
commit57e0a7e5d8872c8fcea47fc20239b8921bda2576 (patch)
tree422eff1c4d2d8e46f6eeb185acaf0caedd825a15 /src/libsyntax_ext/deriving
parent190af51f303e21e22ea0a4d7dffeb09805d19010 (diff)
downloadrust-57e0a7e5d8872c8fcea47fc20239b8921bda2576.tar.gz
rust-57e0a7e5d8872c8fcea47fc20239b8921bda2576.zip
derive: Skip PartialEq::ne for any zero-field enum or struct
Also detect unit structs and enums with zero field struct variants.
Diffstat (limited to 'src/libsyntax_ext/deriving')
-rw-r--r--src/libsyntax_ext/deriving/cmp/partial_eq.rs32
1 files changed, 15 insertions, 17 deletions
diff --git a/src/libsyntax_ext/deriving/cmp/partial_eq.rs b/src/libsyntax_ext/deriving/cmp/partial_eq.rs
index dac25112112..24444c3c39b 100644
--- a/src/libsyntax_ext/deriving/cmp/partial_eq.rs
+++ b/src/libsyntax_ext/deriving/cmp/partial_eq.rs
@@ -11,30 +11,26 @@
 use deriving::generic::*;
 use deriving::generic::ty::*;
 
-use syntax::ast::{MetaItem, Expr, BinOpKind, ItemKind, VariantData};
+use syntax::ast::{MetaItem, Expr, BinOpKind, ItemKind};
 use syntax::codemap::Span;
 use syntax::ext::base::{ExtCtxt, Annotatable};
 use syntax::ext::build::AstBuilder;
 use syntax::parse::token::InternedString;
 use syntax::ptr::P;
 
-fn is_clike_enum(item: &Annotatable) -> bool {
-    match *item {
-        Annotatable::Item(ref item) => {
-            match item.node {
-                ItemKind::Enum(ref enum_def, _) => {
-                    enum_def.variants.iter().all(|v|
-                        if let VariantData::Unit(..) = v.node.data {
-                            true
-                        } else {
-                            false
-                        }
-                    )
-                }
-                _ => false,
+fn is_type_without_fields(item: &Annotatable) -> bool {
+    if let Annotatable::Item(ref item) = *item {
+        match item.node {
+            ItemKind::Enum(ref enum_def, _) => {
+                enum_def.variants.iter().all(|v| v.node.data.fields().is_empty())
             }
+            ItemKind::Struct(ref variant_data, _) => {
+                variant_data.fields().is_empty()
+            }
+            _ => false
         }
-        _ => false,
+    } else {
+        false
     }
 }
 
@@ -101,8 +97,10 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
     }
 
     // avoid defining `ne` if we can
+    // c-like enums, enums without any fields and structs without fields
+    // can safely define only `eq`.
     let mut methods = vec![md!("eq", cs_eq)];
-    if !is_clike_enum(item) {
+    if !is_type_without_fields(item) {
         methods.push(md!("ne", cs_ne));
     }