Help us shape the future of Go! Take the 2026 Go Developer Survey

magic_numbers

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2019 License: MIT Imports: 7 Imported by: 4

README

go-mnd - Magic number detector for Golang

A vet analyzer to detect magic numbers.

What is a magic number?
A magic number is a numeric literal that is not defined as a constant, but which may change, and therefore can be hard to update. It's considered a bad programming practice to use numbers directly in any source code without an explanation. It makes programs harder to read, understand, and maintain.

Project status

Build Status Go Report Card

Install

This analyzer requires Golang in version >= 1.12 because it's depends on the go/analysis API.

go get github.com/tommy-muehle/go-mnd/cmd/mnd

To install with Homebrew, run:

brew tap tommy-muehle/tap && brew install tommy-muehle/tap/mnd

On Windows download the latest release.

Usage

asciicast

go vet -vettool $(which mnd) ./...

or directly

mnd ./...

The -checks option let's you define a comma separated list of checks.

Checks

By default this detector analyses arguments, assigns, cases, conditions, operations and return statements.

  • argument
t := http.StatusText(200)
  • assign
c := &http.Client{
    Timeout: 5 * time.Second,
}
  • case
switch x {
    case 3:
}
  • condition
if x > 7 {
}
  • operation
var x, y int
y = 10 * x
  • return
return 3

Notices

By default the number 0 is excluded!

License

The MIT License (MIT). Please see LICENSE for more information.

Documentation

Index

Constants

View Source
const Doc = `magic number detector`

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:             "mnd",
	Doc:              Doc,
	Run:              run,
	Flags:            options(),
	Requires:         []*analysis.Analyzer{inspect.Analyzer},
	RunDespiteErrors: true,
}

Functions

This section is empty.

Types

type Checker

type Checker interface {
	NodeFilter() []ast.Node
	Check(n ast.Node)
}

type Config

type Config struct {
	Checks map[string]bool
}

func DefaultConfig

func DefaultConfig() *Config

func WithOptions

func WithOptions(options ...Option) *Config

func (*Config) IsCheckEnabled

func (c *Config) IsCheckEnabled(name string) bool

type Option

type Option func(config *Config)

func WithCustomChecks

func WithCustomChecks(checks string) Option

Directories

Path Synopsis
cmd
mnd command
examples
bad command
good command
tools
printer command

Jump to

Keyboard shortcuts

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