Skip to content

Commit d089032

Browse files
committed
Auto merge of #4431 - phansch:more_rustfix, r=flip1995
Add 3 more run-rustfix headers changelog: none cc #3630
2 parents 05f603e + 55aa9d2 commit d089032

9 files changed

+192
-25
lines changed

tests/ui/or_fun_call.fixed

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::or_fun_call)]
4+
#![allow(dead_code)]
5+
6+
use std::collections::BTreeMap;
7+
use std::collections::HashMap;
8+
use std::time::Duration;
9+
10+
/// Checks implementation of the `OR_FUN_CALL` lint.
11+
fn or_fun_call() {
12+
struct Foo;
13+
14+
impl Foo {
15+
fn new() -> Foo {
16+
Foo
17+
}
18+
}
19+
20+
enum Enum {
21+
A(i32),
22+
}
23+
24+
fn make<T>() -> T {
25+
unimplemented!();
26+
}
27+
28+
let with_enum = Some(Enum::A(1));
29+
with_enum.unwrap_or(Enum::A(5));
30+
31+
let with_const_fn = Some(Duration::from_secs(1));
32+
with_const_fn.unwrap_or(Duration::from_secs(5));
33+
34+
let with_constructor = Some(vec![1]);
35+
with_constructor.unwrap_or_else(make);
36+
37+
let with_new = Some(vec![1]);
38+
with_new.unwrap_or_default();
39+
40+
let with_const_args = Some(vec![1]);
41+
with_const_args.unwrap_or_else(|| Vec::with_capacity(12));
42+
43+
let with_err: Result<_, ()> = Ok(vec![1]);
44+
with_err.unwrap_or_else(|_| make());
45+
46+
let with_err_args: Result<_, ()> = Ok(vec![1]);
47+
with_err_args.unwrap_or_else(|_| Vec::with_capacity(12));
48+
49+
let with_default_trait = Some(1);
50+
with_default_trait.unwrap_or_default();
51+
52+
let with_default_type = Some(1);
53+
with_default_type.unwrap_or_default();
54+
55+
let with_vec = Some(vec![1]);
56+
with_vec.unwrap_or_else(|| vec![]);
57+
58+
// FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
59+
60+
let without_default = Some(Foo);
61+
without_default.unwrap_or_else(Foo::new);
62+
63+
let mut map = HashMap::<u64, String>::new();
64+
map.entry(42).or_insert_with(String::new);
65+
66+
let mut btree = BTreeMap::<u64, String>::new();
67+
btree.entry(42).or_insert_with(String::new);
68+
69+
let stringy = Some(String::from(""));
70+
let _ = stringy.unwrap_or_else(|| "".to_owned());
71+
72+
let opt = Some(1);
73+
let hello = "Hello";
74+
let _ = opt.ok_or_else(|| format!("{} world.", hello));
75+
}
76+
77+
struct Foo(u8);
78+
struct Bar(String, Duration);
79+
#[rustfmt::skip]
80+
fn test_or_with_ctors() {
81+
let opt = Some(1);
82+
let opt_opt = Some(Some(1));
83+
// we also test for const promotion, this makes sure we don't hit that
84+
let two = 2;
85+
86+
let _ = opt_opt.unwrap_or(Some(2));
87+
let _ = opt_opt.unwrap_or(Some(two));
88+
let _ = opt.ok_or(Some(2));
89+
let _ = opt.ok_or(Some(two));
90+
let _ = opt.ok_or(Foo(2));
91+
let _ = opt.ok_or(Foo(two));
92+
let _ = opt.or(Some(2));
93+
let _ = opt.or(Some(two));
94+
95+
let _ = Some("a".to_string()).or_else(|| Some("b".to_string()));
96+
97+
let b = "b".to_string();
98+
let _ = Some(Bar("a".to_string(), Duration::from_secs(1)))
99+
.or(Some(Bar(b, Duration::from_secs(2))));
100+
}
101+
102+
fn main() {}

tests/ui/or_fun_call.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// run-rustfix
2+
13
#![warn(clippy::or_fun_call)]
4+
#![allow(dead_code)]
25

36
use std::collections::BTreeMap;
47
use std::collections::HashMap;

tests/ui/or_fun_call.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
11
error: use of `unwrap_or` followed by a function call
2-
--> $DIR/or_fun_call.rs:32:22
2+
--> $DIR/or_fun_call.rs:35:22
33
|
44
LL | with_constructor.unwrap_or(make());
55
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
66
|
77
= note: `-D clippy::or-fun-call` implied by `-D warnings`
88

