/gorecover

🔧可集成golangci-lint的协程内部静态检查recover的插件

Primary LanguageGoMIT LicenseMIT

goroutinen 静态检查recover 插件

用来检查某些场景下goroutine内需要捕获panic,防止不必要的程序异常停止

使用:

./gorecover ../../testdata/src/p/main.go

// 输出检查的异常
gorecover/testdata/src/p/main.go:9:5: goroutine should have recover in defer func

package main

import (
	"fmt"
	"runtime"
)

func goFuncWithRecover() {
	go func() { // want "goroutine should have recover in defer func"
	}()
	
	go func() {
		defer func() {
			recover()
		}()
	}()

	go HandlerPanic(func() {
		panic("panic2")
	})
}

func HandlerPanic(f func()) {
	defer func() {
		if r := recover(); r != nil {
			buf := make([]byte, 64<<10)
			buf = buf[:runtime.Stack(buf, false)]
			fmt.Println(r)
		}
	}()

	f()
}

func main() {
	goFuncWithRecover()
}

集成到golangci-lint使用

  1. 创建文件 golangci-lint/pkg/golinters/gorecover.go
package golinters

import (
"github.com/wangxudong123/gorecover/analyzer"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"golang.org/x/tools/go/analysis"
)

func NewGoRecoverCheck() *goanalysis.Linter {
    return goanalysis.NewLinter(
            analyzer.Analyzer.Name,
            analyzer.Analyzer.Doc,
            []*analysis.Analyzer{analyzer.Analyzer},
            nil,
        ).WithLoadMode(goanalysis.LoadModeSyntax)
}
  1. 导入配置 golangci-lint/pkg/lint/lintersdb/manager.go
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {

	// 一坨代码
	...
	
	// 导入NewGoRecoverCheck配置 
	linter.NewConfig(golinters.NewGoRecoverCheck()).
		WithSince("v1.0.0").
		WithPresets(linter.PresetStyle, linter.PresetBugs).
		WithURL("https://github.com/wangxudong123/gorecover"),
	
}
  1. 重新编译golangci-lint

  2. 在你的项目中.golangci.yml中添加gorecover 静态检查项

linters:
  disable-all: true # 关闭全部检查
  enable: # 打开下面的检查选项
    - gorecover

.golangci.yml 相关配置

Go Versions

Version Supported
1.18.x

Golangci-lint Versions

Version Supported
1.47.3