diff --git a/README.md b/README.md index e531027..a2437d6 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ This `go vet` compatible linter detects mixing pointer and value method receivers for the same type. +```bash +go install github.com/nikolaydubina/smrcptr@latest +``` + ```go type Pancake struct{} @@ -22,55 +26,11 @@ smrcptr/internal/bakery/pancake.go:7:1: Pancake.Fry uses pointer smrcptr/internal/bakery/pancake.go:9:1: Pancake.Bake uses value ``` -Why this is useful? Go has rules on how it can automatically select value and method receivers, which is complex and can lead to bugs. +Go has rules on how it can automatically select value and method receivers, which is complex and can lead to bugs. It is also common style recommendation [Go wiki](https://github.com/golang/go/wiki/CodeReviewComments#receiver-type) and [Google Go style guide](https://google.github.io/styleguide/go/decisions#receiver-type): > Don't mix receiver types. Choose either pointers or struct types for all available methods. -## Requirements - -```bash -go install github.com/nikolaydubina/smrcptr@latest -``` - -## Features - -### Skipping methods from standard packages - -Very common methods that has to have method receivers are skipped with `-skip-std=true` by default. - -- [encoding/json.UnmarshalJSON](https://pkg.go.dev/encoding/json#Unmarshaler) -- [encoding.UnmarshalText](https://pkg.go.dev/encoding#TextUnmarshaler) -- [encoding.UnmarshalBinary](https://pkg.go.dev/encoding/json#Unmarshaler) -- [encoding/xml.UnmarshalXML](https://pkg.go.dev/encoding/xml@go1.19.3#Unmarshaler) -- [encoding/xml.UnmarshalXMLAttr](https://pkg.go.dev/encoding/xml@go1.19.3#UnmarshalerAttr) -- [database/sql.Scan](https://pkg.go.dev/database/sql#Scanner) -- [fmt.Scan](https://pkg.go.dev/fmt#Scanner) -- [io.Read](https://pkg.go.dev/io#Reader) - -### Return Status Code - -When issue is detected, related info is printed and status code is non-zero. -This is similar as other `go vet` and linters. -This allows convenient use in CI. - -### Constructor - -It is also useful to detect if "construtor" functions that commonly start with `New...` returns value that matches used in receivers. - -```bash -$ smrcptr --constructor=true ./internal/... -smrcptr/internal/bakery/pancake.go:7:1: Pancake.Fry uses pointer -smrcptr/internal/bakery/pancake.go:5:1: Pancake.NewPancake uses value -smrcptr/internal/bakery/pancake.go:9:1: Pancake.Bake uses value -smrcptr/internal/bakery/pancake.go:14:1: Cake.Fry uses pointer -smrcptr/internal/bakery/pancake.go:16:1: Cake.Bake uses value -smrcptr/internal/bakery/pancake.go:23:1: Brownie.Bake uses pointer -smrcptr/internal/bakery/pancake.go:21:1: Brownie.NewBrownie uses value -smrcptr/internal/bakery/pancake.go:35:1: BadCookie.NewBadCookie uses pointer -smrcptr/internal/bakery/pancake.go:37:1: BadCookie.Bake uses value -``` - ## Existing Linters #### staticcheck