File tree Expand file tree Collapse file tree 3 files changed +108
-7
lines changed Expand file tree Collapse file tree 3 files changed +108
-7
lines changed Original file line number Diff line number Diff line change
1
+ // run-rustfix
2
+
3
+ #![warn(clippy::let_unit_value)]
4
+ #![allow(clippy::no_effect)]
5
+ #![allow(unused_variables)]
6
+
7
+ macro_rules! let_and_return {
8
+ ($n:expr) => {{
9
+ let ret = $n;
10
+ }};
11
+ }
12
+
13
+ fn main() {
14
+ println!("x");
15
+ let _y = 1; // this is fine
16
+ let _z = ((), 1); // this as well
17
+ if true {
18
+ ();
19
+ }
20
+
21
+ consume_units_with_for_loop(); // should be fine as well
22
+
23
+ multiline_sugg();
24
+
25
+ let_and_return!(()) // should be fine
26
+ }
27
+
28
+ // Related to issue #1964
29
+ fn consume_units_with_for_loop() {
30
+ // `for_let_unit` lint should not be triggered by consuming them using for loop.
31
+ let v = vec![(), (), ()];
32
+ let mut count = 0;
33
+ for _ in v {
34
+ count += 1;
35
+ }
36
+ assert_eq!(count, 3);
37
+
38
+ // Same for consuming from some other Iterator<Item = ()>.
39
+ let (tx, rx) = ::std::sync::mpsc::channel();
40
+ tx.send(()).unwrap();
41
+ drop(tx);
42
+
43
+ count = 0;
44
+ for _ in rx.iter() {
45
+ count += 1;
46
+ }
47
+ assert_eq!(count, 1);
48
+ }
49
+
50
+ fn multiline_sugg() {
51
+ let v: Vec<u8> = vec![2];
52
+
53
+ v
54
+ .into_iter()
55
+ .map(|i| i * 2)
56
+ .filter(|i| i % 2 == 0)
57
+ .map(|_| ())
58
+ .next()
59
+ .unwrap();
60
+ }
61
+
62
+ #[derive(Copy, Clone)]
63
+ pub struct ContainsUnit(()); // should be fine
Original file line number Diff line number Diff line change
1
+ // run-rustfix
2
+
1
3
#![ warn( clippy:: let_unit_value) ]
4
+ #![ allow( clippy:: no_effect) ]
2
5
#![ allow( unused_variables) ]
3
6
4
7
macro_rules! let_and_return {
@@ -17,6 +20,8 @@ fn main() {
17
20
18
21
consume_units_with_for_loop ( ) ; // should be fine as well
19
22
23
+ multiline_sugg ( ) ;
24
+
20
25
let_and_return ! ( ( ) ) // should be fine
21
26
}
22
27
@@ -42,5 +47,17 @@ fn consume_units_with_for_loop() {
42
47
assert_eq ! ( count, 1 ) ;
43
48
}
44
49
50
+ fn multiline_sugg ( ) {
51
+ let v: Vec < u8 > = vec ! [ 2 ] ;
52
+
53
+ let _ = v
54
+ . into_iter ( )
55
+ . map ( |i| i * 2 )
56
+ . filter ( |i| i % 2 == 0 )
57
+ . map ( |_| ( ) )
58
+ . next ( )
59
+ . unwrap ( ) ;
60
+ }
61
+
45
62
#[ derive( Copy , Clone ) ]
46
63
pub struct ContainsUnit ( ( ) ) ; // should be fine
Original file line number Diff line number Diff line change 1
- error: this let-binding has unit value. Consider omitting `let _x =`
2
- --> $DIR/let_unit.rs:11 :5
1
+ error: this let-binding has unit value
2
+ --> $DIR/let_unit.rs:14 :5
3
3
|
4
4
LL | let _x = println!("x");
5
- | ^^^^^^^^^^^^^^^^^^^^^^^
5
+ | ^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `println!("x");`
6
6
|
7
7
= note: `-D clippy::let-unit-value` implied by `-D warnings`
8
8
9
- error: this let-binding has unit value. Consider omitting `let _a =`
10
- --> $DIR/let_unit.rs:15 :9
9
+ error: this let-binding has unit value
10
+ --> $DIR/let_unit.rs:18 :9
11
11
|
12
12
LL | let _a = ();
13
- | ^^^^^^^^^^^^
13
+ | ^^^^^^^^^^^^ help: omit the `let` binding: `();`
14
14
15
- error: aborting due to 2 previous errors
15
+ error: this let-binding has unit value
16
+ --> $DIR/let_unit.rs:53:5
17
+ |
18
+ LL | / let _ = v
19
+ LL | | .into_iter()
20
+ LL | | .map(|i| i * 2)
21
+ LL | | .filter(|i| i % 2 == 0)
22
+ LL | | .map(|_| ())
23
+ LL | | .next()
24
+ LL | | .unwrap();
25
+ | |__________________^
26
+ help: omit the `let` binding
27
+ |
28
+ LL | v
29
+ LL | .into_iter()
30
+ LL | .map(|i| i * 2)
31
+ LL | .filter(|i| i % 2 == 0)
32
+ LL | .map(|_| ())
33
+ LL | .next()
34
+ ...
35
+
36
+ error: aborting due to 3 previous errors
16
37
You can’t perform that action at this time.
0 commit comments