]> git.lizzy.rs Git - rust.git/blobdiff - src/matches.rs
Merge pull request #3510 from topecongiro/issue3509
[rust.git] / src / matches.rs
index fc7d4cca0200a913ee596112c26cbe720490ae9b..c09949cf2e86332245709aa3175d78b41d1b5685 100644 (file)
@@ -1,42 +1,32 @@
-// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
 //! Format match expression.
 
 use std::iter::repeat;
 
-use config::lists::*;
 use syntax::source_map::{BytePos, Span};
 use syntax::{ast, ptr};
 
-use comment::{combine_strs_with_missing_comments, rewrite_comment};
-use config::{Config, ControlBraceStyle, IndentStyle};
-use expr::{
+use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
+use crate::config::lists::*;
+use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
+use crate::expr::{
     format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond,
     rewrite_multiple_patterns, ExprType, RhsTactics,
 };
-use lists::{itemize_list, write_list, ListFormatting};
-use rewrite::{Rewrite, RewriteContext};
-use shape::Shape;
-use source_map::SpanUtils;
-use spanned::Spanned;
-use utils::{
+use crate::lists::{itemize_list, write_list, ListFormatting};
+use crate::rewrite::{Rewrite, RewriteContext};
+use crate::shape::Shape;
+use crate::source_map::SpanUtils;
+use crate::spanned::Spanned;
+use crate::utils::{
     contains_skip, extra_offset, first_line_width, inner_attributes, last_line_extendable, mk_sp,
-    ptr_vec_to_ref_vec, trimmed_last_line_width,
+    ptr_vec_to_ref_vec, semicolon_for_expr, trimmed_last_line_width,
 };
 
 /// A simple wrapper type against `ast::Arm`. Used inside `write_list()`.
 struct ArmWrapper<'a> {
     pub arm: &'a ast::Arm,
-    /// True if the arm is the last one in match expression. Used to decide on whether we should add
-    /// trailing comma to the match arm when `config.trailing_comma() == Never`.
+    /// `true` if the arm is the last one in match expression. Used to decide on whether we should
+    /// add trailing comma to the match arm when `config.trailing_comma() == Never`.
     pub is_last: bool,
     /// Holds a byte position of `|` at the beginning of the arm pattern, if available.
     pub beginning_vert: Option<BytePos>,
@@ -67,13 +57,13 @@ fn span(&self) -> Span {
 }
 
 impl<'a> Rewrite for ArmWrapper<'a> {
-    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
+    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
         rewrite_match_arm(context, self.arm, shape, self.is_last)
     }
 }
 
 pub fn rewrite_match(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     cond: &ast::Expr,
     arms: &[ast::Arm],
     shape: Shape,
@@ -168,7 +158,7 @@ fn arm_comma(config: &Config, body: &ast::Expr, is_last: bool) -> &'static str {
 
 /// Collect a byte position of the beginning `|` for each arm, if available.
 fn collect_beginning_verts(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     arms: &[ast::Arm],
     span: Span,
 ) -> Vec<Option<BytePos>> {
@@ -184,7 +174,7 @@ fn collect_beginning_verts(
 }
 
 fn rewrite_match_arms(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     arms: &[ast::Arm],
     shape: Shape,
     span: Span,
@@ -224,7 +214,7 @@ fn rewrite_match_arms(
 }
 
 fn rewrite_match_arm(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     arm: &ast::Arm,
     shape: Shape,
     is_last: bool,
@@ -273,7 +263,7 @@ fn rewrite_match_arm(
         false,
     )?;
 
-    let arrow_span = mk_sp(arm.pats.last().unwrap().span.hi(), arm.body.span.lo());
+    let arrow_span = mk_sp(arm.pats.last().unwrap().span.hi(), arm.body.span().lo());
     rewrite_match_body(
         context,
         &arm.body,
@@ -286,7 +276,7 @@ fn rewrite_match_arm(
 }
 
 fn block_can_be_flattened<'a>(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     expr: &'a ast::Expr,
 ) -> Option<&'a ast::Block> {
     match expr.node {
@@ -304,7 +294,7 @@ fn block_can_be_flattened<'a>(
 // @extend: true if the arm body can be put next to `=>`
 // @body: flattened body, if the body is block with a single expression
 fn flatten_arm_body<'a>(
-    context: &'a RewriteContext,
+    context: &'a RewriteContext<'_>,
     body: &'a ast::Expr,
     opt_shape: Option<Shape>,
 ) -> (bool, &'a ast::Expr) {
@@ -334,7 +324,7 @@ fn flatten_arm_body<'a>(
 }
 
 fn rewrite_match_body(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     body: &ptr::P<ast::Expr>,
     pats_str: &str,
     shape: Shape,
@@ -374,7 +364,8 @@ fn rewrite_match_body(
         shape.indent
     };
 
-    let forbid_same_line = has_guard && pats_str.contains('\n') && !is_empty_block;
+    let forbid_same_line =
+        (has_guard && pats_str.contains('\n') && !is_empty_block) || !body.attrs.is_empty();
 
     // Look for comments between `=>` and the start of the body.
     let arrow_comment = {
@@ -413,7 +404,16 @@ fn rewrite_match_body(
             } else {
                 ""
             };
-            ("{", format!("{}}}{}", indent_str, comma))
+            let semicolon = if context.config.version() == Version::One {
+                ""
+            } else {
+                if semicolon_for_expr(context, body) {
+                    ";"
+                } else {
+                    ""
+                }
+            };
+            ("{", format!("{}{}}}{}", semicolon, indent_str, comma))
         } else {
             ("", String::from(","))
         };
@@ -490,7 +490,7 @@ fn rewrite_match_body(
 }
 
 impl Rewrite for ast::Guard {
-    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
+    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
         match self {
             ast::Guard::If(ref expr) => expr.rewrite(context, shape),
         }
@@ -499,7 +499,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
 // The `if ...` guard on a match arm.
 fn rewrite_guard(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     guard: &Option<ast::Guard>,
     shape: Shape,
     // The amount of space used up on this line for the pattern in