Skip to content

Commit f53d5dd

Browse files
authored
Merge pull request #1409 from alobb/1408-empty-tuple-enum-decl
Prevent conversion of empty tuples to unit structs
2 parents abca1de + 548de69 commit f53d5dd

File tree

2 files changed

+59
-47
lines changed

2 files changed

+59
-47
lines changed

src/patterns.rs

+46-47
Original file line numberDiff line numberDiff line change
@@ -266,55 +266,54 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
266266
}
267267

268268
if pat_vec.is_empty() {
269-
path_str
269+
return Some(format!("{}()", try_opt!(path_str)));
270+
}
271+
// add comma if `(x,)`
272+
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
273+
274+
let path_len = path_str.as_ref().map(|p| p.len()).unwrap_or(0);
275+
// 2 = "()".len(), 3 = "(,)".len()
276+
let nested_shape = try_opt!(shape.sub_width(path_len + if add_comma { 3 } else { 2 }));
277+
// 1 = "(".len()
278+
let nested_shape = nested_shape.visual_indent(path_len + 1);
279+
let mut items: Vec<_> = itemize_list(context.codemap,
280+
pat_vec.iter(),
281+
if add_comma { ",)" } else { ")" },
282+
|item| item.span().lo,
283+
|item| item.span().hi,
284+
|item| item.rewrite(context, nested_shape),
285+
context.codemap.span_after(span, "("),
286+
span.hi - BytePos(1))
287+
.collect();
288+
289+
// Condense wildcard string suffix into a single ..
290+
let wildcard_suffix_len = count_wildcard_suffix_len(&items);
291+
292+
let list = if context.config.condense_wildcard_suffices && wildcard_suffix_len >= 2 {
293+
let new_item_count = 1 + pats.len() - wildcard_suffix_len;
294+
items[new_item_count - 1].item = Some("..".to_owned());
295+
296+
let da_iter = items.into_iter().take(new_item_count);
297+
try_opt!(format_item_list(da_iter, nested_shape, context.config))
270298
} else {
271-
// add comma if `(x,)`
272-
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
273-
274-
let path_len = path_str.as_ref().map(|p| p.len()).unwrap_or(0);
275-
// 2 = "()".len(), 3 = "(,)".len()
276-
let nested_shape = try_opt!(shape.sub_width(path_len + if add_comma { 3 } else { 2 }));
277-
// 1 = "(".len()
278-
let nested_shape = nested_shape.visual_indent(path_len + 1);
279-
let mut items: Vec<_> = itemize_list(context.codemap,
280-
pat_vec.iter(),
281-
if add_comma { ",)" } else { ")" },
282-
|item| item.span().lo,
283-
|item| item.span().hi,
284-
|item| item.rewrite(context, nested_shape),
285-
context.codemap.span_after(span, "("),
286-
span.hi - BytePos(1))
287-
.collect();
288-
289-
// Condense wildcard string suffix into a single ..
290-
let wildcard_suffix_len = count_wildcard_suffix_len(&items);
291-
292-
let list = if context.config.condense_wildcard_suffices && wildcard_suffix_len >= 2 {
293-
let new_item_count = 1 + pats.len() - wildcard_suffix_len;
294-
items[new_item_count - 1].item = Some("..".to_owned());
295-
296-
let da_iter = items.into_iter().take(new_item_count);
297-
try_opt!(format_item_list(da_iter, nested_shape, context.config))
298-
} else {
299-
try_opt!(format_item_list(items.into_iter(), nested_shape, context.config))
300-
};
299+
try_opt!(format_item_list(items.into_iter(), nested_shape, context.config))
300+
};
301301

302-
match path_str {
303-
Some(path_str) => {
304-
Some(if context.config.spaces_within_parens {
305-
format!("{}( {} )", path_str, list)
306-
} else {
307-
format!("{}({})", path_str, list)
308-
})
309-
}
310-
None => {
311-
let comma = if add_comma { "," } else { "" };
312-
Some(if context.config.spaces_within_parens {
313-
format!("( {}{} )", list, comma)
314-
} else {
315-
format!("({}{})", list, comma)
316-
})
317-
}
302+
match path_str {
303+
Some(path_str) => {
304+
Some(if context.config.spaces_within_parens {
305+
format!("{}( {} )", path_str, list)
306+
} else {
307+
format!("{}({})", path_str, list)
308+
})
309+
}
310+
None => {
311+
let comma = if add_comma { "," } else { "" };
312+
Some(if context.config.spaces_within_parens {
313+
format!("( {}{} )", list, comma)
314+
} else {
315+
format!("({}{})", list, comma)
316+
})
318317
}
319318
}
320319
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
enum TestEnum {
2+
Arm1(),
3+
Arm2,
4+
}
5+
6+
7+
fn foo() {
8+
let test = TestEnum::Arm1;
9+
match test {
10+
TestEnum::Arm1() => {}
11+
TestEnum::Arm2 => {}
12+
}
13+
}

0 commit comments

Comments
 (0)