@@ -4709,6 +4709,8 @@ impl_lint_pass!(Methods => [
4709
4709
] ) ;
4710
4710
4711
4711
/// Extracts a method call name, args, and `Span` of the method name.
4712
+ /// This ensures that neither the receiver nor any of the arguments
4713
+ /// come from expansion.
4712
4714
pub fn method_call < ' tcx > (
4713
4715
recv : & ' tcx Expr < ' tcx > ,
4714
4716
) -> Option < ( & ' tcx str , & ' tcx Expr < ' tcx > , & ' tcx [ Expr < ' tcx > ] , Span , Span ) > {
@@ -4907,6 +4909,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
4907
4909
impl Methods {
4908
4910
#[ allow( clippy:: too_many_lines) ]
4909
4911
fn check_methods < ' tcx > ( & self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
4912
+ // Handle method calls whose receiver and arguments may not come from expansion
4910
4913
if let Some ( ( name, recv, args, span, call_span) ) = method_call ( expr) {
4911
4914
match ( name, args) {
4912
4915
( "add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub" , [ _arg] ) => {
@@ -5049,29 +5052,12 @@ impl Methods {
5049
5052
Some ( ( "err" , recv, [ ] , err_span, _) ) => {
5050
5053
err_expect:: check ( cx, expr, recv, span, err_span, self . msrv ) ;
5051
5054
} ,
5052
- _ => unwrap_expect_used:: check (
5053
- cx,
5054
- expr,
5055
- recv,
5056
- false ,
5057
- self . allow_expect_in_consts ,
5058
- self . allow_expect_in_tests ,
5059
- unwrap_expect_used:: Variant :: Expect ,
5060
- ) ,
5055
+ _ => { } ,
5061
5056
}
5062
5057
unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5063
5058
} ,
5064
- ( "expect_err" , [ _] ) => {
5059
+ ( "expect_err" , [ _] ) | ( "unwrap_err" | "unwrap_unchecked" | "unwrap_err_unchecked" , [ ] ) => {
5065
5060
unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5066
- unwrap_expect_used:: check (
5067
- cx,
5068
- expr,
5069
- recv,
5070
- true ,
5071
- self . allow_expect_in_consts ,
5072
- self . allow_expect_in_tests ,
5073
- unwrap_expect_used:: Variant :: Expect ,
5074
- ) ;
5075
5061
} ,
5076
5062
( "extend" , [ arg] ) => {
5077
5063
string_extend_chars:: check ( cx, expr, recv, arg) ;
@@ -5437,27 +5423,6 @@ impl Methods {
5437
5423
_ => { } ,
5438
5424
}
5439
5425
unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5440
- unwrap_expect_used:: check (
5441
- cx,
5442
- expr,
5443
- recv,
5444
- false ,
5445
- self . allow_unwrap_in_consts ,
5446
- self . allow_unwrap_in_tests ,
5447
- unwrap_expect_used:: Variant :: Unwrap ,
5448
- ) ;
5449
- } ,
5450
- ( "unwrap_err" , [ ] ) => {
5451
- unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5452
- unwrap_expect_used:: check (
5453
- cx,
5454
- expr,
5455
- recv,
5456
- true ,
5457
- self . allow_unwrap_in_consts ,
5458
- self . allow_unwrap_in_tests ,
5459
- unwrap_expect_used:: Variant :: Unwrap ,
5460
- ) ;
5461
5426
} ,
5462
5427
( "unwrap_or" , [ u_arg] ) => {
5463
5428
match method_call ( recv) {
@@ -5486,9 +5451,6 @@ impl Methods {
5486
5451
}
5487
5452
unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5488
5453
} ,
5489
- ( "unwrap_unchecked" | "unwrap_err_unchecked" , [ ] ) => {
5490
- unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5491
- } ,
5492
5454
( "unwrap_or_else" , [ u_arg] ) => {
5493
5455
match method_call ( recv) {
5494
5456
Some ( ( "map" , recv, [ map_arg] , _, _) )
@@ -5526,6 +5488,56 @@ impl Methods {
5526
5488
_ => { } ,
5527
5489
}
5528
5490
}
5491
+ // Handle method calls whose receiver and arguments may come from expansion
5492
+ if let ExprKind :: MethodCall ( path, recv, args, _call_span) = expr. kind {
5493
+ match ( path. ident . name . as_str ( ) , args) {
5494
+ ( "expect" , [ _] ) if !matches ! ( method_call( recv) , Some ( ( "ok" | "err" , _, [ ] , _, _) ) ) => {
5495
+ unwrap_expect_used:: check (
5496
+ cx,
5497
+ expr,
5498
+ recv,
5499
+ false ,
5500
+ self . allow_expect_in_consts ,
5501
+ self . allow_expect_in_tests ,
5502
+ unwrap_expect_used:: Variant :: Expect ,
5503
+ ) ;
5504
+ } ,
5505
+ ( "expect_err" , [ _] ) => {
5506
+ unwrap_expect_used:: check (
5507
+ cx,
5508
+ expr,
5509
+ recv,
5510
+ true ,
5511
+ self . allow_expect_in_consts ,
5512
+ self . allow_expect_in_tests ,
5513
+ unwrap_expect_used:: Variant :: Expect ,
5514
+ ) ;
5515
+ } ,
5516
+ ( "unwrap" , [ ] ) => {
5517
+ unwrap_expect_used:: check (
5518
+ cx,
5519
+ expr,
5520
+ recv,
5521
+ false ,
5522
+ self . allow_unwrap_in_consts ,
5523
+ self . allow_unwrap_in_tests ,
5524
+ unwrap_expect_used:: Variant :: Unwrap ,
5525
+ ) ;
5526
+ } ,
5527
+ ( "unwrap_err" , [ ] ) => {
5528
+ unwrap_expect_used:: check (
5529
+ cx,
5530
+ expr,
5531
+ recv,
5532
+ true ,
5533
+ self . allow_unwrap_in_consts ,
5534
+ self . allow_unwrap_in_tests ,
5535
+ unwrap_expect_used:: Variant :: Unwrap ,
5536
+ ) ;
5537
+ } ,
5538
+ _ => { } ,
5539
+ }
5540
+ }
5529
5541
}
5530
5542
}
5531
5543
0 commit comments