• Stars
    star
    191
  • Rank 201,864 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Magic number detector for Go.

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/v2/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/v2/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,make

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.

More Repositories

1

tooly-composer-script

Simple composer script to manage phar files using project composer.json.
PHP
101
star
2

puppet-vagrant-boxes

Build fast vagrant-base-boxes with veewee and puppet for further usage.
Shell
63
star
3

error-log-parser

Simple PHP library to parse Apache or Nginx error-log file entries for further usage.
PHP
21
star
4

puppet-typo3

Work on this repository has moved to move-elevator/puppet-typo3.
Puppet
17
star
5

php-hooks

PhpHooks is a collection of some useful customizable checks that you can use as git pre-commit hook before you commit some changes.
PHP
8
star
6

php-composer-plugin-devenv

Development environment to build Composer plugins.
PHP
7
star
7

docker-alpine-php-nightly

Docker Image to run PHP nightly builds. Currently 8.0.0-dev.
Dockerfile
5
star
8

silex-sitemap-service-provider

Simple sitemap generator provider for Silex.
PHP
5
star
9

speakerdeck_exporter

Unofficial Prometheus Exporter for SpeakerDeck.
Go
5
star
10

puppet-typo3-testbox

Testbox for puppet module typo3.
Puppet
5
star
11

composer-tool-installer-plugin

Install binary project requirements with composer.
PHP
5
star
12

php-repository-starter

My personal starter template for a new PHP project.
4
star
13

php-in-memoriam-extension

My farewell gift for the remaining colleagues at @move-elevator.
C
4
star
14

php-daily-business

My personal list of libraries to solve daily business challenges.
4
star
15

docker-phpstan

Work on this repository has moved to phpstan/docker-image
Makefile
4
star
16

go-jira-prepare-commit-msg

Git hook to add JIRA issue key from branch-name (if it exists) and add it before the commit message.
Go
3
star
17

awesome-dockerized-php-tools

List of awesome Dockerized PHP tools.
3
star
18

silex-doctrine-orm-service-provider

PHP
2
star
19

keynote-defensive-programming

Code snippets for my keynote "Defense programming (with PHP)".
PHP
2
star
20

php-time-conversion

Convert, compare and operate with time in different unit's.
PHP
2
star
21

php-metrics-monitor

You have multiple project metrics and want to see a trend in a single diagram without compare multiple ones? You are right.
PHP
2
star
22

docker-php-test-compilation

Run any checks in various PHP versions.
Shell
1
star
23

ansible-vagrant-boxes

Shell
1
star
24

tommy-muehle

1
star
25

github-counter

PHP
1
star
26

tommy-muehle.io

My personal website.
CSS
1
star
27

smtp-reader

Go
1
star
28

php-gpg-verifier

Simple library to verify a file with gpg signature.
PHP
1
star
29

garvis

Go
1
star
30

phive-composer-plugin-devenv

Development environment for PHIVE's Composer plugin.
1
star
31

keynote-enums-an-introduction

Code snippets for my keynote "Enums - An introduction".
PHP
1
star
32

keynote-why-setters-are-bad

Code snippets for my keynote "Why setters are bad".
PHP
1
star
33

dockerized-php-generator

Go
1
star
34

gearman-monitor-bundle

PHP
1
star
35

tommy-muehle.github.io

My personal blog on Github.io
CSS
1
star
36

homebrew-tap

Ruby
1
star
37

jetbrains-color-schemes

My personal color schemes.
1
star
38

dotfiles

My .dotfiles
Shell
1
star