recvcheck

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: MIT Imports: 4 Imported by: 1

README

recvcheck

.github/workflows/build.yaml Go Report Card
Golang linter for check receiver type in method

Motivation

From Go Wiki: Go Code Review Comments - The Go Programming Language

Don’t mix receiver types. Choose either pointers or struct types for all available method

Following code from Dave Cheney causes data race. Could you find it? This linter does it for you.

package main

import (
        "fmt"
        "time"
)

type RPC struct {
        result int
        done   chan struct{}
}

func (rpc *RPC) compute() {
        time.Sleep(time.Second) // strenuous computation intensifies
        rpc.result = 42
        close(rpc.done)
}

func (RPC) version() int {
        return 1 // never going to need to change this
}

func main() {
        rpc := &RPC{done: make(chan struct{})}

        go rpc.compute()         // kick off computation in the background
        version := rpc.version() // grab some other information while we're waiting
        <-rpc.done               // wait for computation to finish
        result := rpc.result

        fmt.Printf("RPC computation complete, result: %d, version: %d\n", result, version)
}

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer added in v0.2.0

func NewAnalyzer(s Settings) *analysis.Analyzer

NewAnalyzer returns a new analyzer to check for receiver type consistency.

Types

type Settings added in v0.2.0

type Settings struct {
	// DisableBuiltin if true, disables the built-in method excludes.
	// Built-in excluded methods:
	//   - "MarshalText"
	//   - "MarshalJSON"
	//   - "MarshalYAML"
	//   - "MarshalXML"
	//   - "MarshalBinary"
	//   - "GobEncode"
	DisableBuiltin bool

	// Exclusions format is `struct_name.method_name` (ex: `Foo.MethodName`).
	// A wildcard `*` can use as a struct name (ex: `*.MethodName`).
	Exclusions []string
}

Settings is the configuration for the analyzer.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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