mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
removing redundant closures in the whole project
This commit is contained in:
parent
b38c587b98
commit
16881390e1
14 changed files with 24 additions and 19 deletions
|
@ -47,7 +47,7 @@ impl Lint {
|
|||
name: name.to_lowercase(),
|
||||
group: group.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(),
|
||||
}
|
||||
}
|
||||
|
@ -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/`.
|
||||
WalkDir::new("../clippy_lints/src")
|
||||
.into_iter()
|
||||
.filter_map(|f| f.ok())
|
||||
.filter_map(std::result::Result::ok)
|
||||
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
|
||||
}
|
||||
|
||||
|
|
|
@ -326,7 +326,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
|
|||
lint.span,
|
||||
&format!("unknown clippy lint: clippy::{}", name),
|
||||
|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();
|
||||
match lint_store.check_lint_name(
|
||||
&name_lower,
|
||||
|
|
|
@ -53,7 +53,7 @@ fn is_empty_str(value: &Option<String>) -> bool {
|
|||
|
||||
fn is_empty_vec(value: &[String]) -> bool {
|
||||
// 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;
|
||||
|
|
|
@ -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
|
||||
for error in errors {
|
||||
|
|
|
@ -829,7 +829,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|||
|
||||
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<&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() {
|
||||
["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 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 {
|
||||
None
|
||||
}
|
||||
|
|
|
@ -241,7 +241,7 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
|
|||
// or too many chars differ (x_foo, y_boo) or (xfoo, yboo)
|
||||
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(
|
||||
|
|
|
@ -14,7 +14,7 @@ use toml;
|
|||
pub fn file_from_args(
|
||||
args: &[source_map::Spanned<ast::NestedMetaItemKind>],
|
||||
) -> 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" {
|
||||
return match arg.node {
|
||||
ast::MetaItemKind::Word | ast::MetaItemKind::List(_) => {
|
||||
|
|
|
@ -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> {
|
||||
let mut apb = AbsolutePathBuffer { names: vec![] };
|
||||
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.
|
||||
|
|
|
@ -9,8 +9,8 @@ macro_rules! get_version_info {
|
|||
let crate_name = String::from(env!("CARGO_PKG_NAME"));
|
||||
|
||||
let host_compiler = $crate::get_channel();
|
||||
let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string());
|
||||
let commit_date = option_env!("COMMIT_DATE").map(|s| s.to_string());
|
||||
let commit_hash = option_env!("GIT_HASH").map(str::to_string);
|
||||
let commit_date = option_env!("COMMIT_DATE").map(str::to_string);
|
||||
|
||||
VersionInfo {
|
||||
major,
|
||||
|
|
|
@ -47,7 +47,7 @@ fn arg_value<'a>(
|
|||
fn test_arg_value() {
|
||||
let args: Vec<_> = ["--bar=bar", "--foobar", "123", "--foo"]
|
||||
.iter()
|
||||
.map(|s| s.to_string())
|
||||
.map(std::string::ToString::to_string)
|
||||
.collect();
|
||||
|
||||
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 have_sys_root_arg = sys_root_arg.is_some();
|
||||
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(|| {
|
||||
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
|
||||
|
|
|
@ -32,7 +32,7 @@ fn explore_directory(dir: &Path) -> Vec<String> {
|
|||
let mut missing_files: Vec<String> = Vec::new();
|
||||
let mut current_file = String::new();
|
||||
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 {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#![allow(clippy::iter_cloned_collect)]
|
||||
#![allow(clippy::clone_on_copy)]
|
||||
#![allow(clippy::missing_docs_in_private_items)]
|
||||
#![allow(clippy::redundant_closure)]
|
||||
|
||||
fn main() {
|
||||
let _: Vec<i8> = vec![5_i8; 6].iter().cloned().collect();
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#![allow(clippy::iter_cloned_collect)]
|
||||
#![allow(clippy::clone_on_copy)]
|
||||
#![allow(clippy::missing_docs_in_private_items)]
|
||||
#![allow(clippy::redundant_closure)]
|
||||
|
||||
fn main() {
|
||||
let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
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();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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`
|
||||
|
||||
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();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![String::new()].iter().cloned()`
|
||||
|
||||
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();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![42, 43].iter().cloned()`
|
||||
|
||||
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());
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: Remove the map call
|
||||
|
|
Loading…
Reference in a new issue