@@ -4661,6 +4661,8 @@ impl_lint_pass!(Methods => [
4661
4661
] ) ;
4662
4662
4663
4663
/// Extracts a method call name, args, and `Span` of the method name.
4664
+ /// This ensures that neither the receiver nor any of the arguments
4665
+ /// come from expansion.
4664
4666
pub fn method_call < ' tcx > (
4665
4667
recv : & ' tcx Expr < ' tcx > ,
4666
4668
) -> Option < ( & ' tcx str , & ' tcx Expr < ' tcx > , & ' tcx [ Expr < ' tcx > ] , Span , Span ) > {
@@ -4858,6 +4860,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
4858
4860
impl Methods {
4859
4861
#[ allow( clippy:: too_many_lines) ]
4860
4862
fn check_methods < ' tcx > ( & self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
4863
+ // Handle method calls whose receiver and arguments may not come from expansion
4861
4864
if let Some ( ( name, recv, args, span, call_span) ) = method_call ( expr) {
4862
4865
match ( name, args) {
4863
4866
( "add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub" , [ _arg] ) => {
@@ -5000,29 +5003,12 @@ impl Methods {
5000
5003
Some ( ( "err" , recv, [ ] , err_span, _) ) => {
5001
5004
err_expect:: check ( cx, expr, recv, span, err_span, self . msrv ) ;
5002
5005
} ,
5003
- _ => unwrap_expect_used:: check (
5004
- cx,
5005
- expr,
5006
- recv,
5007
- false ,
5008
- self . allow_expect_in_consts ,
5009
- self . allow_expect_in_tests ,
5010
- unwrap_expect_used:: Variant :: Expect ,
5011
- ) ,
5006
+ _ => { } ,
5012
5007
}
5013
5008
unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5014
5009
} ,
5015
- ( "expect_err" , [ _] ) => {
5010
+ ( "expect_err" , [ _] ) | ( "unwrap_err" | "unwrap_unchecked" | "unwrap_err_unchecked" , [ ] ) => {
5016
5011
unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5017
- unwrap_expect_used:: check (
5018
- cx,
5019
- expr,
5020
- recv,
5021
- true ,
5022
- self . allow_expect_in_consts ,
5023
- self . allow_expect_in_tests ,
5024
- unwrap_expect_used:: Variant :: Expect ,
5025
- ) ;
5026
5012
} ,
5027
5013
( "extend" , [ arg] ) => {
5028
5014
string_extend_chars:: check ( cx, expr, recv, arg) ;
@@ -5388,27 +5374,6 @@ impl Methods {
5388
5374
_ => { } ,
5389
5375
}
5390
5376
unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5391
- unwrap_expect_used:: check (
5392
- cx,
5393
- expr,
5394
- recv,
5395
- false ,
5396
- self . allow_unwrap_in_consts ,
5397
- self . allow_unwrap_in_tests ,
5398
- unwrap_expect_used:: Variant :: Unwrap ,
5399
- ) ;
5400
- } ,
5401
- ( "unwrap_err" , [ ] ) => {
5402
- unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5403
- unwrap_expect_used:: check (
5404
- cx,
5405
- expr,
5406
- recv,
5407
- true ,
5408
- self . allow_unwrap_in_consts ,
5409
- self . allow_unwrap_in_tests ,
5410
- unwrap_expect_used:: Variant :: Unwrap ,
5411
- ) ;
5412
5377
} ,
5413
5378
( "unwrap_or" , [ u_arg] ) => {
5414
5379
match method_call ( recv) {
@@ -5437,9 +5402,6 @@ impl Methods {
5437
5402
}
5438
5403
unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5439
5404
} ,
5440
- ( "unwrap_unchecked" | "unwrap_err_unchecked" , [ ] ) => {
5441
- unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5442
- } ,
5443
5405
( "unwrap_or_else" , [ u_arg] ) => {
5444
5406
match method_call ( recv) {
5445
5407
Some ( ( "map" , recv, [ map_arg] , _, _) )
@@ -5477,6 +5439,56 @@ impl Methods {
5477
5439
_ => { } ,
5478
5440
}
5479
5441
}
5442
+ // Handle method calls whose receiver and arguments may come from expansion
5443
+ if let ExprKind :: MethodCall ( path, recv, args, _call_span) = expr. kind {
5444
+ match ( path. ident . name . as_str ( ) , args) {
5445
+ ( "expect" , [ _] ) if !matches ! ( method_call( recv) , Some ( ( "ok" | "err" , _, [ ] , _, _) ) ) => {
5446
+ unwrap_expect_used:: check (
5447
+ cx,
5448
+ expr,
5449
+ recv,
5450
+ false ,
5451
+ self . allow_expect_in_consts ,
5452
+ self . allow_expect_in_tests ,
5453
+ unwrap_expect_used:: Variant :: Expect ,
5454
+ ) ;
5455
+ } ,
5456
+ ( "expect_err" , [ _] ) => {
5457
+ unwrap_expect_used:: check (
5458
+ cx,
5459
+ expr,
5460
+ recv,
5461
+ true ,
5462
+ self . allow_expect_in_consts ,
5463
+ self . allow_expect_in_tests ,
5464
+ unwrap_expect_used:: Variant :: Expect ,
5465
+ ) ;
5466
+ } ,
5467
+ ( "unwrap" , [ ] ) => {
5468
+ unwrap_expect_used:: check (
5469
+ cx,
5470
+ expr,
5471
+ recv,
5472
+ false ,
5473
+ self . allow_unwrap_in_consts ,
5474
+ self . allow_unwrap_in_tests ,
5475
+ unwrap_expect_used:: Variant :: Unwrap ,
5476
+ ) ;
5477
+ } ,
5478
+ ( "unwrap_err" , [ ] ) => {
5479
+ unwrap_expect_used:: check (
5480
+ cx,
5481
+ expr,
5482
+ recv,
5483
+ true ,
5484
+ self . allow_unwrap_in_consts ,
5485
+ self . allow_unwrap_in_tests ,
5486
+ unwrap_expect_used:: Variant :: Unwrap ,
5487
+ ) ;
5488
+ } ,
5489
+ _ => { } ,
5490
+ }
5491
+ }
5480
5492
}
5481
5493
}
5482
5494
0 commit comments