Skip to content

Commit f4f3ec8

Browse files
authored
Merge pull request #6 from Buzzvil/add_recover_flag
Add recover flag
2 parents b541639 + 36a5b31 commit f4f3ec8

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ go install github.com/Buzzvil/recovergoroutine
1212

1313
## Usage
1414
```bash
15-
recovergoroutine ./...
15+
recovergoroutine -recover="" ./...
16+
17+
# -recover string
18+
# Custom recover method name. Currently, it is difficult to determine
19+
# if a CustomRecover function declared in another package is valid,
20+
# so this option can be used to resolve it.
1621
```
1722

1823
Check out the test cases for validation [examples](./test/src/faildata/failcode.go).

recovergoroutine/recovergoroutine.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package recovergoroutine
22

33
import (
4+
"flag"
45
"fmt"
56
"go/ast"
67
"go/parser"
@@ -10,13 +11,24 @@ import (
1011
"golang.org/x/tools/go/analysis"
1112
)
1213

14+
var customRecover string
15+
1316
func NewAnalyzer() *analysis.Analyzer {
1417
analyzer := &analysis.Analyzer{
1518
Name: "recovergoroutine",
1619
Doc: "finds goroutine code without recover",
1720
Run: run,
1821
}
1922

23+
analyzer.Flags.Init("recovergoroutine", flag.ExitOnError)
24+
analyzer.Flags.StringVar(
25+
&customRecover,
26+
"recover",
27+
"",
28+
"It is difficult to determine if a CustomRecover function declared in another package is valid,"+
29+
" so this option can be used to resolve it.",
30+
)
31+
2032
return analyzer
2133
}
2234

@@ -122,7 +134,7 @@ func hasRecover(expr ast.Node, pass *analysis.Pass) (bool, error) {
122134
return false
123135
}
124136

125-
if ok {
137+
if ok || n.Sel.Name == customRecover {
126138
result = true
127139
return false
128140
}

0 commit comments

Comments
 (0)