Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
check-circle

GitHub Action

ireturn-linter

v0.2.0

ireturn-linter

check-circle

ireturn-linter

Runs the Go's ireturn linter that checks for returned interfaces

Installation

Copy and paste the following snippet into your .yml file.

              

- name: ireturn-linter

uses: butuzov/[email protected]

Learn more about this action in butuzov/ireturn

Choose a version

ireturn Code Coverage build status MIT License

Accept Interfaces, Return Concrete Types

Install

You can get ireturn with go install command. Go1.18+ required.

go install github.com/butuzov/ireturn/cmd/ireturn@latest

Compiled Binary

Or you can download the suitable binary from the releases section.

Usage

ireturn work with two arguments (but allow to use of only one of them in the same moment):

  • accept - accept-list of the comma-separated interfaces.
  • reject - reject-list of the comma-separated interfaces.

By default, ireturn will accept all errors (error), empty interfaces (interfaces{}), anonymous interfaces declarations ( interface { methodName() } ), interfaces from standard library, and generic interfaces (introduced in go1.18) as a valid ones.

Interfaces in the list can be provided as regexps or keywords ("error" for "error", "empty" for interface{}, anon for anonymous interfaces):

# allow usage of empty interfaces, errors and Doer interface from any package.
ireturn --accept="\\.Doer,error,empty" ./...
# reject standard library interfaces and plinko.Payload as valid ones
ireturn --reject="std,github.com/shipt/plinko.Payload" ./...
# default settings allows errors, empty interfaces, anonymous declarations and standard library
ireturn ./...
# checkfor non idiomatic interface names
ireturn -allow="error,generic,anon,stdlib,.*(or|er)$" ./...

Keywords

You can use shorthand for some types of interfaces:

  • empty for interface{} type
  • anon for anonymous declarations interface{ someMethod() }
  • error for error type
  • stdlib for all interfaces from standard library.
  • generic for generic interfaces (added in go1.18)

Disable directive

golangci-lint compliant disable directive //nolint: ireturn can be used with ireturn

GitHub Action

- uses: butuzov/ireturn-linter@main
  with:
    allow: "error,empty"

Examples

// Bad.
type Doer interface { Do() }
type IDoer struct{}
func New() Doer { return new(IDoer)}
func (d *IDoer) Do() {/*...*/}

// Good.
type Doer interface { Do() }
type IDoer struct{}
func New() *IDoer { return new(IDoer)}
func (d *IDoer) Do() {/*...*/}

// Very Good (Verify Interface Compliance in compile time)
var _ Doer = (*IDoer)(nil)

type Doer interface { Do() }
type IDoer struct{}
func New() *IDoer { return new(IDoer)}
func (d *IDoer) Do() {/*...*/}

Reading List