Skip to content

Commit

Permalink
any name for error, not just 'err'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiang-Gianni committed Dec 28, 2023
1 parent 15436a5 commit 6bcf11d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 32 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# deferror

Deferror is a Go linter that suggests a customly made `defer` function call when the function has a return error named `err` and the function does not already start with a `defer` call.
Deferror is a Go linter that suggests a customly made `defer` function call when the function has a return named error and the function does not already start with a `defer` call.

## Install

Expand Down Expand Up @@ -51,6 +51,7 @@ type F struct {
RecvType string
RecvPointer bool
Params []P
ErrName string
}

type P struct {
Expand Down Expand Up @@ -82,7 +83,7 @@ func (r *R) MyExample(now time.Duration, i *int) (a int, err error) {
The input data map for the template is:

```go
&{PkgName:example PkgPath:github.com/Jiang-Gianni/deferror/example FnName:MyExample RecvName:r RecvType:*R RecvPointer:true Params:[{Name:now Type:time.Duration Pointer:false} {Name:i Type:*int Pointer:true}]}
&{PkgName:example PkgPath:github.com/Jiang-Gianni/deferror/example FnName:MyExample RecvName:r RecvType:*R RecvPointer:true Params:[{Name:now Type:time.Duration Pointer:false} {Name:i Type:*int Pointer:true}] ErrName: err}
```

The following defer call is suggested with the default template:
Expand Down
16 changes: 7 additions & 9 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@ import (
"go/ast"
)

func (a *A) namedReturnErr(fd *ast.FuncDecl) bool {
// Return true/false and the name of the error
func (a *A) namedReturnErr(fd *ast.FuncDecl) (bool, string) {
if fd.Type == nil || fd.Type.Results == nil || fd.Type.Results.List == nil {
return false
return false, ""
}
for _, field := range fd.Type.Results.List {
ident, ok := field.Type.(*ast.Ident)
if ok && ident.Name == "error" {
for _, name := range field.Names {
if name.Name == "err" {
return true
}
}
if ok && ident.Name == "error" && field.Names != nil && len(field.Names) > 0 && field.Names[0].Name != "" {
return true, field.Names[0].Name
}
}
return false
return false, ""
}

// Function body start with a defer call
func (a *A) deferStart(fd *ast.FuncDecl) bool {
if len(fd.Body.List) == 0 {
return false
Expand Down
4 changes: 2 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ var initCmd = &cobra.Command{
}

var tmplContents = []byte(`{{define "main"}} defer func({{template "input" .}}) {
if err != nil {
err = fmt.Errorf("{{if .RecvName}}{{.RecvName}}.{{end}}{{.FnName}}({{template "percV" .}}): %w", {{template "plist" .}}{{if .Params}} ,{{end}}err)
if {{.ErrName}} != nil {
{{.ErrName}} = fmt.Errorf("{{if .RecvName}}{{.RecvName}}.{{end}}{{.FnName}}({{template "percV" .}}): %w", {{template "plist" .}}{{if .Params}} ,{{end}}{{.ErrName}})
}
}({{template "plist" .}})
{{end}}
Expand Down
4 changes: 2 additions & 2 deletions deferror.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func run(pass *analysis.Pass) (any, error) {
a.inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.FuncDecl:
if a.namedReturnErr(n) && !a.deferStart(n) {
a.report(n)
if ok, name := a.namedReturnErr(n); ok && !a.deferStart(n) {
a.report(n, name)
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions deferror.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{define "main"}} defer func({{template "input" .}}) {
if err != nil {
err = fmt.Errorf("{{if .RecvName}}{{.RecvName}}.{{end}}{{.FnName}}({{template "percV" .}}): %w", {{template "plist" .}}{{if .Params}} ,{{end}}err)
if {{.ErrName}} != nil {
{{.ErrName}} = fmt.Errorf("{{if .RecvName}}{{.RecvName}}.{{end}}{{.FnName}}({{template "percV" .}}): %w", {{template "plist" .}}{{if .Params}} ,{{end}}{{.ErrName}})
}
}({{template "plist" .}})
{{end}}
Expand Down
23 changes: 12 additions & 11 deletions example/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ func (r *R) MyExample(now time.Duration, i *int) (a int, err error) {
return 0, fmt.Errorf("new err is right here")
}

/*
Default
/* Default:
defer func(now time.Duration, i *int) {
if err != nil {
err = fmt.Errorf("r.MyExample(%v, %v): %w", now, i ,err)
}
}(now, i)
*/
// defer func(now time.Duration, i *int) {
// if err != nil {
// err = fmt.Errorf("r.MyExample(%v, %v): %w", now, i ,err)
// }
// }(now, i)

/*
Wrap

/* Wrap:
defer dfrr.Wrap(&err, "r.MyExample(%v, %v)",now, i)
*/
// defer dfrr.Wrap(&err, "r.MyExample(%v, %v)",now, i)
1 change: 1 addition & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type F struct {
RecvType string
RecvPointer bool
Params []P
ErrName string
}

type P struct {
Expand Down
3 changes: 2 additions & 1 deletion report.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"golang.org/x/tools/go/analysis"
)

func (a *A) report(fd *ast.FuncDecl) {
func (a *A) report(fd *ast.FuncDecl, errName string) {

f := &F{
PkgName: a.pass.Pkg.Name(),
PkgPath: a.pass.Pkg.Path(),
FnName: fd.Name.String(),
Params: params(fd),
ErrName: errName,
}
f.RecvName, f.RecvType, f.RecvPointer = receiver(fd)
// fmt.Printf("\n%+v\n", f)
Expand Down
4 changes: 2 additions & 2 deletions templates/default.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{define "main"}} defer func({{template "input" .}}) {
if err != nil {
err = fmt.Errorf("{{if .RecvName}}{{.RecvName}}.{{end}}{{.FnName}}({{template "percV" .}}): %w", {{template "plist" .}}{{if .Params}} ,{{end}}err)
if {{.ErrName}} != nil {
{{.ErrName}} = fmt.Errorf("{{if .RecvName}}{{.RecvName}}.{{end}}{{.FnName}}({{template "percV" .}}): %w", {{template "plist" .}}{{if .Params}} ,{{end}}err)
}
}({{template "plist" .}})
{{end}}
Expand Down
2 changes: 1 addition & 1 deletion templates/wrap.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{define "main"}} defer dfrr.Wrap(&err, "{{if .RecvName}}{{.RecvName}}.{{end}}{{.FnName}}({{template "percV" .}})"{{if .Params}},{{end}}{{template "plist" .}})
{{define "main"}} defer dfrr.Wrap(&{{.ErrName}}, "{{if .RecvName}}{{.RecvName}}.{{end}}{{.FnName}}({{template "percV" .}})"{{if .Params}},{{end}}{{template "plist" .}})
{{end}}

{{define "plist"}}{{range $index, $item := .Params}}{{if $index}}, {{end}}{{$item.Name}}{{end}}{{end}}
Expand Down

0 comments on commit 6bcf11d

Please sign in to comment.