99
error: use of `unwrap_or` followed by a call to `new`
10-
--> $DIR/or_fun_call.rs:35:5
10+
--> $DIR/or_fun_call.rs:38:5
1111
|
1212
LL | with_new.unwrap_or(Vec::new());
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
1414

1515
error: use of `unwrap_or` followed by a function call
16-
--> $DIR/or_fun_call.rs:38:21
16+
--> $DIR/or_fun_call.rs:41:21
1717
|
1818
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`
2020

2121
error: use of `unwrap_or` followed by a function call
22-
--> $DIR/or_fun_call.rs:41:14
22+
--> $DIR/or_fun_call.rs:44:14
2323
|
2424
LL | with_err.unwrap_or(make());
2525
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`
2626

2727
error: use of `unwrap_or` followed by a function call
28-
--> $DIR/or_fun_call.rs:44:19
28+
--> $DIR/or_fun_call.rs:47:19
2929
|
3030
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`
3232

3333
error: use of `unwrap_or` followed by a call to `default`
34-
--> $DIR/or_fun_call.rs:47:5
34+
--> $DIR/or_fun_call.rs:50:5
3535
|
3636
LL | with_default_trait.unwrap_or(Default::default());
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`
3838

3939
error: use of `unwrap_or` followed by a call to `default`
40-
--> $DIR/or_fun_call.rs:50:5
40+
--> $DIR/or_fun_call.rs:53:5
4141
|
4242
LL | with_default_type.unwrap_or(u64::default());
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
4444

4545
error: use of `unwrap_or` followed by a function call
46-
--> $DIR/or_fun_call.rs:53:14
46+
--> $DIR/or_fun_call.rs:56:14
4747
|
4848
LL | with_vec.unwrap_or(vec![]);
4949
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`
5050

5151
error: use of `unwrap_or` followed by a function call
52-
--> $DIR/or_fun_call.rs:58:21
52+
--> $DIR/or_fun_call.rs:61:21
5353
|
5454
LL | without_default.unwrap_or(Foo::new());
5555
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
5656

5757
error: use of `or_insert` followed by a function call
58-
--> $DIR/or_fun_call.rs:61:19
58+
--> $DIR/or_fun_call.rs:64:19
5959
|
6060
LL | map.entry(42).or_insert(String::new());
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
6262

6363
error: use of `or_insert` followed by a function call
64-
--> $DIR/or_fun_call.rs:64:21
64+
--> $DIR/or_fun_call.rs:67:21
6565
|
6666
LL | btree.entry(42).or_insert(String::new());
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
6868

6969
error: use of `unwrap_or` followed by a function call
70-
--> $DIR/or_fun_call.rs:67:21
70+
--> $DIR/or_fun_call.rs:70:21
7171
|
7272
LL | let _ = stringy.unwrap_or("".to_owned());
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
7474

7575
error: use of `ok_or` followed by a function call
76-
--> $DIR/or_fun_call.rs:71:17
76+
--> $DIR/or_fun_call.rs:74:17
7777
|
7878
LL | let _ = opt.ok_or(format!("{} world.", hello));
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `ok_or_else(|| format!("{} world.", hello))`
8080

8181
error: use of `or` followed by a function call
82-
--> $DIR/or_fun_call.rs:92:35
82+
--> $DIR/or_fun_call.rs:95:35
8383
|
8484
LL | let _ = Some("a".to_string()).or(Some("b".to_string()));
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some("b".to_string()))`

tests/ui/range_plus_minus_one.fixed

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// run-rustfix
2+
3+
#![allow(unused_parens)]
4+
5+
fn f() -> usize {
6+
42
7+
}
8+
9+
#[warn(clippy::range_plus_one)]
10+
fn main() {
11+
for _ in 0..2 {}
12+
for _ in 0..=2 {}
13+
14+
for _ in 0..=3 {}
15+
for _ in 0..=3 + 1 {}
16+
17+
for _ in 0..=5 {}
18+
for _ in 0..=1 + 5 {}
19+
20+
for _ in 1..=1 {}
21+
for _ in 1..=1 + 1 {}
22+
23+
for _ in 0..13 + 13 {}
24+
for _ in 0..=13 - 7 {}
25+
26+
for _ in 0..=f() {}
27+
for _ in 0..=(1 + f()) {}
28+
29+
let _ = ..11 - 1;
30+
let _ = ..11;
31+
let _ = ..11;
32+
let _ = (1..=11);
33+
let _ = ((f() + 1)..=f());
34+
35+
let mut vec: Vec<()> = std::vec::Vec::new();
36+
vec.drain(..);
37+
}

tests/ui/range_plus_minus_one.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// run-rustfix
2+
3+
#![allow(unused_parens)]
4+
15
fn f() -> usize {
26
42
37
}

