Skip to content

Commit 548de69

Browse files
committed
Prevent conversion of empty tuples to unit structs
Fixes #1408
1 parent 488c0b9 commit 548de69

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
@@ -260,55 +260,54 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
260260
}
261261

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

296-
match path_str {
297-
Some(path_str) => {
298-
Some(if context.config.spaces_within_parens {
299-
format!("{}( {} )", path_str, list)
300-
} else {
301-
format!("{}({})", path_str, list)
302-
})
303-
}
304-
None => {
305-
let comma = if add_comma { "," } else { "" };
306-
Some(if context.config.spaces_within_parens {
307-
format!("( {}{} )", list, comma)
308-
} else {
309-
format!("({}{})", list, comma)
310-
})
311-
}
296+
match path_str {
297+
Some(path_str) => {
298+
Some(if context.config.spaces_within_parens {
299+
format!("{}( {} )", path_str, list)
300+
} else {
301+
format!("{}({})", path_str, list)
302+
})
303+
}
304+
None => {
305+
let comma = if add_comma { "," } else { "" };
306+
Some(if context.config.spaces_within_parens {
307+
format!("( {}{} )", list, comma)
308+
} else {
309+
format!("({}{})", list, comma)
310+
})
312311
}
313312
}
314313
}
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)