smrcptr

command module
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2024 License: MIT Imports: 2 Imported by: 0

README

Same Receiver Pointer (smrcptr)

go-recipes Go Report Card OpenSSF Scorecard

Don't mix receiver types. Choose either pointers or struct types for all available methods.

Go has rules on how it automatically selects either value or method receivers, which is complex and can lead to bugs. Therefore, it is a common style recommendation[^1][^2].

go install github.com/nikolaydubina/smrcptr@latest
type Pancake struct{}

func NewPancake() Pancake { return Pancake{} }

func (s *Pancake) Fry() {}

func (s Pancake) Bake() {}
$ smrcptr ./...
/pancake.go:12:1: Pancake.Fry uses pointer
/pancake.go:10:1: Pancake.NewPancake uses value
/pancake.go:14:1: Pancake.Bake uses value

Existing Linters

staticcheck

As of 2022-11-30, it does not detect that pointer and value method receivers are mixed. Most relevant analyzer ST1016 checks only name of method receiver.

$ staticcheck -checks ST1016 ./...    
main.go:9:18: methods on the same type should have the same receiver name (seen 1x "v", 2x "s") (ST1016)

Using all analyzers does not detect it either.

staticcheck -checks all ./...
main.go:9:18: methods on the same type should have the same receiver name (seen 1x "v", 2x "s") (ST1016)

References

[^1]: Go wiki https://github.com/golang/go/wiki/CodeReviewComments#receiver-type [^2]: Google Go style guide https://google.github.io/styleguide/go/decisions#receiver-type

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
analysis
internal
bakery
Code generated by pancake factory DO NOT EDIT.
Code generated by pancake factory DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL