Skip to content

Commit ae137d5

Browse files
bors[bot]phansch
andcommitted
Merge #3397 #3398
3397: UI test cleanup: Extract expect_fun_call tests r=matthiaskrgr a=phansch Note that the new stderr file does not include a `shadow-unrelated` error, because the new UI test file does not use `#![warn(clippy::all)]` cc #2038 3398: UI test cleanup: Extract match_overlapping_arm tests r=matthiaskrgr a=phansch cc #2038 Co-authored-by: Philipp Hansch <[email protected]>
3 parents 0ad5b9b + 26569f3 + e5af43d commit ae137d5

8 files changed

+379
-363
lines changed

tests/ui/expect_fun_call.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
#![warn(clippy::expect_fun_call)]
11+
#![allow(clippy::useless_format)]
12+
13+
/// Checks implementation of the `EXPECT_FUN_CALL` lint
14+
15+
fn expect_fun_call() {
16+
struct Foo;
17+
18+
impl Foo {
19+
fn new() -> Self { Foo }
20+
21+
fn expect(&self, msg: &str) {
22+
panic!("{}", msg)
23+
}
24+
}
25+
26+
let with_some = Some("value");
27+
with_some.expect("error");
28+
29+
let with_none: Option<i32> = None;
30+
with_none.expect("error");
31+
32+
let error_code = 123_i32;
33+
let with_none_and_format: Option<i32> = None;
34+
with_none_and_format.expect(&format!("Error {}: fake error", error_code));
35+
36+
let with_none_and_as_str: Option<i32> = None;
37+
with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
38+
39+
let with_ok: Result<(), ()> = Ok(());
40+
with_ok.expect("error");
41+
42+
let with_err: Result<(), ()> = Err(());
43+
with_err.expect("error");
44+
45+
let error_code = 123_i32;
46+
let with_err_and_format: Result<(), ()> = Err(());
47+
with_err_and_format.expect(&format!("Error {}: fake error", error_code));
48+
49+
let with_err_and_as_str: Result<(), ()> = Err(());
50+
with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
51+
52+
let with_dummy_type = Foo::new();
53+
with_dummy_type.expect("another test string");
54+
55+
let with_dummy_type_and_format = Foo::new();
56+
with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));
57+
58+
let with_dummy_type_and_as_str = Foo::new();
59+
with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
60+
61+
//Issue #2979 - this should not lint
62+
let msg = "bar";
63+
Some("foo").expect(msg);
64+
65+
Some("foo").expect({ &format!("error") });
66+
Some("foo").expect(format!("error").as_ref());
67+
}
68+
69+
fn main() {}

tests/ui/expect_fun_call.stderr

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: use of `expect` followed by a function call
2+
--> $DIR/expect_fun_call.rs:34:26
3+
|
4+
34 | with_none_and_format.expect(&format!("Error {}: fake error", error_code));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`
6+
|
7+
= note: `-D clippy::expect-fun-call` implied by `-D warnings`
8+
9+
error: use of `expect` followed by a function call
10+
--> $DIR/expect_fun_call.rs:37:26
11+
|
12+
37 | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`
14+
15+
error: use of `expect` followed by a function call
16+
--> $DIR/expect_fun_call.rs:47:25
17+
|
18+
47 | with_err_and_format.expect(&format!("Error {}: fake error", error_code));
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
20+
21+
error: use of `expect` followed by a function call
22+
--> $DIR/expect_fun_call.rs:50:25
23+
|
24+
50 | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
26+
27+
error: use of `expect` followed by a function call
28+
--> $DIR/expect_fun_call.rs:65:17
29+
|
30+
65 | Some("foo").expect({ &format!("error") });
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { let msg = { &format!("error") }; panic!(msg) }))`
32+
33+
error: use of `expect` followed by a function call
34+
--> $DIR/expect_fun_call.rs:66:17
35+
|
36+
66 | Some("foo").expect(format!("error").as_ref());
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("error"))`
38+
39+
error: aborting due to 6 previous errors
40+

tests/ui/match_overlapping_arm.rs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
#![feature(exclusive_range_pattern)]
11+
#![warn(clippy::match_overlapping_arm)]
12+
#![allow(clippy::redundant_pattern_matching)]
13+
14+
/// Tests for match_overlapping_arm
15+
16+
fn overlapping() {
17+
const FOO : u64 = 2;
18+
19+
match 42 {
20+
0 ... 10 => println!("0 ... 10"),
21+
0 ... 11 => println!("0 ... 11"),
22+
_ => (),
23+
}
24+
25+
match 42 {
26+
0 ... 5 => println!("0 ... 5"),
27+
6 ... 7 => println!("6 ... 7"),
28+
FOO ... 11 => println!("0 ... 11"),
29+
_ => (),
30+
}
31+
32+
match 42 {
33+
2 => println!("2"),
34+
0 ... 5 => println!("0 ... 5"),
35+
_ => (),
36+
}
37+
38+
match 42 {
39+
2 => println!("2"),
40+
0 ... 2 => println!("0 ... 2"),
41+
_ => (),
42+
}
43+
44+
match 42 {
45+
0 ... 10 => println!("0 ... 10"),
46+
11 ... 50 => println!("11 ... 50"),
47+
_ => (),
48+
}
49+
50+
match 42 {
51+
2 => println!("2"),
52+
0 .. 2 => println!("0 .. 2"),
53+
_ => (),
54+
}
55+
56+
match 42 {
57+
0 .. 10 => println!("0 .. 10"),
58+
10 .. 50 => println!("10 .. 50"),
59+
_ => (),
60+
}
61+
62+
match 42 {
63+
0 .. 11 => println!("0 .. 11"),
64+
0 ... 11 => println!("0 ... 11"),
65+
_ => (),
66+
}
67+
68+
if let None = Some(42) {
69+
// nothing
70+
} else if let None = Some(42) {
71+
// another nothing :-)
72+
}
73+
}
74+
75+
fn main() {}

