Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Dubina <[email protected]>
  • Loading branch information
nikolaydubina authored Apr 9, 2024
1 parent 2a3f3f6 commit 8d889ef
Showing 1 changed file with 5 additions and 45 deletions.
50 changes: 5 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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/[email protected]#Unmarshaler)
- [encoding/xml.UnmarshalXMLAttr](https://pkg.go.dev/encoding/[email protected]#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
Expand Down

0 comments on commit 8d889ef

Please sign in to comment.