Cargo clippy

This commit is contained in:
Seiichi Uchida 2017-12-12 13:48:12 +09:00
parent fc52a4d33f
commit 516f15aba1
9 changed files with 36 additions and 41 deletions

View File

@ -54,7 +54,7 @@ fn execute() -> i32 {
// If there is any invalid argument passed to `cargo fmt`, return without formatting.
let mut is_package_arg = false;
for arg in env::args().skip(2).take_while(|a| a != "--") {
if arg.starts_with("-") {
if arg.starts_with('-') {
is_package_arg = arg.starts_with("--package");
} else if !is_package_arg {
print_usage_to_stderr(&opts, &format!("Invalid argument: `{}`.", arg));
@ -215,7 +215,7 @@ impl CargoFmtStrategy {
}
}
/// Based on the specified CargoFmtStrategy, returns a set of main source files.
/// Based on the specified `CargoFmtStrategy`, returns a set of main source files.
fn get_targets(strategy: &CargoFmtStrategy) -> Result<HashSet<Target>, io::Error> {
let mut targets = HashSet::new();
@ -228,7 +228,7 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result<HashSet<Target>, io::Error
if targets.is_empty() {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Failed to find targets"),
"Failed to find targets".to_owned(),
))
} else {
Ok(targets)
@ -310,7 +310,7 @@ fn get_targets_with_hitlist(
fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut HashSet<Target>) {
for target in target_paths {
targets.insert(Target::from_target(&target));
targets.insert(Target::from_target(target));
}
}

View File

@ -930,7 +930,7 @@ pub fn recover_comment_removed(
context: &RewriteContext,
) -> Option<String> {
let snippet = context.snippet(span);
if snippet != new && changed_comment_content(&snippet, &new) {
if snippet != new && changed_comment_content(snippet, &new) {
// We missed some comments. Keep the original text.
Some(snippet.to_owned())
} else {

View File

@ -304,7 +304,7 @@ pub fn format_expr(
})
}
#[derive(new)]
#[derive(new, Clone, Copy)]
pub struct PairParts<'a> {
prefix: &'a str,
infix: &'a str,
@ -729,7 +729,7 @@ struct ControlFlow<'a> {
span: Span,
}
fn to_control_flow<'a>(expr: &'a ast::Expr, expr_type: ExprType) -> Option<ControlFlow<'a>> {
fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow> {
match expr.node {
ast::ExprKind::If(ref cond, ref if_block, ref else_block) => Some(ControlFlow::new_if(
cond,
@ -2122,16 +2122,12 @@ fn is_every_args_simple<T: ToExpr>(lists: &[&T]) -> bool {
/// In case special-case style is required, returns an offset from which we start horizontal layout.
fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<usize> {
if FORMAT_LIKE_WHITELIST
.iter()
.find(|s| **s == callee_str)
.is_some() && args.len() >= 1 && is_every_args_simple(args)
if FORMAT_LIKE_WHITELIST.iter().any(|s| *s == callee_str) && args.len() >= 1
&& is_every_args_simple(args)
{
Some(1)
} else if WRITE_LIKE_WHITELIST
.iter()
.find(|s| **s == callee_str)
.is_some() && args.len() >= 2 && is_every_args_simple(args)
} else if WRITE_LIKE_WHITELIST.iter().any(|s| *s == callee_str) && args.len() >= 2
&& is_every_args_simple(args)
{
Some(2)
} else {

View File

@ -45,7 +45,7 @@ where
}
// Prints all newlines either as `\n` or as `\r\n`.
pub fn write_system_newlines<T>(writer: T, text: &String, config: &Config) -> Result<(), io::Error>
pub fn write_system_newlines<T>(writer: T, text: &str, config: &Config) -> Result<(), io::Error>
where
T: Write,
{
@ -79,7 +79,7 @@ where
}
pub fn write_file<T>(
text: &String,
text: &str,
filename: &str,
out: &mut T,
config: &Config,
@ -88,7 +88,7 @@ where
T: Write,
{
fn source_and_formatted_text(
text: &String,
text: &str,
filename: &str,
config: &Config,
) -> Result<(String, String), io::Error> {
@ -103,7 +103,7 @@ where
fn create_diff(
filename: &str,
text: &String,
text: &str,
config: &Config,
) -> Result<Vec<Mismatch>, io::Error> {
let (ori, fmt) = source_and_formatted_text(text, filename, config)?;

View File

@ -108,10 +108,10 @@ fn compare_use_trees(a: &ast::UseTree, b: &ast::UseTree, nested: bool) -> Orderi
fn compare_use_items(context: &RewriteContext, a: &ast::Item, b: &ast::Item) -> Option<Ordering> {
match (&a.node, &b.node) {
(&ast::ItemKind::Use(ref a_tree), &ast::ItemKind::Use(ref b_tree)) => {
Some(compare_use_trees(&a_tree, &b_tree, false))
Some(compare_use_trees(a_tree, b_tree, false))
}
(&ast::ItemKind::ExternCrate(..), &ast::ItemKind::ExternCrate(..)) => {
Some(context.snippet(a.span).cmp(&context.snippet(b.span)))
Some(context.snippet(a.span).cmp(context.snippet(b.span)))
}
_ => None,
}
@ -141,7 +141,7 @@ impl Rewrite for ast::UseTree {
ast::UseTreeKind::Glob => {
let prefix_shape = shape.sub_width(3)?;
if self.prefix.segments.len() > 0 {
if !self.prefix.segments.is_empty() {
let path_str = rewrite_prefix(&self.prefix, context, prefix_shape)?;
Some(format!("{}::*", path_str))
} else {
@ -476,7 +476,7 @@ fn rewrite_nested_use_tree(
let mut items = vec![ListItem::from_str("")];
let iter = itemize_list(
context.codemap,
trees.iter().map(|ref tree| &tree.0),
trees.iter().map(|tree| &tree.0),
"}",
",",
|tree| tree.span.lo(),

View File

@ -239,8 +239,8 @@ impl<'a> FnSig<'a> {
}
impl<'a> FmtVisitor<'a> {
fn format_item(&mut self, item: Item) {
self.push_str(&item.abi);
fn format_item(&mut self, item: &Item) {
self.buffer.push_str(&item.abi);
let snippet = self.snippet(item.span);
let brace_pos = snippet.find_uncommented("{").unwrap();
@ -279,7 +279,7 @@ impl<'a> FmtVisitor<'a> {
pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
let item = Item::from_foreign_mod(fm, span, self.config);
self.format_item(item);
self.format_item(&item);
}
fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
@ -950,7 +950,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
.span_after(item.span, &format!("{}", item.ident));
let bound_hi = type_param_bounds.last().unwrap().span().hi();
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
if contains_comment(&snippet) {
if contains_comment(snippet) {
return None;
}
}
@ -1175,7 +1175,7 @@ pub fn format_struct_struct(
result.push('\n');
result.push_str(&offset.to_string(context.config));
} else {
result.push_str(&snippet);
result.push_str(snippet);
}
result.push('}');
return Some(result);
@ -1307,7 +1307,7 @@ fn format_tuple_struct(
result.push('\n');
result.push_str(&offset.to_string(context.config));
} else {
result.push_str(&snippet);
result.push_str(snippet);
}
result.push(')');
} else {
@ -2718,7 +2718,7 @@ fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> S
format!("{}{}{}", format_visibility(vis), item_name, ident)
}
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Clone, Copy)]
enum BracePos {
None,
Auto,

View File

@ -144,7 +144,7 @@ pub fn rewrite_macro(
};
let ts: TokenStream = mac.node.stream();
if ts.is_empty() && !contains_comment(&context.snippet(mac.span)) {
if ts.is_empty() && !contains_comment(context.snippet(mac.span)) {
return match style {
MacroStyle::Parens if position == MacroPosition::Item => {
Some(format!("{}();", macro_name))

View File

@ -1,5 +1,5 @@
#[must_use]
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, Copy)]
pub struct Summary {
// Encountered e.g. an IO error.
has_operational_errors: bool,

View File

@ -166,7 +166,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.last_pos,
attr_lo.unwrap_or(first_stmt.span.lo()),
));
let len = CommentCodeSlices::new(&snippet)
let len = CommentCodeSlices::new(snippet)
.nth(0)
.and_then(|(kind, _, s)| {
if kind == CodeCharKind::Normal {
@ -212,7 +212,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
stmt.span.hi(),
source!(self, b.span).hi() - brace_compensation,
));
let len = CommentCodeSlices::new(&snippet)
let len = CommentCodeSlices::new(snippet)
.last()
.and_then(|(kind, _, s)| {
if kind == CodeCharKind::Normal && s.trim().is_empty() {
@ -430,7 +430,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
}
ast::ItemKind::MacroDef(..) => {
// FIXME(#1539): macros 2.0
let mac_snippet = Some(remove_trailing_white_spaces(&self.snippet(item.span)));
let mac_snippet = Some(remove_trailing_white_spaces(self.snippet(item.span)));
self.push_rewrite(item.span, mac_snippet);
}
}
@ -684,9 +684,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
// Extract leading `use ...;`.
let items: Vec<_> = stmts
.iter()
.take_while(|stmt| to_stmt_item(stmt).is_some())
.take_while(|stmt| to_stmt_item(stmt).map_or(false, is_use_item))
.filter_map(|stmt| to_stmt_item(stmt))
.take_while(|item| is_use_item(item))
.collect();
if items.is_empty() {
@ -779,7 +778,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
use_block: false,
is_if_else_block: false,
force_one_line_chain: false,
snippet_provider: &self.snippet_provider,
snippet_provider: self.snippet_provider,
}
}
}
@ -888,7 +887,7 @@ where
// Extract comments between two attributes.
let span_between_attr = mk_sp(attr.span.hi(), next_attr.span.lo());
let snippet = context.snippet(span_between_attr);
if count_newlines(&snippet) >= 2 || snippet.contains('/') {
if count_newlines(snippet) >= 2 || snippet.contains('/') {
break;
}
}
@ -974,7 +973,7 @@ impl<'a> Rewrite for [ast::Attribute] {
// Preserve an empty line before/after doc comments.
if self[0].is_sugared_doc || self[first_group_len].is_sugared_doc {
let snippet = context.snippet(missing_span);
let (mla, mlb) = has_newlines_before_after_comment(&snippet);
let (mla, mlb) = has_newlines_before_after_comment(snippet);
let comment = ::comment::recover_missing_comment_in_span(
missing_span,
shape.with_max_width(context.config),
@ -1068,7 +1067,7 @@ fn get_derive_args<'a>(context: &'a RewriteContext, attr: &ast::Attribute) -> Op
pub fn rewrite_extern_crate(context: &RewriteContext, item: &ast::Item) -> Option<String> {
assert!(is_extern_crate(item));
let new_str = context.snippet(item.span);
Some(if contains_comment(&new_str) {
Some(if contains_comment(new_str) {
new_str.to_owned()
} else {
let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");