Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
1. Cargo clippy
2. Run 'cargo fmt' with import reordering options set to `true`.
3. Factor out `rewrite_lifetime_param()`.
  • Loading branch information
topecongiro committed Dec 24, 2017
1 parent 939a6c5 commit 4604fea
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 54 deletions.
8 changes: 4 additions & 4 deletions src/bin/cargo-fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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) => {
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/bin/rustfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<error::Error + Send + Sync>;
type FmtResult<T> = std::result::Result<T, FmtError>;
Expand Down
73 changes: 25 additions & 48 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<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)?;
Expand Down Expand Up @@ -598,21 +586,9 @@ impl Rewrite for ast::TyParam {

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
Expand Down Expand Up @@ -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("> ");
}

Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4604fea

Please sign in to comment.