From 548de69d2d767730f15244a66ec4b595efe07786 Mon Sep 17 00:00:00 2001 From: Aaron Lobb Date: Fri, 24 Mar 2017 23:58:34 -0700 Subject: [PATCH] Prevent conversion of empty tuples to unit structs Fixes #1408 --- src/patterns.rs | 93 +++++++++---------- ...mpty-tuple-no-conversion-to-unit-struct.rs | 13 +++ 2 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 tests/target/empty-tuple-no-conversion-to-unit-struct.rs diff --git a/src/patterns.rs b/src/patterns.rs index 18def6bfb64..7ece118f872 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -260,55 +260,54 @@ fn rewrite_tuple_pat(pats: &[ptr::P], } if pat_vec.is_empty() { - path_str + return Some(format!("{}()", try_opt!(path_str))); + } + // add comma if `(x,)` + let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none(); + + let path_len = path_str.as_ref().map(|p| p.len()).unwrap_or(0); + // 2 = "()".len(), 3 = "(,)".len() + let nested_shape = try_opt!(shape.sub_width(path_len + if add_comma { 3 } else { 2 })); + // 1 = "(".len() + let nested_shape = nested_shape.visual_indent(path_len + 1); + let mut items: Vec<_> = itemize_list(context.codemap, + pat_vec.iter(), + if add_comma { ",)" } else { ")" }, + |item| item.span().lo, + |item| item.span().hi, + |item| item.rewrite(context, nested_shape), + context.codemap.span_after(span, "("), + span.hi - BytePos(1)) + .collect(); + + // Condense wildcard string suffix into a single .. + let wildcard_suffix_len = count_wildcard_suffix_len(&items); + + let list = if context.config.condense_wildcard_suffices && wildcard_suffix_len >= 2 { + let new_item_count = 1 + pats.len() - wildcard_suffix_len; + items[new_item_count - 1].item = Some("..".to_owned()); + + let da_iter = items.into_iter().take(new_item_count); + try_opt!(format_item_list(da_iter, nested_shape, context.config)) } else { - // add comma if `(x,)` - let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none(); + try_opt!(format_item_list(items.into_iter(), nested_shape, context.config)) + }; - let path_len = path_str.as_ref().map(|p| p.len()).unwrap_or(0); - // 2 = "()".len(), 3 = "(,)".len() - let nested_shape = try_opt!(shape.sub_width(path_len + if add_comma { 3 } else { 2 })); - // 1 = "(".len() - let nested_shape = nested_shape.visual_indent(path_len + 1); - let mut items: Vec<_> = itemize_list(context.codemap, - pat_vec.iter(), - if add_comma { ",)" } else { ")" }, - |item| item.span().lo, - |item| item.span().hi, - |item| item.rewrite(context, nested_shape), - context.codemap.span_after(span, "("), - span.hi - BytePos(1)) - .collect(); - - // Condense wildcard string suffix into a single .. - let wildcard_suffix_len = count_wildcard_suffix_len(&items); - - let list = if context.config.condense_wildcard_suffices && wildcard_suffix_len >= 2 { - let new_item_count = 1 + pats.len() - wildcard_suffix_len; - items[new_item_count - 1].item = Some("..".to_owned()); - - let da_iter = items.into_iter().take(new_item_count); - try_opt!(format_item_list(da_iter, nested_shape, context.config)) - } else { - try_opt!(format_item_list(items.into_iter(), nested_shape, context.config)) - }; - - match path_str { - Some(path_str) => { - Some(if context.config.spaces_within_parens { - format!("{}( {} )", path_str, list) - } else { - format!("{}({})", path_str, list) - }) - } - None => { - let comma = if add_comma { "," } else { "" }; - Some(if context.config.spaces_within_parens { - format!("( {}{} )", list, comma) - } else { - format!("({}{})", list, comma) - }) - } + match path_str { + Some(path_str) => { + Some(if context.config.spaces_within_parens { + format!("{}( {} )", path_str, list) + } else { + format!("{}({})", path_str, list) + }) + } + None => { + let comma = if add_comma { "," } else { "" }; + Some(if context.config.spaces_within_parens { + format!("( {}{} )", list, comma) + } else { + format!("({}{})", list, comma) + }) } } } diff --git a/tests/target/empty-tuple-no-conversion-to-unit-struct.rs b/tests/target/empty-tuple-no-conversion-to-unit-struct.rs new file mode 100644 index 00000000000..778937b0d56 --- /dev/null +++ b/tests/target/empty-tuple-no-conversion-to-unit-struct.rs @@ -0,0 +1,13 @@ +enum TestEnum { + Arm1(), + Arm2, +} + + +fn foo() { + let test = TestEnum::Arm1; + match test { + TestEnum::Arm1() => {} + TestEnum::Arm2 => {} + } +}