Skip to content

Commit

Permalink
Merge #3397 #3398
Browse files Browse the repository at this point in the history
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 <dev@phansch.net>
  • Loading branch information
bors[bot] and phansch committed Nov 2, 2018
3 parents 0ad5b9b + 26569f3 + e5af43d commit ae137d5
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 363 deletions.
69 changes: 69 additions & 0 deletions tests/ui/expect_fun_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![warn(clippy::expect_fun_call)]
#![allow(clippy::useless_format)]

/// Checks implementation of the `EXPECT_FUN_CALL` lint
fn expect_fun_call() {
struct Foo;

impl Foo {
fn new() -> Self { Foo }

fn expect(&self, msg: &str) {
panic!("{}", msg)
}
}

let with_some = Some("value");
with_some.expect("error");

let with_none: Option<i32> = None;
with_none.expect("error");

let error_code = 123_i32;
let with_none_and_format: Option<i32> = None;
with_none_and_format.expect(&format!("Error {}: fake error", error_code));

let with_none_and_as_str: Option<i32> = None;
with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());

let with_ok: Result<(), ()> = Ok(());
with_ok.expect("error");

let with_err: Result<(), ()> = Err(());
with_err.expect("error");

let error_code = 123_i32;
let with_err_and_format: Result<(), ()> = Err(());
with_err_and_format.expect(&format!("Error {}: fake error", error_code));

let with_err_and_as_str: Result<(), ()> = Err(());
with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());

let with_dummy_type = Foo::new();
with_dummy_type.expect("another test string");

let with_dummy_type_and_format = Foo::new();
with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));

let with_dummy_type_and_as_str = Foo::new();
with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());

//Issue #2979 - this should not lint
let msg = "bar";
Some("foo").expect(msg);

Some("foo").expect({ &format!("error") });
Some("foo").expect(format!("error").as_ref());
}

fn main() {}
40 changes: 40 additions & 0 deletions tests/ui/expect_fun_call.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
error: use of `expect` followed by a function call
--> $DIR/expect_fun_call.rs:34:26
|
34 | with_none_and_format.expect(&format!("Error {}: fake error", error_code));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`
|
= note: `-D clippy::expect-fun-call` implied by `-D warnings`

error: use of `expect` followed by a function call
--> $DIR/expect_fun_call.rs:37:26
|
37 | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`

error: use of `expect` followed by a function call
--> $DIR/expect_fun_call.rs:47:25
|
47 | with_err_and_format.expect(&format!("Error {}: fake error", error_code));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`

error: use of `expect` followed by a function call
--> $DIR/expect_fun_call.rs:50:25
|
50 | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`

error: use of `expect` followed by a function call
--> $DIR/expect_fun_call.rs:65:17
|
65 | Some("foo").expect({ &format!("error") });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { let msg = { &format!("error") }; panic!(msg) }))`

error: use of `expect` followed by a function call
--> $DIR/expect_fun_call.rs:66:17
|
66 | Some("foo").expect(format!("error").as_ref());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("error"))`

error: aborting due to 6 previous errors

75 changes: 75 additions & 0 deletions tests/ui/match_overlapping_arm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(exclusive_range_pattern)]
#![warn(clippy::match_overlapping_arm)]
#![allow(clippy::redundant_pattern_matching)]

/// Tests for match_overlapping_arm
fn overlapping() {
const FOO : u64 = 2;

match 42 {
0 ... 10 => println!("0 ... 10"),
0 ... 11 => println!("0 ... 11"),
_ => (),
}

match 42 {
0 ... 5 => println!("0 ... 5"),
6 ... 7 => println!("6 ... 7"),
FOO ... 11 => println!("0 ... 11"),
_ => (),
}

match 42 {
2 => println!("2"),
0 ... 5 => println!("0 ... 5"),
_ => (),
}

match 42 {
2 => println!("2"),
0 ... 2 => println!("0 ... 2"),
_ => (),
}

match 42 {
0 ... 10 => println!("0 ... 10"),
11 ... 50 => println!("11 ... 50"),
_ => (),
}

match 42 {
2 => println!("2"),
0 .. 2 => println!("0 .. 2"),
_ => (),
}

match 42 {
0 .. 10 => println!("0 .. 10"),
10 .. 50 => println!("10 .. 50"),
_ => (),
}

match 42 {
0 .. 11 => println!("0 .. 11"),
0 ... 11 => println!("0 ... 11"),
_ => (),
}

if let None = Some(42) {
// nothing
} else if let None = Some(42) {
// another nothing :-)
}
}

fn main() {}
63 changes: 63 additions & 0 deletions tests/ui/match_overlapping_arm.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:20:9
|
20 | 0 ... 10 => println!("0 ... 10"),
| ^^^^^^^^
|
= note: `-D clippy::match-overlapping-arm` implied by `-D warnings`
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:21:9
|
21 | 0 ... 11 => println!("0 ... 11"),
| ^^^^^^^^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:26:9
|
26 | 0 ... 5 => println!("0 ... 5"),
| ^^^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:28:9
|
28 | FOO ... 11 => println!("0 ... 11"),
| ^^^^^^^^^^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:34:9
|
34 | 0 ... 5 => println!("0 ... 5"),
| ^^^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:33:9
|
33 | 2 => println!("2"),
| ^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:40:9
|
40 | 0 ... 2 => println!("0 ... 2"),
| ^^^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:39:9
|
39 | 2 => println!("2"),
| ^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:63:9
|
63 | 0 .. 11 => println!("0 .. 11"),
| ^^^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:64:9
|
64 | 0 ... 11 => println!("0 ... 11"),
| ^^^^^^^^

error: aborting due to 5 previous errors

59 changes: 0 additions & 59 deletions tests/ui/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,65 +74,6 @@ fn ref_pats() {
}
}

fn overlapping() {
const FOO : u64 = 2;

match 42 {
0 ... 10 => println!("0 ... 10"),
0 ... 11 => println!("0 ... 11"),
_ => (),
}

match 42 {
0 ... 5 => println!("0 ... 5"),
6 ... 7 => println!("6 ... 7"),
FOO ... 11 => println!("0 ... 11"),
_ => (),
}

match 42 {
2 => println!("2"),
0 ... 5 => println!("0 ... 5"),
_ => (),
}

match 42 {
2 => println!("2"),
0 ... 2 => println!("0 ... 2"),
_ => (),
}

match 42 {
0 ... 10 => println!("0 ... 10"),
11 ... 50 => println!("11 ... 50"),
_ => (),
}

match 42 {
2 => println!("2"),
0 .. 2 => println!("0 .. 2"),
_ => (),
}

match 42 {
0 .. 10 => println!("0 .. 10"),
10 .. 50 => println!("10 .. 50"),
_ => (),
}

match 42 {
0 .. 11 => println!("0 .. 11"),
0 ... 11 => println!("0 ... 11"),
_ => (),
}

if let None = Some(42) {
// nothing
} else if let None = Some(42) {
// another nothing :-)
}
}

fn match_wild_err_arm() {
let x: Result<i32, &str> = Ok(3);

Expand Down
Loading

0 comments on commit ae137d5

Please sign in to comment.