tests/ui/range_plus_minus_one.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
error: an inclusive range would be more readable
2-
--> $DIR/range_plus_minus_one.rs:10:14
2+
--> $DIR/range_plus_minus_one.rs:14:14
33
|
44
LL | for _ in 0..3 + 1 {}
55
| ^^^^^^^^ help: use: `0..=3`
66
|
77
= note: `-D clippy::range-plus-one` implied by `-D warnings`
88

99
error: an inclusive range would be more readable
10-
--> $DIR/range_plus_minus_one.rs:13:14
10+
--> $DIR/range_plus_minus_one.rs:17:14
1111
|
1212
LL | for _ in 0..1 + 5 {}
1313
| ^^^^^^^^ help: use: `0..=5`
1414

1515
error: an inclusive range would be more readable
16-
--> $DIR/range_plus_minus_one.rs:16:14
16+
--> $DIR/range_plus_minus_one.rs:20:14
1717
|
1818
LL | for _ in 1..1 + 1 {}
1919
| ^^^^^^^^ help: use: `1..=1`
2020

2121
error: an inclusive range would be more readable
22-
--> $DIR/range_plus_minus_one.rs:22:14
22+
--> $DIR/range_plus_minus_one.rs:26:14
2323
|
2424
LL | for _ in 0..(1 + f()) {}
2525
| ^^^^^^^^^^^^ help: use: `0..=f()`
2626

2727
error: an exclusive range would be more readable
28-
--> $DIR/range_plus_minus_one.rs:26:13
28+
--> $DIR/range_plus_minus_one.rs:30:13
2929
|
3030
LL | let _ = ..=11 - 1;
3131
| ^^^^^^^^^ help: use: `..11`
3232
|
3333
= note: `-D clippy::range-minus-one` implied by `-D warnings`
3434

3535
error: an exclusive range would be more readable
36-
--> $DIR/range_plus_minus_one.rs:27:13
36+
--> $DIR/range_plus_minus_one.rs:31:13
3737
|
3838
LL | let _ = ..=(11 - 1);
3939
| ^^^^^^^^^^^ help: use: `..11`
4040

4141
error: an inclusive range would be more readable
42-
--> $DIR/range_plus_minus_one.rs:28:13
42+
--> $DIR/range_plus_minus_one.rs:32:13
4343
|
4444
LL | let _ = (1..11 + 1);
4545
| ^^^^^^^^^^^ help: use: `(1..=11)`
4646

4747
error: an inclusive range would be more readable
48-
--> $DIR/range_plus_minus_one.rs:29:13
48+
--> $DIR/range_plus_minus_one.rs:33:13
4949
|
5050
LL | let _ = (f() + 1)..(f() + 1);
5151
| ^^^^^^^^^^^^^^^^^^^^ help: use: `((f() + 1)..=f())`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::short_circuit_statement)]
4+
#![allow(clippy::nonminimal_bool)]
5+
6+
fn main() {
7+
if f() { g(); }
8+
if !f() { g(); }
9+
if !(1 == 2) { g(); }
10+
}
11+
12+
fn f() -> bool {
13+
true
14+
}
15+
16+
fn g() -> bool {
17+
false
18+
}

tests/ui/short_circuit_statement.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// run-rustfix
2+
13
#![warn(clippy::short_circuit_statement)]
4+
#![allow(clippy::nonminimal_bool)]
25

36
fn main() {
47
f() && g();

tests/ui/short_circuit_statement.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: boolean short circuit operator in statement may be clearer using an explicit test
2-
--> $DIR/short_circuit_statement.rs:4:5
2+
--> $DIR/short_circuit_statement.rs:7:5
33
|
44
LL | f() && g();
55
| ^^^^^^^^^^^ help: replace it with: `if f() { g(); }`
66
|
77
= note: `-D clippy::short-circuit-statement` implied by `-D warnings`
88

99
error: boolean short circuit operator in statement may be clearer using an explicit test
10-
--> $DIR/short_circuit_statement.rs:5:5
10+
--> $DIR/short_circuit_statement.rs:8:5
1111
|
1212
LL | f() || g();
1313
| ^^^^^^^^^^^ help: replace it with: `if !f() { g(); }`
1414

1515
error: boolean short circuit operator in statement may be clearer using an explicit test
16-
--> $DIR/short_circuit_statement.rs:6:5
16+
--> $DIR/short_circuit_statement.rs:9:5
1717
|
1818
LL | 1 == 2 || g();
1919
| ^^^^^^^^^^^^^^ help: replace it with: `if !(1 == 2) { g(); }`

0 commit comments

Comments
 (0)