From 4bb90f5cc8e0347534436612f45a3c22c7e265bb Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sat, 9 Feb 2019 16:14:30 +0900 Subject: Fix rust_2018_idioms warnings --- src/expr.rs | 92 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) (limited to 'src/expr.rs') diff --git a/src/expr.rs b/src/expr.rs index 744bc00d0de..14a5ebd28ee 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -47,7 +47,7 @@ use crate::vertical::rewrite_with_alignment; use crate::visitor::FmtVisitor; impl Rewrite for ast::Expr { - fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { format_expr(self, ExprType::SubExpression, context, shape) } } @@ -61,7 +61,7 @@ pub enum ExprType { pub fn format_expr( expr: &ast::Expr, expr_type: ExprType, - context: &RewriteContext, + context: &RewriteContext<'_>, shape: Shape, ) -> Option { skip_out_of_file_lines_range!(context, expr.span); @@ -249,7 +249,7 @@ pub fn format_expr( ast::RangeLimits::Closed => "..=", }; - fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool { + fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool { match lhs.node { ast::ExprKind::Lit(ref lit) => match lit.node { ast::LitKind::FloatUnsuffixed(..) => { @@ -399,7 +399,7 @@ pub fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>( name: &'a str, exprs: impl Iterator, span: Span, - context: &'a RewriteContext, + context: &'a RewriteContext<'_>, shape: Shape, force_separator_tactic: Option, delim_token: Option, @@ -416,7 +416,7 @@ pub fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>( } fn rewrite_empty_block( - context: &RewriteContext, + context: &RewriteContext<'_>, block: &ast::Block, attrs: Option<&[ast::Attribute]>, label: Option, @@ -453,7 +453,7 @@ fn rewrite_empty_block( None } -fn block_prefix(context: &RewriteContext, block: &ast::Block, shape: Shape) -> Option { +fn block_prefix(context: &RewriteContext<'_>, block: &ast::Block, shape: Shape) -> Option { Some(match block.rules { ast::BlockCheckMode::Unsafe(..) => { let snippet = context.snippet(block.span); @@ -482,7 +482,7 @@ fn block_prefix(context: &RewriteContext, block: &ast::Block, shape: Shape) -> O } fn rewrite_single_line_block( - context: &RewriteContext, + context: &RewriteContext<'_>, prefix: &str, block: &ast::Block, attrs: Option<&[ast::Attribute]>, @@ -502,7 +502,7 @@ fn rewrite_single_line_block( } pub fn rewrite_block_with_visitor( - context: &RewriteContext, + context: &RewriteContext<'_>, prefix: &str, block: &ast::Block, attrs: Option<&[ast::Attribute]>, @@ -533,7 +533,7 @@ pub fn rewrite_block_with_visitor( } impl Rewrite for ast::Block { - fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { rewrite_block(self, None, None, context, shape) } } @@ -542,7 +542,7 @@ fn rewrite_block( block: &ast::Block, attrs: Option<&[ast::Attribute]>, label: Option, - context: &RewriteContext, + context: &RewriteContext<'_>, shape: Shape, ) -> Option { let prefix = block_prefix(context, block, shape)?; @@ -568,7 +568,7 @@ fn rewrite_block( } impl Rewrite for ast::Stmt { - fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { skip_out_of_file_lines_range!(context, self.span()); let result = match self.node { @@ -590,7 +590,7 @@ impl Rewrite for ast::Stmt { } // Rewrite condition if the given expression has one. -pub fn rewrite_cond(context: &RewriteContext, expr: &ast::Expr, shape: Shape) -> Option { +pub fn rewrite_cond(context: &RewriteContext<'_>, expr: &ast::Expr, shape: Shape) -> Option { match expr.node { ast::ExprKind::Match(ref cond, _) => { // `match `cond` {` @@ -627,7 +627,7 @@ struct ControlFlow<'a> { span: Span, } -fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option { +fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option> { match expr.node { ast::ExprKind::If(ref cond, ref if_block, ref else_block) => Some(ControlFlow::new_if( cond, @@ -767,7 +767,7 @@ impl<'a> ControlFlow<'a> { fn rewrite_single_line( &self, pat_expr_str: &str, - context: &RewriteContext, + context: &RewriteContext<'_>, width: usize, ) -> Option { assert!(self.allow_single_line); @@ -825,7 +825,7 @@ fn last_line_offsetted(start_column: usize, pat_str: &str) -> bool { impl<'a> ControlFlow<'a> { fn rewrite_pat_expr( &self, - context: &RewriteContext, + context: &RewriteContext<'_>, expr: &ast::Expr, shape: Shape, offset: usize, @@ -865,7 +865,7 @@ impl<'a> ControlFlow<'a> { fn rewrite_cond( &self, - context: &RewriteContext, + context: &RewriteContext<'_>, shape: Shape, alt_block_sep: &str, ) -> Option<(String, usize)> { @@ -1001,7 +1001,7 @@ impl<'a> ControlFlow<'a> { } impl<'a> Rewrite for ControlFlow<'a> { - fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { debug!("ControlFlow::rewrite {:?} {:?}", self, shape); let alt_block_sep = &shape.indent.to_string_with_newline(context.config); @@ -1126,7 +1126,7 @@ fn rewrite_label(opt_label: Option) -> Cow<'static, str> { } } -fn extract_comment(span: Span, context: &RewriteContext, shape: Shape) -> Option { +fn extract_comment(span: Span, context: &RewriteContext<'_>, shape: Shape) -> Option { match rewrite_missing_comment(span, shape, context) { Some(ref comment) if !comment.is_empty() => Some(format!( "{indent}{}{indent}", @@ -1197,7 +1197,7 @@ pub fn is_unsafe_block(block: &ast::Block) -> bool { } pub fn rewrite_multiple_patterns( - context: &RewriteContext, + context: &RewriteContext<'_>, pats: &[&ast::Pat], shape: Shape, ) -> Option { @@ -1229,7 +1229,7 @@ pub fn rewrite_multiple_patterns( write_list(&items, &fmt) } -pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) -> Option { +pub fn rewrite_literal(context: &RewriteContext<'_>, l: &ast::Lit, shape: Shape) -> Option { match l.node { ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape), _ => wrap_str( @@ -1240,7 +1240,7 @@ pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) -> } } -fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Option { +fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> Option { let string_lit = context.snippet(span); if !context.config.format_strings() { @@ -1285,7 +1285,7 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt ) } -fn choose_separator_tactic(context: &RewriteContext, span: Span) -> Option { +fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option { if context.inside_macro() { if span_ends_with_comma(context, span) { Some(SeparatorTactic::Always) @@ -1298,7 +1298,7 @@ fn choose_separator_tactic(context: &RewriteContext, span: Span) -> Option, callee: &str, args: &[ptr::P], span: Span, @@ -1333,11 +1333,11 @@ pub fn is_simple_expr(expr: &ast::Expr) -> bool { } } -pub fn is_every_expr_simple(lists: &[OverflowableItem]) -> bool { +pub fn is_every_expr_simple(lists: &[OverflowableItem<'_>]) -> bool { lists.iter().all(OverflowableItem::is_simple) } -pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_len: usize) -> bool { +pub fn can_be_overflowed_expr(context: &RewriteContext<'_>, expr: &ast::Expr, args_len: usize) -> bool { match expr.node { ast::ExprKind::Match(..) => { (context.use_block_indent() && args_len == 1) @@ -1397,7 +1397,7 @@ pub fn is_nested_call(expr: &ast::Expr) -> bool { /// Return true if a function call or a method call represented by the given span ends with a /// trailing comma. This function is used when rewriting macro, as adding or removing a trailing /// comma from macro can potentially break the code. -pub fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool { +pub fn span_ends_with_comma(context: &RewriteContext<'_>, span: Span) -> bool { let mut result: bool = Default::default(); let mut prev_char: char = Default::default(); let closing_delimiters = &[')', '}', ']']; @@ -1418,7 +1418,7 @@ pub fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool { } fn rewrite_paren( - context: &RewriteContext, + context: &RewriteContext<'_>, mut subexpr: &ast::Expr, shape: Shape, mut span: Span, @@ -1462,7 +1462,7 @@ fn rewrite_paren( } fn rewrite_paren_in_multi_line( - context: &RewriteContext, + context: &RewriteContext<'_>, subexpr: &ast::Expr, shape: Shape, pre_span: Span, @@ -1495,7 +1495,7 @@ fn rewrite_paren_in_multi_line( fn rewrite_index( expr: &ast::Expr, index: &ast::Expr, - context: &RewriteContext, + context: &RewriteContext<'_>, shape: Shape, ) -> Option { let expr_str = expr.rewrite(context, shape)?; @@ -1556,7 +1556,7 @@ fn struct_lit_can_be_aligned(fields: &[ast::Field], base: Option<&ast::Expr>) -> } fn rewrite_struct_lit<'a>( - context: &RewriteContext, + context: &RewriteContext<'_>, path: &ast::Path, fields: &'a [ast::Field], base: Option<&'a ast::Expr>, @@ -1599,7 +1599,7 @@ fn rewrite_struct_lit<'a>( .map(StructLitField::Regular) .chain(base.into_iter().map(StructLitField::Base)); - let span_lo = |item: &StructLitField| match *item { + let span_lo = |item: &StructLitField<'_>| match *item { StructLitField::Regular(field) => field.span().lo(), StructLitField::Base(expr) => { let last_field_hi = fields.last().map_or(span.lo(), |field| field.span.hi()); @@ -1608,11 +1608,11 @@ fn rewrite_struct_lit<'a>( last_field_hi + BytePos(pos as u32) } }; - let span_hi = |item: &StructLitField| match *item { + let span_hi = |item: &StructLitField<'_>| match *item { StructLitField::Regular(field) => field.span().hi(), StructLitField::Base(expr) => expr.span.hi(), }; - let rewrite = |item: &StructLitField| match *item { + let rewrite = |item: &StructLitField<'_>| match *item { StructLitField::Regular(field) => { // The 1 taken from the v_budget is for the comma. rewrite_field(context, field, v_shape.sub_width(1)?, 0) @@ -1662,7 +1662,7 @@ fn rewrite_struct_lit<'a>( } pub fn wrap_struct_field( - context: &RewriteContext, + context: &RewriteContext<'_>, fields_str: &str, shape: Shape, nested_shape: Shape, @@ -1690,7 +1690,7 @@ pub fn struct_lit_field_separator(config: &Config) -> &str { } pub fn rewrite_field( - context: &RewriteContext, + context: &RewriteContext<'_>, field: &ast::Field, shape: Shape, prefix_max_width: usize, @@ -1739,7 +1739,7 @@ pub fn rewrite_field( } fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>( - context: &RewriteContext, + context: &RewriteContext<'_>, mut items: impl Iterator, span: Span, shape: Shape, @@ -1787,7 +1787,7 @@ fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>( } pub fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>( - context: &'a RewriteContext, + context: &'a RewriteContext<'_>, items: impl Iterator, span: Span, shape: Shape, @@ -1822,7 +1822,7 @@ pub fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>( } pub fn rewrite_unary_prefix( - context: &RewriteContext, + context: &RewriteContext<'_>, prefix: &str, rewrite: &R, shape: Shape, @@ -1835,7 +1835,7 @@ pub fn rewrite_unary_prefix( // FIXME: this is probably not correct for multi-line Rewrites. we should // subtract suffix.len() from the last line budget, not the first! pub fn rewrite_unary_suffix( - context: &RewriteContext, + context: &RewriteContext<'_>, suffix: &str, rewrite: &R, shape: Shape, @@ -1849,7 +1849,7 @@ pub fn rewrite_unary_suffix( } fn rewrite_unary_op( - context: &RewriteContext, + context: &RewriteContext<'_>, op: ast::UnOp, expr: &ast::Expr, shape: Shape, @@ -1859,7 +1859,7 @@ fn rewrite_unary_op( } fn rewrite_assignment( - context: &RewriteContext, + context: &RewriteContext<'_>, lhs: &ast::Expr, rhs: &ast::Expr, op: Option<&ast::BinOp>, @@ -1889,7 +1889,7 @@ pub enum RhsTactics { // The left hand side must contain everything up to, and including, the // assignment operator. pub fn rewrite_assign_rhs, R: Rewrite>( - context: &RewriteContext, + context: &RewriteContext<'_>, lhs: S, ex: &R, shape: Shape, @@ -1898,7 +1898,7 @@ pub fn rewrite_assign_rhs, R: Rewrite>( } pub fn rewrite_assign_rhs_with, R: Rewrite>( - context: &RewriteContext, + context: &RewriteContext<'_>, lhs: S, ex: &R, shape: Shape, @@ -1927,7 +1927,7 @@ pub fn rewrite_assign_rhs_with, R: Rewrite>( } fn choose_rhs( - context: &RewriteContext, + context: &RewriteContext<'_>, expr: &R, shape: Shape, orig_rhs: Option, @@ -1968,7 +1968,7 @@ fn choose_rhs( } fn shape_from_rhs_tactic( - context: &RewriteContext, + context: &RewriteContext<'_>, shape: Shape, rhs_tactic: RhsTactics, ) -> Option { @@ -1993,7 +1993,7 @@ pub fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str, rhs_tactics: RhsTac } fn rewrite_expr_addrof( - context: &RewriteContext, + context: &RewriteContext<'_>, mutability: ast::Mutability, expr: &ast::Expr, shape: Shape, -- cgit 1.4.1-3-g733a5 From 8183b949c49cfdd5889859162e82fab4c8e64065 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sat, 9 Feb 2019 16:20:38 +0900 Subject: cargo fmt --- src/cargo-fmt/main.rs | 1 - src/chains.rs | 14 +++++++++++--- src/expr.rs | 12 ++++++++++-- src/items.rs | 12 ++++++++++-- src/macros.rs | 12 ++++++++++-- src/overflow.rs | 5 ++++- src/patterns.rs | 6 +++++- 7 files changed, 50 insertions(+), 12 deletions(-) (limited to 'src/expr.rs') diff --git a/src/cargo-fmt/main.rs b/src/cargo-fmt/main.rs index 05861743963..3873671781c 100644 --- a/src/cargo-fmt/main.rs +++ b/src/cargo-fmt/main.rs @@ -16,7 +16,6 @@ use cargo_metadata; use getopts; - use std::collections::{HashMap, HashSet}; use std::env; use std::fs; diff --git a/src/chains.rs b/src/chains.rs index 37bbde1f4ee..66862116323 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -84,7 +84,11 @@ use crate::utils::{ trimmed_last_line_width, wrap_str, }; -pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext<'_>, shape: Shape) -> Option { +pub fn rewrite_chain( + expr: &ast::Expr, + context: &RewriteContext<'_>, + shape: Shape, +) -> Option { let chain = Chain::from_ast(expr, context); debug!("rewrite_chain {:?} {:?}", chain, shape); @@ -419,8 +423,12 @@ impl Rewrite for Chain { debug!("rewrite chain {:?} {:?}", self, shape); let mut formatter = match context.config.indent_style() { - IndentStyle::Block => Box::new(ChainFormatterBlock::new(self)) as Box, - IndentStyle::Visual => Box::new(ChainFormatterVisual::new(self)) as Box, + IndentStyle::Block => { + Box::new(ChainFormatterBlock::new(self)) as Box + } + IndentStyle::Visual => { + Box::new(ChainFormatterVisual::new(self)) as Box + } }; formatter.format_root(&self.parent, context, shape)?; diff --git a/src/expr.rs b/src/expr.rs index 14a5ebd28ee..5b59a5114b5 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -590,7 +590,11 @@ impl Rewrite for ast::Stmt { } // Rewrite condition if the given expression has one. -pub fn rewrite_cond(context: &RewriteContext<'_>, expr: &ast::Expr, shape: Shape) -> Option { +pub fn rewrite_cond( + context: &RewriteContext<'_>, + expr: &ast::Expr, + shape: Shape, +) -> Option { match expr.node { ast::ExprKind::Match(ref cond, _) => { // `match `cond` {` @@ -1337,7 +1341,11 @@ pub fn is_every_expr_simple(lists: &[OverflowableItem<'_>]) -> bool { lists.iter().all(OverflowableItem::is_simple) } -pub fn can_be_overflowed_expr(context: &RewriteContext<'_>, expr: &ast::Expr, args_len: usize) -> bool { +pub fn can_be_overflowed_expr( + context: &RewriteContext<'_>, + expr: &ast::Expr, + args_len: usize, +) -> bool { match expr.node { ast::ExprKind::Match(..) => { (context.use_block_indent() && args_len == 1) diff --git a/src/items.rs b/src/items.rs index b272a42ba18..a18cba69d65 100644 --- a/src/items.rs +++ b/src/items.rs @@ -998,7 +998,11 @@ fn format_struct( } } -pub fn format_trait(context: &RewriteContext<'_>, item: &ast::Item, offset: Indent) -> Option { +pub fn format_trait( + context: &RewriteContext<'_>, + item: &ast::Item, + offset: Indent, +) -> Option { if let ast::ItemKind::Trait( is_auto, unsafety, @@ -1172,7 +1176,11 @@ pub fn format_trait_alias( rewrite_assign_rhs(context, lhs, generic_bounds, shape.sub_width(1)?).map(|s| s + ";") } -fn format_unit_struct(context: &RewriteContext<'_>, p: &StructParts<'_>, offset: Indent) -> Option { +fn format_unit_struct( + context: &RewriteContext<'_>, + p: &StructParts<'_>, + offset: Indent, +) -> Option { let header_str = format_header(context, p.prefix, p.ident, p.vis); let generics_str = if let Some(generics) = p.generics { let hi = if generics.where_clause.predicates.is_empty() { diff --git a/src/macros.rs b/src/macros.rs index 8243b6a9734..47985a4d6c0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1027,7 +1027,11 @@ fn wrap_macro_args_inner( // // We always try and format on one line. // FIXME: Use multi-line when every thing does not fit on one line. -fn format_macro_args(context: &RewriteContext<'_>, toks: TokenStream, shape: Shape) -> Option { +fn format_macro_args( + context: &RewriteContext<'_>, + toks: TokenStream, + shape: Shape, +) -> Option { if !context.config.format_macro_matchers() { let token_stream: TokenStream = toks.into(); let span = span_for_token_stream(&token_stream); @@ -1340,7 +1344,11 @@ impl MacroBranch { /// [pub] static ref NAME_N: TYPE_N = EXPR_N; /// } /// ``` -fn format_lazy_static(context: &RewriteContext<'_>, shape: Shape, ts: &TokenStream) -> Option { +fn format_lazy_static( + context: &RewriteContext<'_>, + shape: Shape, + ts: &TokenStream, +) -> Option { let mut result = String::with_capacity(1024); let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect()); let nested_shape = shape diff --git a/src/overflow.rs b/src/overflow.rs index 8f50f08ed2a..3bd57711be9 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -758,7 +758,10 @@ fn no_long_items(list: &[ListItem]) -> bool { } /// In case special-case style is required, returns an offset from which we start horizontal layout. -pub fn maybe_get_args_offset(callee_str: &str, args: &[OverflowableItem<'_>]) -> Option<(bool, usize)> { +pub fn maybe_get_args_offset( + callee_str: &str, + args: &[OverflowableItem<'_>], +) -> Option<(bool, usize)> { if let Some(&(_, num_args_before)) = args .get(0)? .whitelist() diff --git a/src/patterns.rs b/src/patterns.rs index 6114362e76f..956ce56bdf1 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -288,7 +288,11 @@ impl<'a> Spanned for TuplePatField<'a> { } } -pub fn can_be_overflowed_pat(context: &RewriteContext<'_>, pat: &TuplePatField<'_>, len: usize) -> bool { +pub fn can_be_overflowed_pat( + context: &RewriteContext<'_>, + pat: &TuplePatField<'_>, + len: usize, +) -> bool { match *pat { TuplePatField::Pat(pat) => match pat.node { ast::PatKind::Path(..) -- cgit 1.4.1-3-g733a5