Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qawatake/named
Linter: named ensures a target function to be called with a named return value.
https://github.com/qawatake/named
go golang linter staticana
Last synced: 15 days ago
JSON representation
Linter: named ensures a target function to be called with a named return value.
- Host: GitHub
- URL: https://github.com/qawatake/named
- Owner: qawatake
- License: mit
- Created: 2023-10-23T12:20:48.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-11T08:05:55.000Z (about 1 year ago)
- Last Synced: 2024-10-19T09:39:25.344Z (2 months ago)
- Topics: go, golang, linter, staticana
- Language: Go
- Homepage: https://pkg.go.dev/github.com/qawatake/named
- Size: 44.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# named
[![Go Reference](https://pkg.go.dev/badge/github.com/qawatake/named.svg)](https://pkg.go.dev/github.com/qawatake/named)
[![test](https://github.com/qawatake/named/actions/workflows/test.yaml/badge.svg)](https://github.com/qawatake/named/actions/workflows/test.yaml)
[![codecov](https://codecov.io/gh/qawatake/named/graph/badge.svg?token=0XZh5C4Gq8)](https://codecov.io/gh/qawatake/named)Linter `named` ensures a target function to be called with a named return value.
A typical use case is to prevent the misuse of a error wrapping function such as [derrors.Wrap](https://github.com/golang/pkgsite/blob/5f0513d53cff8382238b5f8c78e8317d2b4ad06d/internal/derrors/derrors.go#L240), which does not allow the resulted error to be unwrapped.
Function `Bad` below fails to wrap the error simply because it doesn't use the named return value as an argument.```go
func Good() (err error) {
err = DoSomething()
defer Wrap(&err, "wrapped!!") // ok because a named return value is passed.
return fmt.Errorf("error from Good: %w", err)
}func Bad() error {
err := DoSomething()
defer Wrap(&err, "wrapped!!") // <- err is not a named return value.
return fmt.Errorf("error from Bad: %w", err)
}func Wrap(errp *error, msg string) {
if *errp == nil {
return
}
*errp = fmt.Errorf("%s: %w", msg, *errp)
}func DoSomething() error {
return errors.New("original error")
}func main() {
err := Good()
fmt.Println(err)
err = Bad()
fmt.Println(err)
// Output:
// wrapped!!: error from Good: original error
// error from Bad: original error
}
```## How to use
Build your `named` binary by writing `main.go` like below.
```go
package mainimport (
"github.com/qawatake/named"
"golang.org/x/tools/go/analysis/unitchecker"
)func main() {
unitchecker.Main(
named.NewAnalyzer(
named.Deferred{
PkgPath: "pkg/in/which/target/func/is/defined",
FuncName: "Wrap",
ArgPos: 0,
},
),
)
}
```Then, run `go vet` with your `named` binary.
```sh
go vet -vettool=/path/to/your/named ./...
```