Skip to content

Commit 4604fea

Browse files
committed
Refactoring
1. Cargo clippy 2. Run 'cargo fmt' with import reordering options set to `true`. 3. Factor out `rewrite_lifetime_param()`.
1 parent 939a6c5 commit 4604fea

File tree

4 files changed

+31
-54
lines changed

4 files changed

+31
-54
lines changed

src/bin/cargo-fmt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ extern crate cargo_metadata;
1717
extern crate getopts;
1818
extern crate serde_json as json;
1919

20+
use std::collections::HashSet;
2021
use std::env;
2122
use std::fs;
2223
use std::hash::{Hash, Hasher};
2324
use std::io::{self, Write};
25+
use std::iter::FromIterator;
2426
use std::path::{Path, PathBuf};
2527
use std::process::{Command, ExitStatus};
2628
use std::str;
27-
use std::collections::HashSet;
28-
use std::iter::FromIterator;
2929

3030
use getopts::{Matches, Options};
3131

@@ -125,7 +125,7 @@ pub enum Verbosity {
125125
fn handle_command_status(status: Result<ExitStatus, io::Error>, opts: &getopts::Options) -> i32 {
126126
match status {
127127
Err(e) => {
128-
print_usage_to_stderr(&opts, &e.to_string());
128+
print_usage_to_stderr(opts, &e.to_string());
129129
FAILURE
130130
}
131131
Ok(status) => {
@@ -139,7 +139,7 @@ fn handle_command_status(status: Result<ExitStatus, io::Error>, opts: &getopts::
139139
}
140140

141141
fn get_version(verbosity: Verbosity) -> Result<ExitStatus, io::Error> {
142-
run_rustfmt(&vec![], &vec![String::from("--version")], verbosity)
142+
run_rustfmt(&[], &[String::from("--version")], verbosity)
143143
}
144144

145145
fn format_crate(

src/bin/rustfmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use std::str::FromStr;
2424
use getopts::{Matches, Options};
2525

2626
use rustfmt::{run, FileName, Input, Summary};
27-
use rustfmt::file_lines::FileLines;
2827
use rustfmt::config::{get_toml_path, Color, Config, WriteMode};
28+
use rustfmt::file_lines::FileLines;
2929

3030
type FmtError = Box<error::Error + Send + Sync>;
3131
type FmtResult<T> = std::result::Result<T, FmtError>;

src/types.rs

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -422,21 +422,9 @@ impl Rewrite for ast::WherePredicate {
422422

423423
let colon = type_bound_colon(context);
424424

425-
if bound_generic_params
426-
.iter()
427-
.filter(|p| p.is_lifetime_param())
428-
.count() > 0
425+
if let Some(lifetime_str) =
426+
rewrite_lifetime_param(context, shape, bound_generic_params)
429427
{
430-
let lifetime_str: String = bound_generic_params
431-
.iter()
432-
.filter_map(|p| match p {
433-
&ast::GenericParam::Lifetime(ref l) => Some(l),
434-
_ => None,
435-
})
436-
.map(|lt| lt.rewrite(context, shape))
437-
.collect::<Option<Vec<_>>>()?
438-
.join(", ");
439-
440428
// 6 = "for<> ".len()
441429
let used_width = lifetime_str.len() + type_str.len() + colon.len() + 6;
442430
let ty_shape = shape.offset_left(used_width)?;
@@ -598,21 +586,9 @@ impl Rewrite for ast::TyParam {
598586

599587
impl Rewrite for ast::PolyTraitRef {
600588
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
601-
if self.bound_generic_params
602-
.iter()
603-
.filter(|p| p.is_lifetime_param())
604-
.count() > 0
589+
if let Some(lifetime_str) =
590+
rewrite_lifetime_param(context, shape, &self.bound_generic_params)
605591
{
606-
let lifetime_str: String = self.bound_generic_params
607-
.iter()
608-
.filter_map(|p| match p {
609-
&ast::GenericParam::Lifetime(ref l) => Some(l),
610-
_ => None,
611-
})
612-
.map(|lt| lt.rewrite(context, shape))
613-
.collect::<Option<Vec<_>>>()?
614-
.join(", ");
615-
616592
// 6 is "for<> ".len()
617593
let extra_offset = lifetime_str.len() + 6;
618594
let path_str = self.trait_ref
@@ -762,31 +738,13 @@ fn rewrite_bare_fn(
762738
) -> Option<String> {
763739
let mut result = String::with_capacity(128);
764740

765-
if bare_fn
766-
.generic_params
767-
.iter()
768-
.filter(|p| p.is_lifetime_param())
769-
.count() > 0
741+
if let Some(ref lifetime_str) = rewrite_lifetime_param(context, shape, &bare_fn.generic_params)
770742
{
771743
result.push_str("for<");
772744
// 6 = "for<> ".len(), 4 = "for<".
773745
// This doesn't work out so nicely for mutliline situation with lots of
774746
// rightward drift. If that is a problem, we could use the list stuff.
775-
result.push_str(&bare_fn
776-
.generic_params
777-
.iter()
778-
.filter_map(|p| match p {
779-
&ast::GenericParam::Lifetime(ref l) => Some(l),
780-
_ => None,
781-
})
782-
.map(|l| {
783-
l.rewrite(
784-
context,
785-
Shape::legacy(shape.width.checked_sub(6)?, shape.indent + 4),
786-
)
787-
})
788-
.collect::<Option<Vec<_>>>()?
789-
.join(", "));
747+
result.push_str(lifetime_str);
790748
result.push_str("> ");
791749
}
792750

@@ -841,3 +799,22 @@ pub fn can_be_overflowed_type(context: &RewriteContext, ty: &ast::Ty, len: usize
841799
_ => false,
842800
}
843801
}
802+
803+
/// Returns `None` if there is no `LifetimeDef` in the given generic parameters.
804+
fn rewrite_lifetime_param(
805+
context: &RewriteContext,
806+
shape: Shape,
807+
generic_params: &[ast::GenericParam],
808+
) -> Option<String> {
809+
let result = generic_params
810+
.iter()
811+
.filter(|p| p.is_lifetime_param())
812+
.map(|lt| lt.rewrite(context, shape))
813+
.collect::<Option<Vec<_>>>()?
814+
.join(", ");
815+
if result.is_empty() {
816+
None
817+
} else {
818+
Some(result)
819+
}
820+
}

tests/system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use std::path::{Path, PathBuf};
2424
use std::str::Chars;
2525

2626
use rustfmt::*;
27-
use rustfmt::filemap::{write_system_newlines, FileMap};
2827
use rustfmt::config::{Color, Config, ReportTactic};
28+
use rustfmt::filemap::{write_system_newlines, FileMap};
2929
use rustfmt::rustfmt_diff::*;
3030

3131
const DIFF_CONTEXT_SIZE: usize = 3;

0 commit comments

Comments
 (0)