Skip to content

Commit 52ae0d4

Browse files
committed
Support unions here and there
1 parent b1353e6 commit 52ae0d4

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

clippy_lints/src/derive.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref
140140
return; // ty is not Copy
141141
}
142142

143-
// Some types are not Clone by default but could be cloned `by hand` if necessary
144143
match ty.sty {
144+
TypeVariants::TyUnion(..) => return,
145+
146+
// Some types are not Clone by default but could be cloned “by hand” if necessary
145147
TypeVariants::TyEnum(def, substs) |
146148
TypeVariants::TyStruct(def, substs) => {
147149
for variant in &def.variants {

clippy_lints/src/len_zero.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ fn has_is_empty(cx: &LateContext, expr: &Expr) -> bool {
209209
}
210210
ty::TyProjection(_) => ty.ty_to_def_id().map_or(false, |id| has_is_empty_impl(cx, &id)),
211211
ty::TyEnum(id, _) |
212-
ty::TyStruct(id, _) => has_is_empty_impl(cx, &id.did),
212+
ty::TyStruct(id, _) |
213+
ty::TyUnion(id, _) => has_is_empty_impl(cx, &id.did),
213214
ty::TyArray(..) | ty::TyStr => true,
214215
_ => false,
215216
}

tests/compile-fail/derive.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![feature(plugin)]
22
#![plugin(clippy)]
33

4+
#![feature(untagged_unions)]
5+
46
#![deny(warnings)]
57
#![allow(dead_code)]
68

@@ -45,6 +47,20 @@ impl Clone for Qux {
4547
fn clone(&self) -> Self { Qux }
4648
}
4749

50+
// looks like unions don't support deriving Clone for now
51+
#[derive(Copy)]
52+
union Union {
53+
a: u8,
54+
}
55+
56+
impl Clone for Union {
57+
fn clone(&self) -> Self {
58+
Union {
59+
a: 42,
60+
}
61+
}
62+
}
63+
4864
// See #666
4965
#[derive(Copy)]
5066
struct Lt<'a> {

tests/compile-fail/no_effect.rs

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![deny(no_effect, unnecessary_operation)]
55
#![allow(dead_code)]
66
#![allow(path_statements)]
7+
#![feature(untagged_unions)]
78

89
struct Unit;
910
struct Tuple(i32);
@@ -15,6 +16,11 @@ enum Enum {
1516
Struct { field: i32 },
1617
}
1718

19+
union Union {
20+
a: u8,
21+
b: f64,
22+
}
23+
1824
fn get_number() -> i32 { 0 }
1925
fn get_struct() -> Struct { Struct { field: 0 } }
2026

@@ -30,6 +36,7 @@ fn main() {
3036
Tuple(0); //~ERROR statement with no effect
3137
Struct { field: 0 }; //~ERROR statement with no effect
3238
Struct { ..s }; //~ERROR statement with no effect
39+
Union { a: 0 }; //~ERROR statement with no effect
3340
Enum::Tuple(0); //~ERROR statement with no effect
3441
Enum::Struct { field: 0 }; //~ERROR statement with no effect
3542
5 + 6; //~ERROR statement with no effect

0 commit comments

Comments
 (0)