Skip to content

Commit 1688139

Browse files
author
Grzegorz
committed
removing redundant closures in the whole project
1 parent b38c587 commit 1688139

File tree

14 files changed

+24
-19
lines changed

14 files changed

+24
-19
lines changed

clippy_dev/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Lint {
4747
name: name.to_lowercase(),
4848
group: group.to_string(),
4949
desc: NL_ESCAPE_RE.replace(&desc.replace("\\\"", "\""), "").to_string(),
50-
deprecation: deprecation.map(|d| d.to_string()),
50+
deprecation: deprecation.map(std::string::ToString::to_string),
5151
module: module.to_string(),
5252
}
5353
}
@@ -178,7 +178,7 @@ fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> {
178178
// Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`.
179179
WalkDir::new("../clippy_lints/src")
180180
.into_iter()
181-
.filter_map(|f| f.ok())
181+
.filter_map(std::result::Result::ok)
182182
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
183183
}
184184

clippy_lints/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
326326
lint.span,
327327
&format!("unknown clippy lint: clippy::{}", name),
328328
|db| {
329-
if name.as_str().chars().any(|c| c.is_uppercase()) {
329+
if name.as_str().chars().any(char::is_uppercase) {
330330
let name_lower = name.as_str().to_lowercase();
331331
match lint_store.check_lint_name(
332332
&name_lower,

clippy_lints/src/cargo_common_metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn is_empty_str(value: &Option<String>) -> bool {
5353

5454
fn is_empty_vec(value: &[String]) -> bool {
5555
// This works because empty iterators return true
56-
value.iter().all(|v| v.is_empty())
56+
value.iter().all(std::string::String::is_empty)
5757
}
5858

5959
pub struct Pass;

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
267267
}
268268
});
269269

270-
let (conf, errors) = utils::conf::read(file_name.as_ref().map(|p| p.as_ref()));
270+
let (conf, errors) = utils::conf::read(file_name.as_ref().map(std::convert::AsRef::as_ref));
271271

272272
// all conf errors are non-fatal, we just use the default conf in case of error
273273
for error in errors {

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
829829

830830
let (method_names, arg_lists) = method_calls(expr, 2);
831831
let method_names: Vec<LocalInternedString> = method_names.iter().map(|s| s.as_str()).collect();
832-
let method_names: Vec<&str> = method_names.iter().map(|s| s.as_ref()).collect();
832+
let method_names: Vec<&str> = method_names.iter().map(std::convert::AsRef::as_ref).collect();
833833

834834
match method_names.as_slice() {
835835
["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
16951695

16961696
if let hir::ExprKind::MethodCall(ref path, _, ref args) = expr.node {
16971697
if path.ident.name == "iter" && may_slice(cx, cx.tables.expr_ty(&args[0])) {
1698-
sugg::Sugg::hir_opt(cx, &args[0]).map(|sugg| sugg.addr())
1698+
sugg::Sugg::hir_opt(cx, &args[0]).map(sugg::Sugg::addr)
16991699
} else {
17001700
None
17011701
}

clippy_lints/src/non_expressive_names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
241241
// or too many chars differ (x_foo, y_boo) or (xfoo, yboo)
242242
continue;
243243
}
244-
split_at = interned_name.chars().next().map(|c| c.len_utf8());
244+
split_at = interned_name.chars().next().map(char::len_utf8);
245245
}
246246
}
247247
span_lint_and_then(

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use toml;
1414
pub fn file_from_args(
1515
args: &[source_map::Spanned<ast::NestedMetaItemKind>],
1616
) -> Result<Option<path::PathBuf>, (&'static str, source_map::Span)> {
17-
for arg in args.iter().filter_map(|a| a.meta_item()) {
17+
for arg in args.iter().filter_map(syntax::source_map::Spanned::meta_item) {
1818
if arg.name() == "conf_file" {
1919
return match arg.node {
2020
ast::MetaItemKind::Word | ast::MetaItemKind::List(_) => {

clippy_lints/src/utils/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ pub fn match_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId, path: &[&str]) ->
142142
pub fn get_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Vec<&'static str> {
143143
let mut apb = AbsolutePathBuffer { names: vec![] };
144144
tcx.push_item_path(&mut apb, def_id, false);
145-
apb.names.iter().map(|n| n.get()).collect()
145+
apb.names
146+
.iter()
147+
.map(syntax_pos::symbol::LocalInternedString::get)
148+
.collect()
146149
}
147150

148151
/// Check if type is struct, enum or union type with given def path.

rustc_tools_util/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ macro_rules! get_version_info {
99
let crate_name = String::from(env!("CARGO_PKG_NAME"));
1010

1111
let host_compiler = $crate::get_channel();
12-
let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string());
13-
let commit_date = option_env!("COMMIT_DATE").map(|s| s.to_string());
12+
let commit_hash = option_env!("GIT_HASH").map(str::to_string);
13+
let commit_date = option_env!("COMMIT_DATE").map(str::to_string);
1414

1515
VersionInfo {
1616
major,

src/driver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn arg_value<'a>(
4747
fn test_arg_value() {
4848
let args: Vec<_> = ["--bar=bar", "--foobar", "123", "--foo"]
4949
.iter()
50-
.map(|s| s.to_string())
50+
.map(std::string::ToString::to_string)
5151
.collect();
5252

5353
assert_eq!(arg_value(None, "--foobar", |_| true), None);
@@ -84,7 +84,7 @@ pub fn main() {
8484
let sys_root_arg = arg_value(&orig_args, "--sysroot", |_| true);
8585
let have_sys_root_arg = sys_root_arg.is_some();
8686
let sys_root = sys_root_arg
87-
.map(|s| s.to_string())
87+
.map(std::string::ToString::to_string)
8888
.or_else(|| std::env::var("SYSROOT").ok())
8989
.or_else(|| {
9090
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));

tests/missing-test-files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn explore_directory(dir: &Path) -> Vec<String> {
3232
let mut missing_files: Vec<String> = Vec::new();
3333
let mut current_file = String::new();
3434
let mut files: Vec<DirEntry> = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect();
35-
files.sort_by_key(|e| e.path());
35+
files.sort_by_key(std::fs::DirEntry::path);
3636
for entry in &files {
3737
let path = entry.path();
3838
if path.is_dir() {

tests/ui/map_clone.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::iter_cloned_collect)]
44
#![allow(clippy::clone_on_copy)]
55
#![allow(clippy::missing_docs_in_private_items)]
6+
#![allow(clippy::redundant_closure)]
67

78
fn main() {
89
let _: Vec<i8> = vec![5_i8; 6].iter().cloned().collect();

tests/ui/map_clone.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::iter_cloned_collect)]
44
#![allow(clippy::clone_on_copy)]
55
#![allow(clippy::missing_docs_in_private_items)]
6+
#![allow(clippy::redundant_closure)]
67

78
fn main() {
89
let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();

tests/ui/map_clone.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error: You are using an explicit closure for cloning elements
2-
--> $DIR/map_clone.rs:8:22
2+
--> $DIR/map_clone.rs:9:22
33
|
44
LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![5_i8; 6].iter().cloned()`
66
|
77
= note: `-D clippy::map-clone` implied by `-D warnings`
88

99
error: You are using an explicit closure for cloning elements
10-
--> $DIR/map_clone.rs:9:26
10+
--> $DIR/map_clone.rs:10:26
1111
|
1212
LL | let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![String::new()].iter().cloned()`
1414

1515
error: You are using an explicit closure for cloning elements
16-
--> $DIR/map_clone.rs:10:23
16+
--> $DIR/map_clone.rs:11:23
1717
|
1818
LL | let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![42, 43].iter().cloned()`
2020

2121
error: You are needlessly cloning iterator elements
22-
--> $DIR/map_clone.rs:22:29
22+
--> $DIR/map_clone.rs:23:29
2323
|
2424
LL | let _ = std::env::args().map(|v| v.clone());
2525
| ^^^^^^^^^^^^^^^^^^^ help: Remove the map call

0 commit comments

Comments
 (0)