Refactoring

1. Cargo clippy
2. Run 'cargo fmt' with import reordering options set to `true`.
3. Factor out `rewrite_lifetime_param()`.
This commit is contained in:
Seiichi Uchida 2017-12-25 00:13:59 +09:00
parent 939a6c5820
commit 4604fea0a0
4 changed files with 31 additions and 54 deletions

View File

@ -17,15 +17,15 @@
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<ExitStatus, io::Error>, 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<ExitStatus, io::Error>, opts: &getopts::
}
fn get_version(verbosity: Verbosity) -> Result<ExitStatus, io::Error> {
run_rustfmt(&vec![], &vec![String::from("--version")], verbosity)
run_rustfmt(&[], &[String::from("--version")], verbosity)
}
fn format_crate(

View File

@ -24,8 +24,8 @@
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<error::Error + Send + Sync>;
type FmtResult<T> = std::result::Result<T, FmtError>;

View File

@ -422,21 +422,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
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::<Option<Vec<_>>>()?
.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 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
impl Rewrite for ast::PolyTraitRef {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
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::<Option<Vec<_>>>()?
.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<String> {
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::<Option<Vec<_>>>()?
.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<String> {
let result = generic_params
.iter()
.filter(|p| p.is_lifetime_param())
.map(|lt| lt.rewrite(context, shape))
.collect::<Option<Vec<_>>>()?
.join(", ");
if result.is_empty() {
None
} else {
Some(result)
}
}

View File

@ -24,8 +24,8 @@
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;