]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
transition to Rust 2018
[rust.git] / src / expr.rs
index 7a6a147306011c05336009bd8c60003a828a0f44..0e22134680f6e5f0356d3ab32af9ff339956e805 100644 (file)
 use std::borrow::Cow;
 use std::cmp::min;
 
-use config::lists::*;
 use syntax::parse::token::DelimToken;
 use syntax::source_map::{BytePos, SourceMap, Span};
 use syntax::{ast, ptr};
 
-use chains::rewrite_chain;
-use closures;
-use comment::{
+use crate::chains::rewrite_chain;
+use crate::closures;
+use crate::comment::{
     combine_strs_with_missing_comments, contains_comment, recover_comment_removed, rewrite_comment,
     rewrite_missing_comment, CharClasses, FindUncommented,
 };
-use config::{Config, ControlBraceStyle, IndentStyle};
-use lists::{
+use crate::config::lists::*;
+use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
+use crate::lists::{
     definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
     struct_lit_tactic, write_list, ListFormatting, ListItem, Separator,
 };
-use macros::{rewrite_macro, MacroPosition};
-use matches::rewrite_match;
-use overflow::{self, IntoOverflowableItem, OverflowableItem};
-use pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
-use patterns::is_short_pattern;
-use rewrite::{Rewrite, RewriteContext};
-use shape::{Indent, Shape};
-use source_map::{LineRangeUtils, SpanUtils};
-use spanned::Spanned;
-use string::{rewrite_string, StringFormat};
-use types::{rewrite_path, PathContext};
-use utils::{
+use crate::macros::{rewrite_macro, MacroPosition};
+use crate::matches::rewrite_match;
+use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
+use crate::pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
+use crate::patterns::is_short_pattern;
+use crate::rewrite::{Rewrite, RewriteContext};
+use crate::shape::{Indent, Shape};
+use crate::source_map::{LineRangeUtils, SpanUtils};
+use crate::spanned::Spanned;
+use crate::string::{rewrite_string, StringFormat};
+use crate::types::{rewrite_path, PathContext};
+use crate::utils::{
     colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes,
     last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
-    semicolon_for_stmt, wrap_str,
+    semicolon_for_expr, semicolon_for_stmt, wrap_str,
 };
-use vertical::rewrite_with_alignment;
-use visitor::FmtVisitor;
+use crate::vertical::rewrite_with_alignment;
+use crate::visitor::FmtVisitor;
 
 impl Rewrite for ast::Expr {
     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
@@ -69,6 +69,11 @@ pub fn format_expr(
     if contains_skip(&*expr.attrs) {
         return Some(context.snippet(expr.span()).to_owned());
     }
+    let shape = if expr_type == ExprType::Statement && semicolon_for_expr(context, expr) {
+        shape.sub_width(1)?
+    } else {
+        shape
+    };
 
     let expr_rw = match expr.node {
         ast::ExprKind::Array(ref expr_vec) => rewrite_array(
@@ -417,15 +422,16 @@ fn rewrite_empty_block(
     prefix: &str,
     shape: Shape,
 ) -> Option<String> {
+    if !block.stmts.is_empty() {
+        return None;
+    }
+
     let label_str = rewrite_label(label);
     if attrs.map_or(false, |a| !inner_attributes(a).is_empty()) {
         return None;
     }
 
-    if block.stmts.is_empty()
-        && !block_contains_comment(block, context.source_map)
-        && shape.width >= 2
-    {
+    if !block_contains_comment(block, context.source_map) && shape.width >= 2 {
         return Some(format!("{}{}{{}}", prefix, label_str));
     }
 
@@ -510,13 +516,13 @@ pub fn rewrite_block_with_visitor(
     let mut visitor = FmtVisitor::from_context(context);
     visitor.block_indent = shape.indent;
     visitor.is_if_else_block = context.is_if_else_block();
-    match block.rules {
-        ast::BlockCheckMode::Unsafe(..) => {
+    match (block.rules, label) {
+        (ast::BlockCheckMode::Unsafe(..), _) | (ast::BlockCheckMode::Default, Some(_)) => {
             let snippet = context.snippet(block.span);
             let open_pos = snippet.find_uncommented("{")?;
             visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
         }
-        ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo(),
+        (ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
     }
 
     let inner_attrs = attrs.map(inner_attributes);
@@ -801,6 +807,20 @@ fn rewrite_single_line(
     }
 }
 
+/// Returns true if the last line of pat_str has leading whitespace and it is wider than the
+/// shape's indent.
+fn last_line_offsetted(start_column: usize, pat_str: &str) -> bool {
+    let mut leading_whitespaces = 0;
+    for c in pat_str.chars().rev() {
+        match c {
+            '\n' => break,
+            _ if c.is_whitespace() => leading_whitespaces += 1,
+            _ => leading_whitespaces = 0,
+        }
+    }
+    leading_whitespaces > start_column
+}
+
 impl<'a> ControlFlow<'a> {
     fn rewrite_pat_expr(
         &self,
@@ -885,7 +905,8 @@ fn rewrite_cond(
             .saturating_sub(constr_shape.used_width() + offset + brace_overhead);
         let force_newline_brace = (pat_expr_string.contains('\n')
             || pat_expr_string.len() > one_line_budget)
-            && !last_line_extendable(&pat_expr_string);
+            && (!last_line_extendable(&pat_expr_string)
+                || last_line_offsetted(shape.used_width(), &pat_expr_string));
 
         // Try to format if-else on single line.
         if self.allow_single_line
@@ -898,10 +919,11 @@ fn rewrite_cond(
             let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width);
 
             if let Some(cond_str) = trial {
-                if cond_str.len() <= context
-                    .config
-                    .width_heuristics()
-                    .single_line_if_else_max_width
+                if cond_str.len()
+                    <= context
+                        .config
+                        .width_heuristics()
+                        .single_line_if_else_max_width
                 {
                     return Some((cond_str, 0));
                 }
@@ -1235,14 +1257,18 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
                         format!(
                             "{}{}",
                             new_indent.to_string(context.config),
-                            line.trim_left()
+                            line.trim_start()
                         )
                     })
                     .collect::<Vec<_>>()
                     .join("\n")
-                    .trim_left(),
+                    .trim_start(),
             );
-            return wrap_str(indented_string_lit, context.config.max_width(), shape);
+            return if context.config.version() == Version::Two {
+                Some(indented_string_lit)
+            } else {
+                wrap_str(indented_string_lit, context.config.max_width(), shape)
+            };
         } else {
             return wrap_str(string_lit.to_owned(), context.config.max_width(), shape);
         }
@@ -1254,6 +1280,7 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
     rewrite_string(
         str_lit,
         &StringFormat::new(shape.visual_indent(0), context.config),
+        shape.width.saturating_sub(2),
     )
 }
 
@@ -1323,15 +1350,28 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
         | ast::ExprKind::WhileLet(..) => {
             context.config.combine_control_expr() && context.use_block_indent() && args_len == 1
         }
-        ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => {
-            context.use_block_indent() || context.config.indent_style() == IndentStyle::Visual
+
+        // Handle always block-like expressions
+        ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => true,
+
+        // Handle `[]` and `{}`-like expressions
+        ast::ExprKind::Array(..) | ast::ExprKind::Struct(..) => {
+            context.config.overflow_delimited_expr()
+                || (context.use_block_indent() && args_len == 1)
+        }
+        ast::ExprKind::Mac(ref macro_) => {
+            match (macro_.node.delim, context.config.overflow_delimited_expr()) {
+                (ast::MacDelimiter::Bracket, true) | (ast::MacDelimiter::Brace, true) => true,
+                _ => context.use_block_indent() && args_len == 1,
+            }
         }
-        ast::ExprKind::Array(..)
-        | ast::ExprKind::Call(..)
-        | ast::ExprKind::Mac(..)
-        | ast::ExprKind::MethodCall(..)
-        | ast::ExprKind::Struct(..)
-        | ast::ExprKind::Tup(..) => context.use_block_indent() && args_len == 1,
+
+        // Handle parenthetical expressions
+        ast::ExprKind::Call(..) | ast::ExprKind::MethodCall(..) | ast::ExprKind::Tup(..) => {
+            context.use_block_indent() && args_len == 1
+        }
+
+        // Handle unary-like expressions
         ast::ExprKind::AddrOf(_, ref expr)
         | ast::ExprKind::Box(ref expr)
         | ast::ExprKind::Try(ref expr)
@@ -1548,13 +1588,13 @@ enum StructLitField<'a> {
         rewrite_with_alignment(
             fields,
             context,
-            shape,
+            v_shape,
             mk_sp(body_lo, span.hi()),
             one_line_width,
         )?
     } else {
         let field_iter = fields
-            .into_iter()
+            .iter()
             .map(StructLitField::Regular)
             .chain(base.into_iter().map(StructLitField::Base));
 
@@ -1814,12 +1854,7 @@ fn rewrite_unary_op(
     shape: Shape,
 ) -> Option<String> {
     // For some reason, an UnOp is not spanned like BinOp!
-    let operator_str = match op {
-        ast::UnOp::Deref => "*",
-        ast::UnOp::Not => "!",
-        ast::UnOp::Neg => "-",
-    };
-    rewrite_unary_prefix(context, operator_str, expr, shape)
+    rewrite_unary_prefix(context, ast::UnOp::to_string(op), expr, shape)
 }
 
 fn rewrite_assignment(
@@ -1937,7 +1972,9 @@ fn shape_from_rhs_tactic(
     rhs_tactic: RhsTactics,
 ) -> Option<Shape> {
     match rhs_tactic {
-        RhsTactics::ForceNextLineWithoutIndent => Some(shape.with_max_width(context.config)),
+        RhsTactics::ForceNextLineWithoutIndent => shape
+            .with_max_width(context.config)
+            .sub_width(shape.indent.width()),
         RhsTactics::Default => {
             Shape::indented(shape.indent.block_indent(context.config), context.config)
                 .sub_width(shape.rhs_overhead(context.config))
@@ -1978,3 +2015,29 @@ pub fn is_method_call(expr: &ast::Expr) -> bool {
         _ => false,
     }
 }
+
+#[cfg(test)]
+mod test {
+    use super::last_line_offsetted;
+
+    #[test]
+    fn test_last_line_offsetted() {
+        let lines = "one\n    two";
+        assert_eq!(last_line_offsetted(2, lines), true);
+        assert_eq!(last_line_offsetted(4, lines), false);
+        assert_eq!(last_line_offsetted(6, lines), false);
+
+        let lines = "one    two";
+        assert_eq!(last_line_offsetted(2, lines), false);
+        assert_eq!(last_line_offsetted(0, lines), false);
+
+        let lines = "\ntwo";
+        assert_eq!(last_line_offsetted(2, lines), false);
+        assert_eq!(last_line_offsetted(0, lines), false);
+
+        let lines = "one\n    two      three";
+        assert_eq!(last_line_offsetted(2, lines), true);
+        let lines = "one\n two      three";
+        assert_eq!(last_line_offsetted(2, lines), false);
+    }
+}