returnstyles

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2023 License: MIT Imports: 10 Imported by: 0

README

returnstyles GoDev Build GitHub release

Go (golang) linter to check function return styles according to rules of your choice.

Install

go install -v github.com/gavv/returnstyles/cmd/returnstyles@latest

Usage

returnstyles [options] <package>

For example:

returnstyles -allow-naked-returns=false .

Options

option default description
-allow-unnamed true allow functions with unnamed return variables
-allow-named true allow functions with named return variables
-allow-partially-named false allow functions with partially named return variables
-allow-normal-returns true allow normal (non-naked) returns in functions with named return variables
-allow-naked-returns true allow naked returns in functions with named return variables
-allow-mixing-returns false allow mixing normal and naked in functions with named return variables

Config

Instead of specifying individual command-line options, you can provide a single configuration file in YAML format:

returnstyles -config path/to/config.yaml <package>

It should have the following format:

returnstyles:
  allow-unnamed: true
  allow-named: true
  allow-partially-named: false
  allow-normal-returns: true
  allow-naked-returns: true
  allow-mixing-returns: false

Package

import "github.com/gavv/returnstyles"

Global variable returnstyles.Analyzer follows guidelines in the golang.org/x/tools/go/analysis package.

Samples

-allow-unnamed=false
func foo() (int, int) { // lint: functions with unnamed return variables not allowed
    return 11, 22
}
-allow-named=false
func foo() (a int, b int) { // lint: functions with named return variables not allowed
    return 11, 22
}
-allow-partially-named=false
func foo() (int, b int) { // lint: functions with partially named return variables not allowed
    return 11, 22
}
-allow-normal-returns=false
func foo() (a int, b int) {
    return 11, 22 // lint: normal (non-naked) returns not allowed
                  // in functions with named return variables
}
-allow-naked-returns=false
func foo() (a int, b int) {
    a = 11
    b = 22
    return // lint: naked returns not allowed
}
-allow-mixing-returns=false
func foo() (a int, b int) {
    if cond() {
        return 11, 22
    } else {
        a = 11
        b = 22
        return // lint: mixing normal and naked returns not allowed
    }
}

func bar() (a int, b int) {
    if cond() {
        a = 11
        b = 22
        return
    } else {
        return 11, 22 // lint: mixing normal and naked returns not allowed
    }
}

Authors

See here.

License

MIT

Documentation

Index

Constants

View Source
const (
	ConfigFlag              = "config"
	AllowUnnamedFlag        = "allow-unnamed"
	AllowNamedFlag          = "allow-named"
	AllowPartiallyNamedFlag = "allow-partially-named"
	AllowNormalReturnsFlag  = "allow-normal-returns"
	AllowNakedReturnsFlag   = "allow-naked-returns"
	AllowMixingReturnsFlag  = "allow-mixing-returns"
)

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "returnstyles",
	Doc:      "Return styles linter",
	URL:      "https://github.com/gavv/returnstyles",
	Flags:    flags(),
	Run:      run,
	Requires: []*analysis.Analyzer{inspect.Analyzer},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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