From 516f15aba154e2cc66b999d0c9613479f4d53275 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Tue, 12 Dec 2017 13:48:12 +0900 Subject: [PATCH] Cargo clippy --- src/bin/cargo-fmt.rs | 8 ++++---- src/comment.rs | 2 +- src/expr.rs | 16 ++++++---------- src/filemap.rs | 8 ++++---- src/imports.rs | 8 ++++---- src/items.rs | 14 +++++++------- src/macros.rs | 2 +- src/summary.rs | 2 +- src/visitor.rs | 17 ++++++++--------- 9 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index 63d7d2c3353..3916b52fe5f 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -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 @@ pub fn from_matches(matches: &Matches) -> 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, io::Error> { let mut targets = HashSet::new(); @@ -228,7 +228,7 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result, 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) { for target in target_paths { - targets.insert(Target::from_target(&target)); + targets.insert(Target::from_target(target)); } } diff --git a/src/comment.rs b/src/comment.rs index a9fc31ea0b0..439a7f6a05f 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -930,7 +930,7 @@ pub fn recover_comment_removed( context: &RewriteContext, ) -> Option { 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 { diff --git a/src/expr.rs b/src/expr.rs index 4fe97a2cb2b..8ef55b1716c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -304,7 +304,7 @@ fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool { }) } -#[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> { +fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option { 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(lists: &[&T]) -> bool { /// In case special-case style is required, returns an offset from which we start horizontal layout. fn maybe_get_args_offset(callee_str: &str, args: &[&T]) -> Option { - 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 { diff --git a/src/filemap.rs b/src/filemap.rs index 68e06f5c9ac..c7fa5e2f1ef 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -45,7 +45,7 @@ pub fn write_all_files( } // Prints all newlines either as `\n` or as `\r\n`. -pub fn write_system_newlines(writer: T, text: &String, config: &Config) -> Result<(), io::Error> +pub fn write_system_newlines(writer: T, text: &str, config: &Config) -> Result<(), io::Error> where T: Write, { @@ -79,7 +79,7 @@ pub fn write_system_newlines(writer: T, text: &String, config: &Config) -> Re } pub fn write_file( - text: &String, + text: &str, filename: &str, out: &mut T, config: &Config, @@ -88,7 +88,7 @@ pub fn write_file( T: Write, { fn source_and_formatted_text( - text: &String, + text: &str, filename: &str, config: &Config, ) -> Result<(String, String), io::Error> { @@ -103,7 +103,7 @@ fn source_and_formatted_text( fn create_diff( filename: &str, - text: &String, + text: &str, config: &Config, ) -> Result, io::Error> { let (ori, fmt) = source_and_formatted_text(text, filename, config)?; diff --git a/src/imports.rs b/src/imports.rs index b8a7e3bb6ba..53ccd67b792 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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 { 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 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { 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(), diff --git a/src/items.rs b/src/items.rs index bff5c00d2f4..7771344f3db 100644 --- a/src/items.rs +++ b/src/items.rs @@ -239,8 +239,8 @@ fn to_str(&self, context: &RewriteContext) -> String { } 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 @@ fn format_body_element(&mut self, element: &BodyElement) { 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, diff --git a/src/macros.rs b/src/macros.rs index ce0616d9ebf..9c32e7f62be 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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)) diff --git a/src/summary.rs b/src/summary.rs index cf034e7dff6..73ee80dac85 100644 --- a/src/summary.rs +++ b/src/summary.rs @@ -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, diff --git a/src/visitor.rs b/src/visitor.rs index a7c33d2697c..8f6cab0e2df 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -166,7 +166,7 @@ pub fn visit_block( 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 @@ pub fn visit_block( 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 @@ pub fn visit_item(&mut self, item: &ast::Item) { } 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 @@ fn to_stmt_item(stmt: &ast::Stmt) -> Option<&ast::Item> { // 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 @@ pub fn get_context(&self) -> RewriteContext { 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 @@ fn take_while_with_pred<'a, P>( // 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 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { // 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 { 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::>().join(" "); -- 2.44.0