tests/ui/match_overlapping_arm.stderr

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error: some ranges overlap
2+
--> $DIR/match_overlapping_arm.rs:20:9
3+
|
4+
20 | 0 ... 10 => println!("0 ... 10"),
5+
| ^^^^^^^^
6+
|
7+
= note: `-D clippy::match-overlapping-arm` implied by `-D warnings`
8+
note: overlaps with this
9+
--> $DIR/match_overlapping_arm.rs:21:9
10+
|
11+
21 | 0 ... 11 => println!("0 ... 11"),
12+
| ^^^^^^^^
13+
14+
error: some ranges overlap
15+
--> $DIR/match_overlapping_arm.rs:26:9
16+
|
17+
26 | 0 ... 5 => println!("0 ... 5"),
18+
| ^^^^^^^
19+
|
20+
note: overlaps with this
21+
--> $DIR/match_overlapping_arm.rs:28:9
22+
|
23+
28 | FOO ... 11 => println!("0 ... 11"),
24+
| ^^^^^^^^^^
25+
26+
error: some ranges overlap
27+
--> $DIR/match_overlapping_arm.rs:34:9
28+
|
29+
34 | 0 ... 5 => println!("0 ... 5"),
30+
| ^^^^^^^
31+
|
32+
note: overlaps with this
33+
--> $DIR/match_overlapping_arm.rs:33:9
34+
|
35+
33 | 2 => println!("2"),
36+
| ^
37+
38+
error: some ranges overlap
39+
--> $DIR/match_overlapping_arm.rs:40:9
40+
|
41+
40 | 0 ... 2 => println!("0 ... 2"),
42+
| ^^^^^^^
43+
|
44+
note: overlaps with this
45+
--> $DIR/match_overlapping_arm.rs:39:9
46+
|
47+
39 | 2 => println!("2"),
48+
| ^
49+
50+
error: some ranges overlap
51+
--> $DIR/match_overlapping_arm.rs:63:9
52+
|
53+
63 | 0 .. 11 => println!("0 .. 11"),
54+
| ^^^^^^^
55+
|
56+
note: overlaps with this
57+
--> $DIR/match_overlapping_arm.rs:64:9
58+
|
59+
64 | 0 ... 11 => println!("0 ... 11"),
60+
| ^^^^^^^^
61+
62+
error: aborting due to 5 previous errors
63+

tests/ui/matches.rs

-59
Original file line numberDiff line numberDiff line change
@@ -74,65 +74,6 @@ fn ref_pats() {
7474
}
7575
}
7676

77-
fn overlapping() {
78-
const FOO : u64 = 2;
79-
80-
match 42 {
81-
0 ... 10 => println!("0 ... 10"),
82-
0 ... 11 => println!("0 ... 11"),
83-
_ => (),
84-
}
85-
86-
match 42 {
87-
0 ... 5 => println!("0 ... 5"),
88-
6 ... 7 => println!("6 ... 7"),
89-
FOO ... 11 => println!("0 ... 11"),
90-
_ => (),
91-
}
92-
93-
match 42 {
94-
2 => println!("2"),
95-
0 ... 5 => println!("0 ... 5"),
96-
_ => (),
97-
}
98-
99-
match 42 {
100-
2 => println!("2"),
101-
0 ... 2 => println!("0 ... 2"),
102-
_ => (),
103-
}
104-
105-
match 42 {
106-
0 ... 10 => println!("0 ... 10"),
107-
11 ... 50 => println!("11 ... 50"),
108-
_ => (),
109-
}
110-
111-
match 42 {
112-
2 => println!("2"),
113-
0 .. 2 => println!("0 .. 2"),
114-
_ => (),
115-
}
116-
117-
match 42 {
118-
0 .. 10 => println!("0 .. 10"),
119-
10 .. 50 => println!("10 .. 50"),
120-
_ => (),
121-
}
122-
123-
match 42 {
124-
0 .. 11 => println!("0 .. 11"),
125-
0 ... 11 => println!("0 ... 11"),
126-
_ => (),
127-
}
128-
129-
if let None = Some(42) {
130-
// nothing
131-
} else if let None = Some(42) {
132-
// another nothing :-)
133-
}
134-
}
135-
13677
fn match_wild_err_arm() {
13778
let x: Result<i32, &str> = Ok(3);
13879

0 commit comments

Comments
 (0)