@@ -4709,6 +4709,8 @@ impl_lint_pass!(Methods => [
47094709] ) ;
47104710
47114711/// 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.
47124714pub fn method_call < ' tcx > (
47134715 recv : & ' tcx Expr < ' tcx > ,
47144716) -> Option < ( & ' tcx str , & ' tcx Expr < ' tcx > , & ' tcx [ Expr < ' tcx > ] , Span , Span ) > {
@@ -4907,6 +4909,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
49074909impl Methods {
49084910 #[ allow( clippy:: too_many_lines) ]
49094911 fn check_methods < ' tcx > ( & self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
4912+ // Handle method calls whose receiver and arguments may not come from expansion
49104913 if let Some ( ( name, recv, args, span, call_span) ) = method_call ( expr) {
49114914 match ( name, args) {
49124915 ( "add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub" , [ _arg] ) => {
@@ -5049,29 +5052,12 @@ impl Methods {
50495052 Some ( ( "err" , recv, [ ] , err_span, _) ) => {
50505053 err_expect:: check ( cx, expr, recv, span, err_span, self . msrv ) ;
50515054 } ,
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+ _ => { } ,
50615056 }
50625057 unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
50635058 } ,
5064- ( "expect_err" , [ _] ) => {
5059+ ( "expect_err" , [ _] ) | ( "unwrap_err" | "unwrap_unchecked" | "unwrap_err_unchecked" , [ ] ) => {
50655060 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- ) ;
50755061 } ,
50765062 ( "extend" , [ arg] ) => {
50775063 string_extend_chars:: check ( cx, expr, recv, arg) ;
@@ -5437,27 +5423,6 @@ impl Methods {
54375423 _ => { } ,
54385424 }
54395425 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- ) ;
54615426 } ,
54625427 ( "unwrap_or" , [ u_arg] ) => {
54635428 match method_call ( recv) {
@@ -5486,9 +5451,6 @@ impl Methods {
54865451 }
54875452 unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
54885453 } ,
5489- ( "unwrap_unchecked" | "unwrap_err_unchecked" , [ ] ) => {
5490- unnecessary_literal_unwrap:: check ( cx, expr, recv, name, args) ;
5491- } ,
54925454 ( "unwrap_or_else" , [ u_arg] ) => {
54935455 match method_call ( recv) {
54945456 Some ( ( "map" , recv, [ map_arg] , _, _) )
@@ -5526,6 +5488,56 @@ impl Methods {
55265488 _ => { } ,
55275489 }
55285490 }
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+ }
55295541 }
55305542}
55315543
0 commit comments