diff options
| author | Seiichi Uchida <seuchida@gmail.com> | 2019-09-08 23:33:21 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-08 23:33:21 +0900 |
| commit | ceca01465a0fe06749b3ac80ae52095f999e495a (patch) | |
| tree | 8afbc8833d9a6a54f16d34bccb5824401a0ddb79 /src | |
| parent | 6fec3a69ec44a8fcc4a0c4e2c2c223da4f0b0161 (diff) | |
Update deps (#3788)
Diffstat (limited to 'src')
| -rw-r--r-- | src/cargo-fmt/main.rs | 3 | ||||
| -rw-r--r-- | src/config/options.rs | 6 | ||||
| -rw-r--r-- | src/expr.rs | 90 | ||||
| -rw-r--r-- | src/format-diff/main.rs | 8 | ||||
| -rw-r--r-- | src/matches.rs | 15 | ||||
| -rw-r--r-- | src/patterns.rs | 48 | ||||
| -rw-r--r-- | src/rustfmt_diff.rs | 2 | ||||
| -rw-r--r-- | src/spanned.rs | 2 | ||||
| -rw-r--r-- | src/utils.rs | 2 |
9 files changed, 68 insertions, 108 deletions
diff --git a/src/cargo-fmt/main.rs b/src/cargo-fmt/main.rs index 05ce9b5f241..c06df111566 100644 --- a/src/cargo-fmt/main.rs +++ b/src/cargo-fmt/main.rs @@ -20,7 +20,6 @@ use structopt::StructOpt; #[derive(StructOpt, Debug)] #[structopt( bin_name = "cargo fmt", - author = "", about = "This utility formats all bin and lib files of \ the current crate using rustfmt." )] @@ -51,7 +50,7 @@ pub struct Opts { /// Options passed to rustfmt // 'raw = true' to make `--` explicit. - #[structopt(name = "rustfmt_options", raw(raw = "true"))] + #[structopt(name = "rustfmt_options", raw(true))] rustfmt_options: Vec<String>, /// Format all packages (only usable in workspaces) diff --git a/src/config/options.rs b/src/config/options.rs index d2063650ccb..4a136f4b180 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -2,7 +2,6 @@ use std::collections::{hash_set, HashSet}; use std::fmt; use std::path::{Path, PathBuf}; -use atty; use itertools::Itertools; use rustfmt_config_proc_macro::config_type; use serde::de::{SeqAccess, Visitor}; @@ -147,7 +146,7 @@ pub enum Color { pub enum Version { /// 1.x.y. When specified, rustfmt will format in the same style as 1.0.0. One, - /// 2.x.y. When specified, rustfmt will formatin the the latest style. + /// 2.x.y. When specified, rustfmt will format in the the latest style. Two, } @@ -155,9 +154,8 @@ impl Color { /// Whether we should use a coloured terminal. pub fn use_colored_tty(self) -> bool { match self { - Color::Always => true, + Color::Always | Color::Auto => true, Color::Never => false, - Color::Auto => atty::is(atty::Stream::Stdout), } } } diff --git a/src/expr.rs b/src/expr.rs index 02bf1fa20e4..36d7b91d102 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -16,13 +16,12 @@ 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, + struct_lit_tactic, write_list, ListFormatting, Separator, }; 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}; @@ -31,8 +30,8 @@ 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_expr, unicode_str_width, wrap_str, + last_line_extendable, last_line_width, mk_sp, outer_attributes, semicolon_for_expr, + unicode_str_width, wrap_str, }; use crate::vertical::rewrite_with_alignment; use crate::visitor::FmtVisitor; @@ -587,7 +586,7 @@ struct ControlFlow<'a> { block: &'a ast::Block, else_block: Option<&'a ast::Expr>, label: Option<ast::Label>, - pats: Vec<&'a ast::Pat>, + pat: Option<&'a ast::Pat>, keyword: &'a str, matcher: &'a str, connector: &'a str, @@ -597,10 +596,10 @@ struct ControlFlow<'a> { span: Span, } -fn extract_pats_and_cond(expr: &ast::Expr) -> (Vec<&ast::Pat>, &ast::Expr) { +fn extract_pats_and_cond(expr: &ast::Expr) -> (Option<&ast::Pat>, &ast::Expr) { match expr.node { - ast::ExprKind::Let(ref pats, ref cond) => (ptr_vec_to_ref_vec(pats), cond), - _ => (vec![], expr), + ast::ExprKind::Let(ref pat, ref cond) => (Some(pat), cond), + _ => (None, expr), } } @@ -608,10 +607,10 @@ fn extract_pats_and_cond(expr: &ast::Expr) -> (Vec<&ast::Pat>, &ast::Expr) { fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow<'_>> { match expr.node { ast::ExprKind::If(ref cond, ref if_block, ref else_block) => { - let (pats, cond) = extract_pats_and_cond(cond); + let (pat, cond) = extract_pats_and_cond(cond); Some(ControlFlow::new_if( cond, - pats, + pat, if_block, else_block.as_ref().map(|e| &**e), expr_type == ExprType::SubExpression, @@ -626,34 +625,34 @@ fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow< Some(ControlFlow::new_loop(block, label, expr.span)) } ast::ExprKind::While(ref cond, ref block, label) => { - let (pats, cond) = extract_pats_and_cond(cond); - Some(ControlFlow::new_while(pats, cond, block, label, expr.span)) + let (pat, cond) = extract_pats_and_cond(cond); + Some(ControlFlow::new_while(pat, cond, block, label, expr.span)) } _ => None, } } -fn choose_matcher(pats: &[&ast::Pat]) -> &'static str { - if pats.is_empty() { "" } else { "let" } +fn choose_matcher(pat: Option<&ast::Pat>) -> &'static str { + pat.map_or("", |_| "let") } impl<'a> ControlFlow<'a> { fn new_if( cond: &'a ast::Expr, - pats: Vec<&'a ast::Pat>, + pat: Option<&'a ast::Pat>, block: &'a ast::Block, else_block: Option<&'a ast::Expr>, allow_single_line: bool, nested_if: bool, span: Span, ) -> ControlFlow<'a> { - let matcher = choose_matcher(&pats); + let matcher = choose_matcher(pat); ControlFlow { cond: Some(cond), block, else_block, label: None, - pats, + pat, keyword: "if", matcher, connector: " =", @@ -669,7 +668,7 @@ impl<'a> ControlFlow<'a> { block, else_block: None, label, - pats: vec![], + pat: None, keyword: "loop", matcher: "", connector: "", @@ -680,19 +679,19 @@ impl<'a> ControlFlow<'a> { } fn new_while( - pats: Vec<&'a ast::Pat>, + pat: Option<&'a ast::Pat>, cond: &'a ast::Expr, block: &'a ast::Block, label: Option<ast::Label>, span: Span, ) -> ControlFlow<'a> { - let matcher = choose_matcher(&pats); + let matcher = choose_matcher(pat); ControlFlow { cond: Some(cond), block, else_block: None, label, - pats, + pat, keyword: "while", matcher, connector: " =", @@ -714,7 +713,7 @@ impl<'a> ControlFlow<'a> { block, else_block: None, label, - pats: vec![pat], + pat: Some(pat), keyword: "for", matcher: "", connector: " in", @@ -790,10 +789,10 @@ impl<'a> ControlFlow<'a> { shape: Shape, offset: usize, ) -> Option<String> { - debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, self.pats, expr); + debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, self.pat, expr); let cond_shape = shape.offset_left(offset)?; - if !self.pats.is_empty() { + if !self.pat.is_none() { let matcher = if self.matcher.is_empty() { self.matcher.to_owned() } else { @@ -802,7 +801,11 @@ impl<'a> ControlFlow<'a> { let pat_shape = cond_shape .offset_left(matcher.len())? .sub_width(self.connector.len())?; - let pat_string = rewrite_multiple_patterns(context, &self.pats, pat_shape)?; + let pat_string = if let Some(pat) = self.pat { + pat.rewrite(context, pat_shape)? + } else { + "".to_owned() + }; let result = format!("{}{}{}", matcher, pat_string, self.connector); return rewrite_assign_rhs(context, result, expr, cond_shape); } @@ -906,10 +909,10 @@ impl<'a> ControlFlow<'a> { context .snippet_provider .span_after(mk_sp(lo, self.span.hi()), self.keyword.trim()), - if self.pats.is_empty() { + if self.pat.is_none() { cond_span.lo() } else if self.matcher.is_empty() { - self.pats[0].span.lo() + self.pat.unwrap().span.lo() } else { context .snippet_provider @@ -1145,39 +1148,6 @@ pub(crate) fn is_unsafe_block(block: &ast::Block) -> bool { } } -pub(crate) fn rewrite_multiple_patterns( - context: &RewriteContext<'_>, - pats: &[&ast::Pat], - shape: Shape, -) -> Option<String> { - let pat_strs = pats - .iter() - .map(|p| p.rewrite(context, shape)) - .collect::<Option<Vec<_>>>()?; - - let use_mixed_layout = pats - .iter() - .zip(pat_strs.iter()) - .all(|(pat, pat_str)| is_short_pattern(pat, pat_str)); - let items: Vec<_> = pat_strs.into_iter().map(ListItem::from_str).collect(); - let tactic = if use_mixed_layout { - DefinitiveListTactic::Mixed - } else { - definitive_tactic( - &items, - ListTactic::HorizontalVertical, - Separator::VerticalBar, - shape.width, - ) - }; - let fmt = ListFormatting::new(shape, context.config) - .tactic(tactic) - .separator(" |") - .separator_place(context.config.binop_separator()) - .ends_with_newline(false); - write_list(&items, &fmt) -} - pub(crate) fn rewrite_literal( context: &RewriteContext<'_>, l: &ast::Lit, diff --git a/src/format-diff/main.rs b/src/format-diff/main.rs index 5bfdcbd0420..afe1ec5d068 100644 --- a/src/format-diff/main.rs +++ b/src/format-diff/main.rs @@ -58,10 +58,8 @@ impl From<io::Error> for FormatDiffError { #[derive(StructOpt, Debug)] #[structopt( name = "rustfmt-format-diff", - author = "", - about = "", - raw(setting = "AppSettings::DisableVersion"), - raw(setting = "AppSettings::NextLineHelp") + setting = AppSettings::DisableVersion, + setting = AppSettings::NextLineHelp )] pub struct Opts { /// Skip the smallest prefix containing NUMBER slashes @@ -78,7 +76,7 @@ pub struct Opts { short = "f", long = "filter", value_name = "PATTERN", - raw(default_value = "DEFAULT_PATTERN") + default_value = DEFAULT_PATTERN )] filter: String, } diff --git a/src/matches.rs b/src/matches.rs index b608faded9e..e8ea5d6ad42 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -10,7 +10,7 @@ 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, + ExprType, RhsTactics, }; use crate::lists::{itemize_list, write_list, ListFormatting}; use crate::rewrite::{Rewrite, RewriteContext}; @@ -19,7 +19,7 @@ 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, semicolon_for_expr, trimmed_last_line_width, unicode_str_width, + semicolon_for_expr, trimmed_last_line_width, unicode_str_width, }; /// A simple wrapper type against `ast::Arm`. Used inside `write_list()`. @@ -161,7 +161,7 @@ fn collect_beginning_verts( let mut beginning_verts = Vec::with_capacity(arms.len()); let mut lo = context.snippet_provider.span_after(span, "{"); for arm in arms { - let hi = arm.pats[0].span.lo(); + let hi = arm.pat.span.lo(); let missing_span = mk_sp(lo, hi); beginning_verts.push(context.snippet_provider.opt_span_before(missing_span, "|")); lo = arm.span().hi(); @@ -225,10 +225,7 @@ fn rewrite_match_arm( arm_comma(context.config, body, is_last), )); } - let missing_span = mk_sp( - arm.attrs[arm.attrs.len() - 1].span.hi(), - arm.pats[0].span.lo(), - ); + let missing_span = mk_sp(arm.attrs[arm.attrs.len() - 1].span.hi(), arm.pat.span.lo()); (missing_span, arm.attrs.rewrite(context, shape)?) } else { (mk_sp(arm.span().lo(), arm.span().lo()), String::new()) @@ -237,7 +234,7 @@ fn rewrite_match_arm( // Patterns // 5 = ` => {` let pat_shape = shape.sub_width(5)?; - let pats_str = rewrite_multiple_patterns(context, &ptr_vec_to_ref_vec(&arm.pats), pat_shape)?; + let pats_str = arm.pat.rewrite(context, pat_shape)?; // Guard let block_like_pat = trimmed_last_line_width(&pats_str) <= context.config.tab_spaces(); @@ -259,7 +256,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.pat.span.hi(), arm.body.span().lo()); rewrite_match_body( context, &arm.body, diff --git a/src/patterns.rs b/src/patterns.rs index 96d45c6ac55..9a89c8d192c 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -7,7 +7,7 @@ use crate::config::lists::*; use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field}; use crate::lists::{ definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape, - struct_lit_tactic, write_list, ListFormatting, Separator, + struct_lit_tactic, write_list, ListFormatting, ListItem, Separator, }; use crate::macros::{rewrite_macro, MacroPosition}; use crate::overflow; @@ -59,30 +59,32 @@ impl Rewrite for Pat { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { match self.node { PatKind::Or(ref pats) => { - let pat_items = itemize_list( - context.snippet_provider, - pats.iter(), - "", - "|", - |pat| pat.span().lo(), - |pat| pat.span().hi(), - |pat| pat.rewrite(context, shape), - self.span.lo(), - self.span.hi(), - false, - ); - let pat_vec: Vec<_> = pat_items.collect(); - let tactic = definitive_tactic( - &pat_vec, - ListTactic::HorizontalVertical, - Separator::VerticalBar, - shape.width, - ); + let pat_strs = pats + .iter() + .map(|p| p.rewrite(context, shape)) + .collect::<Option<Vec<_>>>()?; + + let use_mixed_layout = pats + .iter() + .zip(pat_strs.iter()) + .all(|(pat, pat_str)| is_short_pattern(pat, pat_str)); + let items: Vec<_> = pat_strs.into_iter().map(ListItem::from_str).collect(); + let tactic = if use_mixed_layout { + DefinitiveListTactic::Mixed + } else { + definitive_tactic( + &items, + ListTactic::HorizontalVertical, + Separator::VerticalBar, + shape.width, + ) + }; let fmt = ListFormatting::new(shape, context.config) .tactic(tactic) - .trailing_separator(SeparatorTactic::Never) - .separator(" |"); - write_list(&pat_vec, &fmt) + .separator(" |") + .separator_place(context.config.binop_separator()) + .ends_with_newline(false); + write_list(&items, &fmt) } PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape), PatKind::Ident(binding_mode, ident, ref sub_pat) => { diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs index fd332b12403..fc2c7d06e26 100644 --- a/src/rustfmt_diff.rs +++ b/src/rustfmt_diff.rs @@ -3,8 +3,6 @@ use std::fmt; use std::io; use std::io::Write; -use diff; - use crate::config::{Color, Config, Verbosity}; #[derive(Debug, PartialEq)] diff --git a/src/spanned.rs b/src/spanned.rs index bbdee6ccf4b..576085ae384 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -96,7 +96,7 @@ impl Spanned for ast::Ty { impl Spanned for ast::Arm { fn span(&self) -> Span { let lo = if self.attrs.is_empty() { - self.pats[0].span.lo() + self.pat.span.lo() } else { self.attrs[0].span.lo() }; diff --git a/src/utils.rs b/src/utils.rs index 5b0d94e4780..f2a2a3c6675 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,5 @@ use std::borrow::Cow; -use bytecount; - use rustc_target::spec::abi; use syntax::ast::{ self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility, |
