Giter Site home page Giter Site logo

go-mnd's Introduction

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

CI Go Report Card codecov

Install

Local

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

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

Github action

You can run go-mnd as a GitHub action as follows:

name: Example workflow
on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:
  tests:
    runs-on: ubuntu-latest
    env:
      GO111MODULE: on
    steps:
      - name: Checkout Source
        uses: actions/checkout@v2
      - name: Run go-mnd
        uses: tommy-muehle/go-mnd@master
        with:
          args: ./...

GitLab CI

You can run go-mnd inside a GitLab CI pipeline as follows:

stages:
  - lint

go:lint:mnd:
  stage: lint
  needs: []
  image: golang:latest
  before_script:
    - go get -u github.com/tommy-muehle/go-mnd/cmd/mnd
    - go mod tidy
    - go mod vendor
  script:
    - go vet -vettool $(which mnd) ./...

Homebrew

To install with Homebrew, run:

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

Docker

To get the latest available Docker image:

docker pull tommymuehle/go-mnd

Windows

On Windows download the latest release.

Usage

asciicast

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

or directly

mnd ./...

or via Docker

docker run --rm -v "$PWD":/app -w /app tommymuehle/go-mnd:latest ./...

Options

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

The -ignored-numbers option let's you define a comma separated list of numbers to ignore.
For example: -ignored-numbers=1000,10_000,3.14159264

The -ignored-functions option let's you define a comma separated list of function name regexp patterns to exclude.
For example: -ignored-functions=math.*,http.StatusText

The -ignored-files option let's you define a comma separated list of filename regexp patterns to exclude.
For example: -ignored-files=magic_.*.go,.*_numbers.go

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

Excludes

By default the numbers 0 and 1 as well as test files are excluded!

Further known excludes

The function "Date" in the "Time" package.

t := time.Date(2017, time.September, 26, 12, 13, 14, 0, time.UTC)

Additional custom excludes can be defined via option flag.

Development

Build

You can build the binary with:

make

Tests

You can run all unit tests using:

make test

And with coverage report:

make test-coverage

Docker image

You can also build locally the docker image by using the command:

make image

Stickers

Stickers image Sticker image

Just drop me a message via Twitter DM or email if you want some go-mnd stickers for you or your Gopher usergroup.

License

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

go-mnd's People

Contributors

ksoichiro avatar ldez avatar nyiyui avatar tommy-muehle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.