Skip to content

Commit 1ead12c

Browse files
author
Donald Robertson
committed
Adding handling and tests for custom type with implemented expect method
1 parent fe8c9d5 commit 1ead12c

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

clippy_lints/src/methods.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,11 +1006,13 @@ fn lint_expect_fun_call(cx: &LateContext, expr: &hir::Expr, method_span: Span, n
10061006
return;
10071007
}
10081008

1009-
let self_ty = cx.tables.expr_ty(self_expr);
1010-
let closure = match match_type(cx, self_ty, &paths::OPTION) {
1011-
true => "||",
1012-
false => "|_|",
1013-
};
1009+
let self_type = cx.tables.expr_ty(self_expr);
1010+
let known_types = &[&paths::OPTION, &paths::RESULT];
1011+
1012+
// if not a known type, return early
1013+
if known_types.iter().all(|&k| !match_type(cx, self_type, k)) {
1014+
return;
1015+
}
10141016

10151017
// don't lint for constant values
10161018
let owner_def = cx.tcx.hir.get_parent_did(arg.id);
@@ -1019,6 +1021,11 @@ fn lint_expect_fun_call(cx: &LateContext, expr: &hir::Expr, method_span: Span, n
10191021
return;
10201022
}
10211023

1024+
let closure = match match_type(cx, self_type, &paths::OPTION) {
1025+
true => "||",
1026+
false => "|_|",
1027+
};
1028+
10221029
let sugg: Cow<_> = snippet(cx, arg.span, "..");
10231030
let span_replace_word = method_span.with_hi(span.hi());
10241031

tests/ui/methods.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,16 @@ fn or_fun_call() {
344344

345345
/// Checks implementation of the `EXPECT_FUN_CALL` lint
346346
fn expect_fun_call() {
347+
struct Foo;
348+
349+
impl Foo {
350+
fn new() -> Self { Foo }
351+
352+
fn expect(&self, msg: &str) {
353+
panic!("{}", msg)
354+
}
355+
}
356+
347357
let with_some = Some("value");
348358
with_some.expect("error");
349359

@@ -369,6 +379,15 @@ fn expect_fun_call() {
369379

370380
let with_err_and_as_str: Result<(), ()> = Err(());
371381
with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
382+
383+
let with_dummy_type = Foo::new();
384+
with_dummy_type.expect("another test string");
385+
386+
let with_dummy_type_and_format = Foo::new();
387+
with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));
388+
389+
let with_dummy_type_and_as_str = Foo::new();
390+
with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
372391
}
373392

374393
/// Checks implementation of `ITER_NTH` lint

tests/ui/methods.stderr

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -424,105 +424,105 @@ error: use of `unwrap_or` followed by a function call
424424
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
425425

426426
error: use of `expect` followed by a function call
427-
--> $DIR/methods.rs:355:26
427+
--> $DIR/methods.rs:365:26
428428
|
429-
355 | with_none_and_format.expect(&format!("Error {}: fake error", error_code));
429+
365 | with_none_and_format.expect(&format!("Error {}: fake error", error_code));
430430
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!(&format!("Error {}: fake error", error_code)))`
431431
|
432432
= note: `-D expect-fun-call` implied by `-D warnings`
433433

434434
error: use of `expect` followed by a function call
435-
--> $DIR/methods.rs:358:26
435+
--> $DIR/methods.rs:368:26
436436
|
437-
358 | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
437+
368 | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
438438
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!(format!("Error {}: fake error", error_code).as_str()))`
439439

440440
error: use of `expect` followed by a function call
441-
--> $DIR/methods.rs:368:25
441+
--> $DIR/methods.rs:378:25
442442
|
443-
368 | with_err_and_format.expect(&format!("Error {}: fake error", error_code));
443+
378 | with_err_and_format.expect(&format!("Error {}: fake error", error_code));
444444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!(&format!("Error {}: fake error", error_code)))`
445445

446446
error: use of `expect` followed by a function call
447-
--> $DIR/methods.rs:371:25
447+
--> $DIR/methods.rs:381:25
448448
|
449-
371 | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
449+
381 | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
450450
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!(format!("Error {}: fake error", error_code).as_str()))`
451451

