removing redundant closures in the whole project

This commit is contained in:
Grzegorz 2019-02-10 13:35:44 +01:00
parent b38c587b98
commit 16881390e1
14 changed files with 24 additions and 19 deletions

View File

@ -47,7 +47,7 @@ impl Lint {
name: name.to_lowercase(), name: name.to_lowercase(),
group: group.to_string(), group: group.to_string(),
desc: NL_ESCAPE_RE.replace(&desc.replace("\\\"", "\""), "").to_string(), desc: NL_ESCAPE_RE.replace(&desc.replace("\\\"", "\""), "").to_string(),
deprecation: deprecation.map(|d| d.to_string()), deprecation: deprecation.map(std::string::ToString::to_string),
module: module.to_string(), module: module.to_string(),
} }
} }
@ -178,7 +178,7 @@ fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> {
// Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`. // Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`.
WalkDir::new("../clippy_lints/src") WalkDir::new("../clippy_lints/src")
.into_iter() .into_iter()
.filter_map(|f| f.ok()) .filter_map(std::result::Result::ok)
.filter(|f| f.path().extension() == Some(OsStr::new("rs"))) .filter(|f| f.path().extension() == Some(OsStr::new("rs")))
} }

View File

@ -326,7 +326,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
lint.span, lint.span,
&format!("unknown clippy lint: clippy::{}", name), &format!("unknown clippy lint: clippy::{}", name),
|db| { |db| {
if name.as_str().chars().any(|c| c.is_uppercase()) { if name.as_str().chars().any(char::is_uppercase) {
let name_lower = name.as_str().to_lowercase(); let name_lower = name.as_str().to_lowercase();
match lint_store.check_lint_name( match lint_store.check_lint_name(
&name_lower, &name_lower,

View File

@ -53,7 +53,7 @@ fn is_empty_str(value: &Option<String>) -> bool {
fn is_empty_vec(value: &[String]) -> bool { fn is_empty_vec(value: &[String]) -> bool {
// This works because empty iterators return true // This works because empty iterators return true
value.iter().all(|v| v.is_empty()) value.iter().all(std::string::String::is_empty)
} }
pub struct Pass; pub struct Pass;

View File

@ -267,7 +267,7 @@ pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
} }
}); });
let (conf, errors) = utils::conf::read(file_name.as_ref().map(|p| p.as_ref())); let (conf, errors) = utils::conf::read(file_name.as_ref().map(std::convert::AsRef::as_ref));
// all conf errors are non-fatal, we just use the default conf in case of error // all conf errors are non-fatal, we just use the default conf in case of error
for error in errors { for error in errors {

View File

@ -829,7 +829,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
let (method_names, arg_lists) = method_calls(expr, 2); let (method_names, arg_lists) = method_calls(expr, 2);
let method_names: Vec<LocalInternedString> = method_names.iter().map(|s| s.as_str()).collect(); let method_names: Vec<LocalInternedString> = method_names.iter().map(|s| s.as_str()).collect();
let method_names: Vec<&str> = method_names.iter().map(|s| s.as_ref()).collect(); let method_names: Vec<&str> = method_names.iter().map(std::convert::AsRef::as_ref).collect();
match method_names.as_slice() { match method_names.as_slice() {
["unwrap", "get"] => lint_get_unwrap(cx, expr, arg_lists[1], false), ["unwrap", "get"] => lint_get_unwrap(cx, expr, arg_lists[1], false),
@ -1695,7 +1695,7 @@ fn derefs_to_slice(cx: &LateContext<'_, '_>, expr: &hir::Expr, ty: Ty<'_>) -> Op
if let hir::ExprKind::MethodCall(ref path, _, ref args) = expr.node { if let hir::ExprKind::MethodCall(ref path, _, ref args) = expr.node {
if path.ident.name == "iter" && may_slice(cx, cx.tables.expr_ty(&args[0])) { if path.ident.name == "iter" && may_slice(cx, cx.tables.expr_ty(&args[0])) {
sugg::Sugg::hir_opt(cx, &args[0]).map(|sugg| sugg.addr()) sugg::Sugg::hir_opt(cx, &args[0]).map(sugg::Sugg::addr)
} else { } else {
None None
} }

View File

@ -241,7 +241,7 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
// or too many chars differ (x_foo, y_boo) or (xfoo, yboo) // or too many chars differ (x_foo, y_boo) or (xfoo, yboo)
continue; continue;
} }
split_at = interned_name.chars().next().map(|c| c.len_utf8()); split_at = interned_name.chars().next().map(char::len_utf8);
} }
} }
span_lint_and_then( span_lint_and_then(

View File

@ -14,7 +14,7 @@ use toml;
pub fn file_from_args( pub fn file_from_args(
args: &[source_map::Spanned<ast::NestedMetaItemKind>], args: &[source_map::Spanned<ast::NestedMetaItemKind>],
) -> Result<Option<path::PathBuf>, (&'static str, source_map::Span)> { ) -> Result<Option<path::PathBuf>, (&'static str, source_map::Span)> {
for arg in args.iter().filter_map(|a| a.meta_item()) { for arg in args.iter().filter_map(syntax::source_map::Spanned::meta_item) {
if arg.name() == "conf_file" { if arg.name() == "conf_file" {
return match arg.node { return match arg.node {
ast::MetaItemKind::Word | ast::MetaItemKind::List(_) => { ast::MetaItemKind::Word | ast::MetaItemKind::List(_) => {

View File

@ -142,7 +142,10 @@ pub fn match_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId, path: &[&str]) ->
pub fn get_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Vec<&'static str> { pub fn get_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Vec<&'static str> {
let mut apb = AbsolutePathBuffer { names: vec![] }; let mut apb = AbsolutePathBuffer { names: vec![] };
tcx.push_item_path(&mut apb, def_id, false); tcx.push_item_path(&mut apb, def_id, false);
apb.names.iter().map(|n| n.get()).collect() apb.names
.iter()
.map(syntax_pos::symbol::LocalInternedString::get)
.collect()
} }
/// Check if type is struct, enum or union type with given def path. /// Check if type is struct, enum or union type with given def path.

View File

@ -9,8 +9,8 @@ macro_rules! get_version_info {
let crate_name = String::from(env!("CARGO_PKG_NAME")); let crate_name = String::from(env!("CARGO_PKG_NAME"));
let host_compiler = $crate::get_channel(); let host_compiler = $crate::get_channel();
let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string()); let commit_hash = option_env!("GIT_HASH").map(str::to_string);
let commit_date = option_env!("COMMIT_DATE").map(|s| s.to_string()); let commit_date = option_env!("COMMIT_DATE").map(str::to_string);
VersionInfo { VersionInfo {
major, major,

View File

@ -47,7 +47,7 @@ fn arg_value<'a>(
fn test_arg_value() { fn test_arg_value() {
let args: Vec<_> = ["--bar=bar", "--foobar", "123", "--foo"] let args: Vec<_> = ["--bar=bar", "--foobar", "123", "--foo"]
.iter() .iter()
.map(|s| s.to_string()) .map(std::string::ToString::to_string)
.collect(); .collect();
assert_eq!(arg_value(None, "--foobar", |_| true), None); assert_eq!(arg_value(None, "--foobar", |_| true), None);
@ -84,7 +84,7 @@ pub fn main() {
let sys_root_arg = arg_value(&orig_args, "--sysroot", |_| true); let sys_root_arg = arg_value(&orig_args, "--sysroot", |_| true);
let have_sys_root_arg = sys_root_arg.is_some(); let have_sys_root_arg = sys_root_arg.is_some();
let sys_root = sys_root_arg let sys_root = sys_root_arg
.map(|s| s.to_string()) .map(std::string::ToString::to_string)
.or_else(|| std::env::var("SYSROOT").ok()) .or_else(|| std::env::var("SYSROOT").ok())
.or_else(|| { .or_else(|| {
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME")); let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));

View File

@ -32,7 +32,7 @@ fn explore_directory(dir: &Path) -> Vec<String> {
let mut missing_files: Vec<String> = Vec::new(); let mut missing_files: Vec<String> = Vec::new();
let mut current_file = String::new(); let mut current_file = String::new();
let mut files: Vec<DirEntry> = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect(); let mut files: Vec<DirEntry> = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect();
files.sort_by_key(|e| e.path()); files.sort_by_key(std::fs::DirEntry::path);
for entry in &files { for entry in &files {
let path = entry.path(); let path = entry.path();
if path.is_dir() { if path.is_dir() {

View File

@ -3,6 +3,7 @@
#![allow(clippy::iter_cloned_collect)] #![allow(clippy::iter_cloned_collect)]
#![allow(clippy::clone_on_copy)] #![allow(clippy::clone_on_copy)]
#![allow(clippy::missing_docs_in_private_items)] #![allow(clippy::missing_docs_in_private_items)]
#![allow(clippy::redundant_closure)]
fn main() { fn main() {
let _: Vec<i8> = vec![5_i8; 6].iter().cloned().collect(); let _: Vec<i8> = vec![5_i8; 6].iter().cloned().collect();

View File

@ -3,6 +3,7 @@
#![allow(clippy::iter_cloned_collect)] #![allow(clippy::iter_cloned_collect)]
#![allow(clippy::clone_on_copy)] #![allow(clippy::clone_on_copy)]
#![allow(clippy::missing_docs_in_private_items)] #![allow(clippy::missing_docs_in_private_items)]
#![allow(clippy::redundant_closure)]
fn main() { fn main() {
let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect(); let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();

View File

@ -1,5 +1,5 @@
error: You are using an explicit closure for cloning elements error: You are using an explicit closure for cloning elements
--> $DIR/map_clone.rs:8:22 --> $DIR/map_clone.rs:9:22
| |
LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect(); LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![5_i8; 6].iter().cloned()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![5_i8; 6].iter().cloned()`
@ -7,19 +7,19 @@ LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
= note: `-D clippy::map-clone` implied by `-D warnings` = note: `-D clippy::map-clone` implied by `-D warnings`
error: You are using an explicit closure for cloning elements error: You are using an explicit closure for cloning elements
--> $DIR/map_clone.rs:9:26 --> $DIR/map_clone.rs:10:26
| |
LL | let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect(); LL | let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![String::new()].iter().cloned()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![String::new()].iter().cloned()`
error: You are using an explicit closure for cloning elements error: You are using an explicit closure for cloning elements
--> $DIR/map_clone.rs:10:23 --> $DIR/map_clone.rs:11:23
| |
LL | let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect(); LL | let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![42, 43].iter().cloned()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![42, 43].iter().cloned()`
error: You are needlessly cloning iterator elements error: You are needlessly cloning iterator elements
--> $DIR/map_clone.rs:22:29 --> $DIR/map_clone.rs:23:29
| |
LL | let _ = std::env::args().map(|v| v.clone()); LL | let _ = std::env::args().map(|v| v.clone());
| ^^^^^^^^^^^^^^^^^^^ help: Remove the map call | ^^^^^^^^^^^^^^^^^^^ help: Remove the map call