Skip to content

Commit 26569f3

Browse files
committed
UI test cleanup: Extract expect_fun_call tests
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)]`
1 parent 5172271 commit 26569f3

File tree

4 files changed

+134
-136
lines changed

4 files changed

+134
-136
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/methods.rs

-55
Original file line numberDiff line numberDiff line change
@@ -353,61 +353,6 @@ fn or_fun_call() {
353353
let _ = stringy.unwrap_or("".to_owned());
354354
}
355355

356-
/// Checks implementation of the `EXPECT_FUN_CALL` lint
357-
fn expect_fun_call() {
358-
struct Foo;
359-
360-
impl Foo {
361-
fn new() -> Self { Foo }
362-
363-
fn expect(&self, msg: &str) {
364-
panic!("{}", msg)
365-
}
366-
}
367-
368-
let with_some = Some("value");
369-
with_some.expect("error");
370-
371-
let with_none: Option<i32> = None;
372-
with_none.expect("error");
373-
374-
let error_code = 123_i32;
375-
let with_none_and_format: Option<i32> = None;
376-
with_none_and_format.expect(&format!("Error {}: fake error", error_code));
377-
378-
let with_none_and_as_str: Option<i32> = None;
379-
with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
380-
381-
let with_ok: Result<(), ()> = Ok(());
382-
with_ok.expect("error");
383-
384-
let with_err: Result<(), ()> = Err(());
385-
with_err.expect("error");
386-
387-
let error_code = 123_i32;
388-
let with_err_and_format: Result<(), ()> = Err(());
389-
with_err_and_format.expect(&format!("Error {}: fake error", error_code));
390-
391-
let with_err_and_as_str: Result<(), ()> = Err(());
392-
with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
393-
394-
let with_dummy_type = Foo::new();
395-
with_dummy_type.expect("another test string");
396-
397-
let with_dummy_type_and_format = Foo::new();
398-
with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));
399-
400-
let with_dummy_type_and_as_str = Foo::new();
401-
with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
402-
403-
//Issue #2979 - this should not lint
404-
let msg = "bar";
405-
Some("foo").expect(msg);
406-
407-
Some("foo").expect({ &format!("error") });
408-
Some("foo").expect(format!("error").as_ref());
409-
}
410-
411356
/// Checks implementation of `ITER_NTH` lint
412357
fn iter_nth() {
413358
let mut some_vec = vec![0, 1, 2, 3];

tests/ui/methods.stderr

+25-81
Original file line numberDiff line numberDiff line change
@@ -323,139 +323,83 @@ error: use of `unwrap_or` followed by a function call
323323
353 | let _ = stringy.unwrap_or("".to_owned());
324324
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
325325

326-
error: `error_code` is shadowed by `123_i32`
327-
--> $DIR/methods.rs:387:9
328-
|
329-
387 | let error_code = 123_i32;
330-
| ^^^^^^^^^^
331-
|
332-
= note: `-D clippy::shadow-unrelated` implied by `-D warnings`
333-
note: initialization happens here
334-
--> $DIR/methods.rs:387:22
335-
|
336-
387 | let error_code = 123_i32;
337-
| ^^^^^^^
338-
note: previous binding is here
339-
--> $DIR/methods.rs:374:9
340-
|
341-
374 | let error_code = 123_i32;
342-
| ^^^^^^^^^^
343-
344-
error: use of `expect` followed by a function call
345-
--> $DIR/methods.rs:376:26
346-
|
347-
376 | with_none_and_format.expect(&format!("Error {}: fake error", error_code));
348-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`
349-
|
350-
= note: `-D clippy::expect-fun-call` implied by `-D warnings`
351-
352-
error: use of `expect` followed by a function call
353-
--> $DIR/methods.rs:379:26
354-
|
355-
379 | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
356-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`
357-
358-
error: use of `expect` followed by a function call
359-
--> $DIR/methods.rs:389:25
360-
|
361-
389 | with_err_and_format.expect(&format!("Error {}: fake error", error_code));
362-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
363-
364-
error: use of `expect` followed by a function call
365-
--> $DIR/methods.rs:392:25
366-
|
367-
392 | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
368-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
369-
370-
error: use of `expect` followed by a function call
371-
--> $DIR/methods.rs:407:17
372-
|
373-
407 | Some("foo").expect({ &format!("error") });
374-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { let msg = { &format!("error") }; panic!(msg) }))`
375-
376-
error: use of `expect` followed by a function call
377-
--> $DIR/methods.rs:408:17
378-
|
379-
408 | Some("foo").expect(format!("error").as_ref());
380-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("error"))`
381-
382326
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
383-
--> $DIR/methods.rs:419:23
327+
--> $DIR/methods.rs:364:23
384328
|
385-
419 | let bad_vec = some_vec.iter().nth(3);
329+
364 | let bad_vec = some_vec.iter().nth(3);
386330
| ^^^^^^^^^^^^^^^^^^^^^^
387331
|
388332
= note: `-D clippy::iter-nth` implied by `-D warnings`
389333

390334
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
391-
--> $DIR/methods.rs:420:26
335+
--> $DIR/methods.rs:365:26
392336
|
393-
420 | let bad_slice = &some_vec[..].iter().nth(3);
337+
365 | let bad_slice = &some_vec[..].iter().nth(3);
394338
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
395339

396340
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
397-
--> $DIR/methods.rs:421:31
341+
--> $DIR/methods.rs:366:31
398342
|
399-
421 | let bad_boxed_slice = boxed_slice.iter().nth(3);
343+
366 | let bad_boxed_slice = boxed_slice.iter().nth(3);
400344
| ^^^^^^^^^^^^^^^^^^^^^^^^^
401345

402346
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
403-
--> $DIR/methods.rs:422:29
347+
--> $DIR/methods.rs:367:29
404348
|
405-
422 | let bad_vec_deque = some_vec_deque.iter().nth(3);
349+
367 | let bad_vec_deque = some_vec_deque.iter().nth(3);
406350
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
407351

408352
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
409-
--> $DIR/methods.rs:427:23
353+
--> $DIR/methods.rs:372:23
410354
|
411-
427 | let bad_vec = some_vec.iter_mut().nth(3);
355+
372 | let bad_vec = some_vec.iter_mut().nth(3);
412356
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
413357

414358
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
415-
--> $DIR/methods.rs:430:26
359+
--> $DIR/methods.rs:375:26
416360
|
417-
430 | let bad_slice = &some_vec[..].iter_mut().nth(3);
361+
375 | let bad_slice = &some_vec[..].iter_mut().nth(3);
418362
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
419363

420364
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
421-
--> $DIR/methods.rs:433:29
365+
--> $DIR/methods.rs:378:29
422366
|
423-
433 | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
367+
378 | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
424368
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
425369

426370
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
427-
--> $DIR/methods.rs:445:13
371+
--> $DIR/methods.rs:390:13
428372
|
429-
445 | let _ = some_vec.iter().skip(42).next();
373+
390 | let _ = some_vec.iter().skip(42).next();
430374
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
431375
|
432376
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
433377

434378
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
435-
--> $DIR/methods.rs:446:13
379+
--> $DIR/methods.rs:391:13
436380
|
437-
446 | let _ = some_vec.iter().cycle().skip(42).next();
381+
391 | let _ = some_vec.iter().cycle().skip(42).next();
438382
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
439383

440384
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
441-
--> $DIR/methods.rs:447:13
385+
--> $DIR/methods.rs:392:13
442386
|
443-
447 | let _ = (1..10).skip(10).next();
387+
392 | let _ = (1..10).skip(10).next();
444388
| ^^^^^^^^^^^^^^^^^^^^^^^
445389

446390
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
447-
--> $DIR/methods.rs:448:14
391+
--> $DIR/methods.rs:393:14
448392
|
449-
448 | let _ = &some_vec[..].iter().skip(3).next();
393+
393 | let _ = &some_vec[..].iter().skip(3).next();
450394
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
451395

452396
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
453-
--> $DIR/methods.rs:457:13
397+
--> $DIR/methods.rs:402:13
454398
|
455-
457 | let _ = opt.unwrap();
399+
402 | let _ = opt.unwrap();
456400
| ^^^^^^^^^^^^
457401
|
458402
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
459403

460-
error: aborting due to 57 previous errors
404+
error: aborting due to 50 previous errors
461405

0 commit comments

Comments
 (0)