452452
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
453-
--> $DIR/methods.rs:382:23
453+
--> $DIR/methods.rs:401:23
454454
|
455-
382 | let bad_vec = some_vec.iter().nth(3);
455+
401 | let bad_vec = some_vec.iter().nth(3);
456456
| ^^^^^^^^^^^^^^^^^^^^^^
457457
|
458458
= note: `-D iter-nth` implied by `-D warnings`
459459

460460
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
461-
--> $DIR/methods.rs:383:26
461+
--> $DIR/methods.rs:402:26
462462
|
463-
383 | let bad_slice = &some_vec[..].iter().nth(3);
463+
402 | let bad_slice = &some_vec[..].iter().nth(3);
464464
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
465465

466466
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
467-
--> $DIR/methods.rs:384:31
467+
--> $DIR/methods.rs:403:31
468468
|
469-
384 | let bad_boxed_slice = boxed_slice.iter().nth(3);
469+
403 | let bad_boxed_slice = boxed_slice.iter().nth(3);
470470
| ^^^^^^^^^^^^^^^^^^^^^^^^^
471471

472472
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
473-
--> $DIR/methods.rs:385:29
473+
--> $DIR/methods.rs:404:29
474474
|
475-
385 | let bad_vec_deque = some_vec_deque.iter().nth(3);
475+
404 | let bad_vec_deque = some_vec_deque.iter().nth(3);
476476
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
477477

478478
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
479-
--> $DIR/methods.rs:390:23
479+
--> $DIR/methods.rs:409:23
480480
|
481-
390 | let bad_vec = some_vec.iter_mut().nth(3);
481+
409 | let bad_vec = some_vec.iter_mut().nth(3);
482482
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
483483

484484
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
485-
--> $DIR/methods.rs:393:26
485+
--> $DIR/methods.rs:412:26
486486
|
487-
393 | let bad_slice = &some_vec[..].iter_mut().nth(3);
487+
412 | let bad_slice = &some_vec[..].iter_mut().nth(3);
488488
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
489489

490490
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
491-
--> $DIR/methods.rs:396:29
491+
--> $DIR/methods.rs:415:29
492492
|
493-
396 | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
493+
415 | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
494494
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
495495

496496
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
497-
--> $DIR/methods.rs:408:13
497+
--> $DIR/methods.rs:427:13
498498
|
499-
408 | let _ = some_vec.iter().skip(42).next();
499+
427 | let _ = some_vec.iter().skip(42).next();
500500
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
501501
|
502502
= note: `-D iter-skip-next` implied by `-D warnings`
503503

504504
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
505-
--> $DIR/methods.rs:409:13
505+
--> $DIR/methods.rs:428:13
506506
|
507-
409 | let _ = some_vec.iter().cycle().skip(42).next();
507+
428 | let _ = some_vec.iter().cycle().skip(42).next();
508508
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
509509

510510
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
511-
--> $DIR/methods.rs:410:13
511+
--> $DIR/methods.rs:429:13
512512
|
513-
410 | let _ = (1..10).skip(10).next();
513+
429 | let _ = (1..10).skip(10).next();
514514
| ^^^^^^^^^^^^^^^^^^^^^^^
515515

516516
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
517-
--> $DIR/methods.rs:411:14
517+
--> $DIR/methods.rs:430:14
518518
|
519-
411 | let _ = &some_vec[..].iter().skip(3).next();
519+
430 | let _ = &some_vec[..].iter().skip(3).next();
520520
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
521521

522522
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
523-
--> $DIR/methods.rs:420:13
523+
--> $DIR/methods.rs:439:13
524524
|
525-
420 | let _ = opt.unwrap();
525+
439 | let _ = opt.unwrap();
526526
| ^^^^^^^^^^^^
527527
|
528528
= note: `-D option-unwrap-used` implied by `-D warnings`

0 commit comments

Comments
 (0)