Repository: mgechev/revive (go)

Issue:
False positive of `return in a defer function has no effect`
**Describe the bug**
Unexpected `return in a defer function has no effect`

**To Reproduce**
1. use the 1.3.9 version
2. use this go code
```go
package main

func verify(a any) func() {
	return func() {
		fn := a.(func() error)
		if err := fn(); err != nil {
			panic(err)
		}
	}

}

func main() {
	defer verify(func() error {
		return nil
	})()
}
```

3. use this config
As of my understanding, it disable the `call-chain` defer check but enable the `return` defer check, which is our case.
```toml
[rule.defer]
  arguments=[["return"]]
```

**Expected behavior**
No `return in a defer function has no effect`

**Desktop (please complete the following information):**
 - OS: macOS
 - Version of Go: 1.22

**Additional context**
This issue is similar to #863, which is also the one first on Google, but this issue is not exactly the same.


Pull request description:
fix #1029
Closes #1029 by skipping the analysis of function call arguments


Patch:
diff --git a/rule/defer.go b/rule/defer.go
index adc6478..9a19722 100644
--- a/rule/defer.go
+++ b/rule/defer.go
@@ -111,7 +111,7 @@ func (w lintDeferRule) Visit(node ast.Node) ast.Visitor {
 			// but it is very likely to be a misunderstanding of defer's behavior around arguments.
 			w.newFailure("recover must be called inside a deferred function, this is executing recover immediately", n, 1, "logic", "immediate-recover")
 		}
-
+		return nil // no need to analyze the arguments of the function call
 	case *ast.DeferStmt:
 		if isIdent(n.Call.Fun, "recover") {
 			// defer recover()


Test patch:
diff --git a/testdata/defer.go b/testdata/defer.go
index a035cfe..2cb4f57 100644
--- a/testdata/defer.go
+++ b/testdata/defer.go
@@ -47,3 +47,20 @@ func f() {
 		return nil
 	})
 }
+
+// Issue #1029
+func verify2(a any) func() {
+	return func() {
+		fn := a.(func() error)
+		if err := fn(); err != nil {
+			panic(err)
+		}
+	}
+
+}
+
+func mainf() {
+	defer verify2(func() error { // MATCH /prefer not to defer chains of function calls/
+		return nil
+	})()
+}