Skip to content

Commit 6b32f65

Browse files
committed
Auto merge of #4104 - Manishearth:beta-backports, r=oli-obk
Backport #4101 to beta This lint has been causing lots of problems. I'll check up on other potential beta backports when I build the new changelog r? @oli-obk
2 parents 37f5c1e + 426faff commit 6b32f65

File tree

10 files changed

+51
-20
lines changed

10 files changed

+51
-20
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: rust
22

3-
rust: nightly
3+
rust: beta
44

55
os:
66
- linux
@@ -17,6 +17,7 @@ branches:
1717
env:
1818
global:
1919
- RUST_BACKTRACE=1
20+
- RUSTC_BOOTSTRAP=1
2021

2122
install:
2223
- |
@@ -90,7 +91,7 @@ matrix:
9091
script:
9192
- |
9293
rm rust-toolchain
93-
./setup-toolchain.sh
94+
rustup override set beta
9495
export LD_LIBRARY_PATH=$(rustc --print sysroot)/lib
9596
- |
9697
if [ -z ${INTEGRATION} ]; then

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,7 @@ All notable changes to this project will be documented in this file.
978978
[`redundant_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
979979
[`redundant_closure`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
980980
[`redundant_closure_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call
981+
[`redundant_closure_for_method_calls`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
981982
[`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
982983
[`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
983984
[`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
99

10-
[There are 298 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
10+
[There are 299 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1111

1212
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1313

ci/base-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ if [ "${tests_need_reformatting}" == "true" ] ; then
7575
fi
7676

7777
# switch back to master
78-
rustup override set master
78+
# rustup override set master

clippy_lints/src/eta_reduction.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,31 @@ declare_clippy_lint! {
3333
"redundant closures, i.e., `|a| foo(a)` (which can be written as just `foo`)"
3434
}
3535

36+
declare_clippy_lint! {
37+
/// **What it does:** Checks for closures which only invoke a method on the closure
38+
/// argument and can be replaced by referencing the method directly.
39+
///
40+
/// **Why is this bad?** It's unnecessary to create the closure.
41+
///
42+
/// **Known problems:** rust-lang/rust-clippy#3071, rust-lang/rust-clippy#4002,
43+
/// rust-lang/rust-clippy#3942
44+
///
45+
/// **Example:**
46+
/// ```rust,ignore
47+
/// Some('a').map(|s| s.to_uppercase());
48+
/// ```
49+
/// may be rewritten as
50+
/// ```rust,ignore
51+
/// Some('a').map(char::to_uppercase);
52+
/// ```
53+
pub REDUNDANT_CLOSURE_FOR_METHOD_CALLS,
54+
pedantic,
55+
"redundant closures for method calls"
56+
}
57+
3658
impl LintPass for EtaPass {
3759
fn get_lints(&self) -> LintArray {
38-
lint_array!(REDUNDANT_CLOSURE)
60+
lint_array!(REDUNDANT_CLOSURE, REDUNDANT_CLOSURE_FOR_METHOD_CALLS)
3961
}
4062

4163
fn name(&self) -> &'static str {
@@ -110,7 +132,7 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) {
110132
if let Some(name) = get_ufcs_type_name(cx, method_def_id, &args[0]);
111133

112134
then {
113-
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| {
135+
span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure found", |db| {
114136
db.span_suggestion(
115137
expr.span,
116138
"remove closure as shown",

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
611611
enum_glob_use::ENUM_GLOB_USE,
612612
enum_variants::MODULE_NAME_REPETITIONS,
613613
enum_variants::PUB_ENUM_VARIANT_NAMES,
614+
eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS,
614615
functions::TOO_MANY_LINES,
615616
if_not_else::IF_NOT_ELSE,
616617
infinite_iter::MAYBE_INFINITE_ITER,

tests/ui/eta.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
clippy::option_map_unit_fn,
88
clippy::trivially_copy_pass_by_ref
99
)]
10-
#![warn(clippy::redundant_closure, clippy::needless_borrow)]
10+
#![warn(
11+
clippy::redundant_closure,
12+
clippy::redundant_closure_for_method_calls,
13+
clippy::needless_borrow
14+
)]
1115

1216
use std::path::PathBuf;
1317

tests/ui/eta.stderr

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,71 @@
11
error: redundant closure found
2-
--> $DIR/eta.rs:15:27
2+
--> $DIR/eta.rs:19:27
33
|
44
LL | let a = Some(1u8).map(|a| foo(a));
55
| ^^^^^^^^^^ help: remove closure as shown: `foo`
66
|
77
= note: `-D clippy::redundant-closure` implied by `-D warnings`
88

99
error: redundant closure found
10-
--> $DIR/eta.rs:16:10
10+
--> $DIR/eta.rs:20:10
1111
|
1212
LL | meta(|a| foo(a));
1313
| ^^^^^^^^^^ help: remove closure as shown: `foo`
1414

1515
error: redundant closure found
16-
--> $DIR/eta.rs:17:27
16+
--> $DIR/eta.rs:21:27
1717
|
1818
LL | let c = Some(1u8).map(|a| {1+2; foo}(a));
1919
| ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `{1+2; foo}`
2020

2121
error: this expression borrows a reference that is immediately dereferenced by the compiler
22-
--> $DIR/eta.rs:19:21
22+
--> $DIR/eta.rs:23:21
2323
|
2424
LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
2525
| ^^^ help: change this to: `&2`
2626
|
2727
= note: `-D clippy::needless-borrow` implied by `-D warnings`
2828

2929
error: redundant closure found
30-
--> $DIR/eta.rs:26:27
30+
--> $DIR/eta.rs:30:27
3131
|
3232
LL | let e = Some(1u8).map(|a| generic(a));
3333
| ^^^^^^^^^^^^^^ help: remove closure as shown: `generic`
3434

3535
error: redundant closure found
36-
--> $DIR/eta.rs:69:51
36+
--> $DIR/eta.rs:73:51
3737
|
3838
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
3939
| ^^^^^^^^^^^ help: remove closure as shown: `TestStruct::foo`
40+
|
41+
= note: `-D clippy::redundant-closure-for-method-calls` implied by `-D warnings`
4042

4143
error: redundant closure found
42-
--> $DIR/eta.rs:71:51
44+
--> $DIR/eta.rs:75:51
4345
|
4446
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
4547
| ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `TestTrait::trait_foo`
4648

4749
error: redundant closure found
48-
--> $DIR/eta.rs:74:42
50+
--> $DIR/eta.rs:78:42
4951
|
5052
LL | let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
5153
| ^^^^^^^^^^^^^ help: remove closure as shown: `std::vec::Vec::clear`
5254

5355
error: redundant closure found
54-
--> $DIR/eta.rs:79:29
56+
--> $DIR/eta.rs:83:29
5557
|
5658
LL | let e = Some("str").map(|s| s.to_string());
5759
| ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `std::string::ToString::to_string`
5860

5961
error: redundant closure found
60-
--> $DIR/eta.rs:81:27
62+
--> $DIR/eta.rs:85:27
6163
|
6264
LL | let e = Some('a').map(|s| s.to_uppercase());
6365
| ^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_uppercase`
6466

6567
error: redundant closure found
66-
--> $DIR/eta.rs:84:65
68+
--> $DIR/eta.rs:88:65
6769
|
6870
LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
6971
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_ascii_uppercase`

tests/ui/map_clone.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +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)]
6+
#![allow(clippy::redundant_closure_for_method_calls)]
77

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

tests/ui/map_clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +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)]
6+
#![allow(clippy::redundant_closure_for_method_calls)]
77

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

0 commit comments

Comments
 (0)