diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index 270ed1446d6..180c6f0abf3 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -17,15 +17,15 @@ extern crate cargo_metadata; extern crate getopts; extern crate serde_json as json; +use std::collections::HashSet; use std::env; use std::fs; use std::hash::{Hash, Hasher}; use std::io::{self, Write}; +use std::iter::FromIterator; use std::path::{Path, PathBuf}; use std::process::{Command, ExitStatus}; use std::str; -use std::collections::HashSet; -use std::iter::FromIterator; use getopts::{Matches, Options}; @@ -125,7 +125,7 @@ pub enum Verbosity { fn handle_command_status(status: Result, opts: &getopts::Options) -> i32 { match status { Err(e) => { - print_usage_to_stderr(&opts, &e.to_string()); + print_usage_to_stderr(opts, &e.to_string()); FAILURE } Ok(status) => { @@ -139,7 +139,7 @@ fn handle_command_status(status: Result, opts: &getopts:: } fn get_version(verbosity: Verbosity) -> Result { - run_rustfmt(&vec![], &vec![String::from("--version")], verbosity) + run_rustfmt(&[], &[String::from("--version")], verbosity) } fn format_crate( diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 0e48ab7c23a..ba586e1fd91 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -24,8 +24,8 @@ use std::str::FromStr; use getopts::{Matches, Options}; use rustfmt::{run, FileName, Input, Summary}; -use rustfmt::file_lines::FileLines; use rustfmt::config::{get_toml_path, Color, Config, WriteMode}; +use rustfmt::file_lines::FileLines; type FmtError = Box; type FmtResult = std::result::Result; diff --git a/src/types.rs b/src/types.rs index 77dffd3cd89..588d9100fe6 100644 --- a/src/types.rs +++ b/src/types.rs @@ -422,21 +422,9 @@ impl Rewrite for ast::WherePredicate { let colon = type_bound_colon(context); - if bound_generic_params - .iter() - .filter(|p| p.is_lifetime_param()) - .count() > 0 + if let Some(lifetime_str) = + rewrite_lifetime_param(context, shape, bound_generic_params) { - let lifetime_str: String = bound_generic_params - .iter() - .filter_map(|p| match p { - &ast::GenericParam::Lifetime(ref l) => Some(l), - _ => None, - }) - .map(|lt| lt.rewrite(context, shape)) - .collect::>>()? - .join(", "); - // 6 = "for<> ".len() let used_width = lifetime_str.len() + type_str.len() + colon.len() + 6; let ty_shape = shape.offset_left(used_width)?; @@ -598,21 +586,9 @@ impl Rewrite for ast::TyParam { impl Rewrite for ast::PolyTraitRef { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - if self.bound_generic_params - .iter() - .filter(|p| p.is_lifetime_param()) - .count() > 0 + if let Some(lifetime_str) = + rewrite_lifetime_param(context, shape, &self.bound_generic_params) { - let lifetime_str: String = self.bound_generic_params - .iter() - .filter_map(|p| match p { - &ast::GenericParam::Lifetime(ref l) => Some(l), - _ => None, - }) - .map(|lt| lt.rewrite(context, shape)) - .collect::>>()? - .join(", "); - // 6 is "for<> ".len() let extra_offset = lifetime_str.len() + 6; let path_str = self.trait_ref @@ -762,31 +738,13 @@ fn rewrite_bare_fn( ) -> Option { let mut result = String::with_capacity(128); - if bare_fn - .generic_params - .iter() - .filter(|p| p.is_lifetime_param()) - .count() > 0 + if let Some(ref lifetime_str) = rewrite_lifetime_param(context, shape, &bare_fn.generic_params) { result.push_str("for<"); // 6 = "for<> ".len(), 4 = "for<". // This doesn't work out so nicely for mutliline situation with lots of // rightward drift. If that is a problem, we could use the list stuff. - result.push_str(&bare_fn - .generic_params - .iter() - .filter_map(|p| match p { - &ast::GenericParam::Lifetime(ref l) => Some(l), - _ => None, - }) - .map(|l| { - l.rewrite( - context, - Shape::legacy(shape.width.checked_sub(6)?, shape.indent + 4), - ) - }) - .collect::>>()? - .join(", ")); + result.push_str(lifetime_str); result.push_str("> "); } @@ -841,3 +799,22 @@ pub fn can_be_overflowed_type(context: &RewriteContext, ty: &ast::Ty, len: usize _ => false, } } + +/// Returns `None` if there is no `LifetimeDef` in the given generic parameters. +fn rewrite_lifetime_param( + context: &RewriteContext, + shape: Shape, + generic_params: &[ast::GenericParam], +) -> Option { + let result = generic_params + .iter() + .filter(|p| p.is_lifetime_param()) + .map(|lt| lt.rewrite(context, shape)) + .collect::>>()? + .join(", "); + if result.is_empty() { + None + } else { + Some(result) + } +} diff --git a/tests/system.rs b/tests/system.rs index c1d838169ae..4beb616dd3c 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -24,8 +24,8 @@ use std::path::{Path, PathBuf}; use std::str::Chars; use rustfmt::*; -use rustfmt::filemap::{write_system_newlines, FileMap}; use rustfmt::config::{Color, Config, ReportTactic}; +use rustfmt::filemap::{write_system_newlines, FileMap}; use rustfmt::rustfmt_diff::*; const DIFF_CONTEXT_SIZE: usize = 3;