Skip to content

Commit a016d4e

Browse files
authored
Merge pull request #2991 from mikerite/issue2979
Fix #2979
2 parents 7e5e4c1 + 534d546 commit a016d4e

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

clippy_lints/src/methods.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,10 +1047,21 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span:
10471047
return;
10481048
}
10491049

1050-
// don't lint for constant values
1051-
let owner_def = cx.tcx.hir.get_parent_did(arg.id);
1052-
let promotable = cx.tcx.rvalue_promotable_map(owner_def).contains(&arg.hir_id.local_id);
1053-
if promotable {
1050+
fn is_call(node: &hir::ExprKind) -> bool {
1051+
match node {
1052+
hir::ExprKind::AddrOf(_, expr) => {
1053+
is_call(&expr.node)
1054+
},
1055+
hir::ExprKind::Call(..)
1056+
| hir::ExprKind::MethodCall(..)
1057+
// These variants are debatable or require further examination
1058+
| hir::ExprKind::If(..)
1059+
| hir::ExprKind::Match(..) => true,
1060+
_ => false,
1061+
}
1062+
}
1063+
1064+
if !is_call(&arg.node) {
10541065
return;
10551066
}
10561067

tests/ui/methods.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ fn expect_fun_call() {
389389

390390
let with_dummy_type_and_as_str = Foo::new();
391391
with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
392+
393+
//Issue #2979 - this should not lint
394+
let msg = "bar";
395+
Some("foo").expect(msg);
392396
}
393397

394398
/// Checks implementation of `ITER_NTH` lint

tests/ui/methods.stderr

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -358,79 +358,79 @@ error: use of `expect` followed by a function call
358358
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!(format!("Error {}: fake error", error_code).as_str()))`
359359

360360
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
361-
--> $DIR/methods.rs:402:23
361+
--> $DIR/methods.rs:406:23
362362
|
363-
402 | let bad_vec = some_vec.iter().nth(3);
363+
406 | let bad_vec = some_vec.iter().nth(3);
364364
| ^^^^^^^^^^^^^^^^^^^^^^
365365
|
366366
= note: `-D iter-nth` implied by `-D warnings`
367367

368368
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
369-
--> $DIR/methods.rs:403:26
369+
--> $DIR/methods.rs:407:26
370370
|
371-
403 | let bad_slice = &some_vec[..].iter().nth(3);
371+
407 | let bad_slice = &some_vec[..].iter().nth(3);
372372
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
373373

374374
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
375-
--> $DIR/methods.rs:404:31
375+
--> $DIR/methods.rs:408:31
376376
|
377-
404 | let bad_boxed_slice = boxed_slice.iter().nth(3);
377+
408 | let bad_boxed_slice = boxed_slice.iter().nth(3);
378378
| ^^^^^^^^^^^^^^^^^^^^^^^^^
379379

380380
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
381-
--> $DIR/methods.rs:405:29
381+
--> $DIR/methods.rs:409:29
382382
|
383-
405 | let bad_vec_deque = some_vec_deque.iter().nth(3);
383+
409 | let bad_vec_deque = some_vec_deque.iter().nth(3);
384384
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
385385

386386
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
387-
--> $DIR/methods.rs:410:23
387+
--> $DIR/methods.rs:414:23
388388
|
389-
410 | let bad_vec = some_vec.iter_mut().nth(3);
389+
414 | let bad_vec = some_vec.iter_mut().nth(3);
390390
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
391391

392392
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
393-
--> $DIR/methods.rs:413:26
393+
--> $DIR/methods.rs:417:26
394394
|
395-
413 | let bad_slice = &some_vec[..].iter_mut().nth(3);
395+
417 | let bad_slice = &some_vec[..].iter_mut().nth(3);
396396
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
397397

398398
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
399-
--> $DIR/methods.rs:416:29
399+
--> $DIR/methods.rs:420:29
400400
|
401-
416 | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
401+
420 | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
402402
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
403403

404404
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
405-
--> $DIR/methods.rs:428:13
405+
--> $DIR/methods.rs:432:13
406406
|
407-
428 | let _ = some_vec.iter().skip(42).next();
407+
432 | let _ = some_vec.iter().skip(42).next();
408408
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
409409
|
410410
= note: `-D iter-skip-next` implied by `-D warnings`
411411

412412
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
413-
--> $DIR/methods.rs:429:13
413+
--> $DIR/methods.rs:433:13
414414
|
415-
429 | let _ = some_vec.iter().cycle().skip(42).next();
415+
433 | let _ = some_vec.iter().cycle().skip(42).next();
416416
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
417417

418418
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
419-
--> $DIR/methods.rs:430:13
419+
--> $DIR/methods.rs:434:13
420420
|
421-
430 | let _ = (1..10).skip(10).next();
421+
434 | let _ = (1..10).skip(10).next();
422422
| ^^^^^^^^^^^^^^^^^^^^^^^
423423

424424
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
425-
--> $DIR/methods.rs:431:14
425+
--> $DIR/methods.rs:435:14
426426
|
427-
431 | let _ = &some_vec[..].iter().skip(3).next();
427+
435 | let _ = &some_vec[..].iter().skip(3).next();
428428
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
429429

430430
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
431-
--> $DIR/methods.rs:440:13
431+
--> $DIR/methods.rs:444:13
432432
|
433-
440 | let _ = opt.unwrap();
433+
444 | let _ = opt.unwrap();
434434
| ^^^^^^^^^^^^
435435
|
436436
= note: `-D option-unwrap-used` implied by `-D warnings`

0 commit comments

Comments
 (0)