gometalinter

command module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2017 License: MIT Imports: 22 Imported by: 0

README

Go Meta Linter

Build Status Gitter chat

The number of tools for statically checking Go source for errors and warnings is impressive.

This is a tool that concurrently runs a whole bunch of those linters and normalises their output to a standard format:

<file>:<line>:[<column>]: <message> (<linter>)

eg.

stutter.go:9::warning: unused global variable unusedGlobal (varcheck)
stutter.go:12:6:warning: exported type MyStruct should have comment or be unexported (golint)

It is intended for use with editor/IDE integration.

Editor integration

Supported linters

  • go vet - Reports potential errors that otherwise compile.
  • go vet --shadow - Reports variables that may have been unintentionally shadowed.
  • gotype - Syntactic and semantic analysis similar to the Go compiler.
  • deadcode - Finds unused code.
  • gocyclo - Computes the cyclomatic complexity of functions.
  • golint - Google's (mostly stylistic) linter.
  • varcheck - Find unused global variables and constants.
  • structcheck - Find unused struct fields.
  • aligncheck - Warn about un-optimally aligned structures.
  • errcheck - Check that error return values are used.
  • dupl - Reports potentially duplicated code.
  • ineffassign - Detect when assignments to existing variables are not used.
  • interfacer - Suggest narrower interfaces that can be used.
  • unconvert - Detect redundant type conversions.
  • goconst - Finds repeated strings that could be replaced by a constant.
  • gosimple - Report simplifications in code.
  • staticcheck - Statically detect bugs, both obvious and subtle ones.
  • gas - Inspects source code for security problems by scanning the Go AST.

Disabled by default (enable with --enable=<linter>):

  • testify - Show location of failed testify assertions.
  • test - Show location of test failures from the stdlib testing module.
  • gofmt -s - Checks if the code is properly formatted and could not be further simplified.
  • goimports - Checks missing or unreferenced package imports.
  • lll - Report long lines (see --line-length=N).
  • misspell - Finds commonly misspelled English words.
  • unparam - Find unused function parameters.
  • unused - Find unused variables.
  • safesql - Finds potential SQL injection vulnerabilities.

Additional linters can be added through the command line with --linter=NAME:COMMAND:PATTERN (see below).

Configuration file

gometalinter now supports a JSON configuration file which can be loaded via --config=<file>. The format of this file is determined by the Config struct in config.go.

The configuration file mostly corresponds to command-line flags, with the following exceptions:

  • Linters defined in the configuration file will overlay existing definitions, not replace them.
  • "Enable" defines the exact set of linters that will be enabled.

Here is an example configuration file:

{
  "DisableAll": true,
  "Enable": ["deadcode", "unconvert"]
}

Installing

There are two options for installing gometalinter.

  1. Install a stable version, eg. go get -u gopkg.in/alecthomas/gometalinter.v1. I will generally only tag a new stable version when it has passed the Travis regression tests. The downside is that the binary will be called gometalinter.v1.
  2. Install from HEAD with: go get -u github.com/alecthomas/gometalinter. This has the downside that changes to gometalinter may break.

Comment directives

gometalinter supports suppression of linter messages via comment directives. The form of the directive is:

// nolint[: <linter>[, <linter>, ...]]

Suppression works in the following way:

  1. Line-level suppression

    A comment directive suppresses any linter messages on that line.

    eg. In this example any messages for a := 10 will be suppressed and errcheck messages for defer r.Close() will also be suppressed.

    a := 10 // nolint
    a = 2
    defer r.Close() // nolint: errcheck
    
  2. Statement-level suppression

    A comment directive at the same indentation level as a statement it immediately precedes will also suppress any linter messages in that entire statement.

    eg. In this example all messages for SomeFunc() will be suppressed.

    // nolint
    func SomeFunc() {
    }
    

Implementation details: gometalinter now performs parsing of Go source code, to extract linter directives and associate them with line ranges. To avoid unnecessary processing, parsing is on-demand: the first time a linter emits a message for a file, that file is parsed for directives.

Quickstart

Install gometalinter (see above).

Install all known linters:

$ gometalinter --install
Installing:
  structcheck
  aligncheck
  deadcode
  gocyclo
  ineffassign
  dupl
  golint
  gotype
  goimports
  errcheck
  varcheck
  interfacer
  goconst
  gosimple
  staticcheck
  unparam
  unused
  misspell
  lll
  gas
  safesql

Run it:

$ cd example
$ gometalinter ./...
stutter.go:13::warning: unused struct field MyStruct.Unused (structcheck)
stutter.go:9::warning: unused global variable unusedGlobal (varcheck)
stutter.go:12:6:warning: exported type MyStruct should have comment or be unexported (golint)
stutter.go:16:6:warning: exported type PublicUndocumented should have comment or be unexported (golint)
stutter.go:8:1:warning: unusedGlobal is unused (deadcode)
stutter.go:12:1:warning: MyStruct is unused (deadcode)
stutter.go:16:1:warning: PublicUndocumented is unused (deadcode)
stutter.go:20:1:warning: duplicateDefer is unused (deadcode)
stutter.go:21:15:warning: error return value not checked (defer a.Close()) (errcheck)
stutter.go:22:15:warning: error return value not checked (defer a.Close()) (errcheck)
stutter.go:27:6:warning: error return value not checked (doit()           // test for errcheck) (errcheck)
stutter.go:29::error: unreachable code (vet)
stutter.go:26::error: missing argument for Printf("%d"): format reads arg 1, have only 0 args (vet)

Gometalinter also supports the commonly seen <path>/... recursive path format. Note that this can be very slow, and you may need to increase the linter --deadline to allow linters to complete.

FAQ

Exit status

gometalinter sets two bits of the exit status to indicate different issues:

Bit Meaning
0 A linter generated an issue.
1 An underlying error occurred; eg. a linter failed to execute. In this situation a warning will also be displayed.

eg. linter only = 1, underlying only = 2, linter + underlying = 3

What's the best way to use gometalinter in CI?

There are two main problems running in a CI:

  1. Linters break, causing gometalinter --install --update to error (this is no longer an issue as all linters are vendored).
  2. gometalinter adds a new linter.

I have solved 1 by vendoring the linters.

For 2, the best option is to disable all linters, then explicitly enable the ones you want:

gometalinter --disable-all --enable=errcheck --enable=vet --enable=vetshadow ...
How do I make gometalinter work with Go 1.5 vendoring?

gometalinter has a --vendor flag that just sets GO15VENDOREXPERIMENT=1, however the underlying tools must support it. Ensure that all of the linters are up to date and built with Go 1.5 (gometalinter --install --force) then run gometalinter --vendor .. That should be it.

Why does gometalinter --install install a fork of gocyclo?

I forked gocyclo because the upstream behaviour is to recursively check all subdirectories even when just a single directory is specified. This made it unusably slow when vendoring. The recursive behaviour can be achieved with gometalinter by explicitly specifying <path>/.... There is a pull request open.

Gometalinter is not working

That's more of a statement than a question, but okay.

Sometimes gometalinter will not report issues that you think it should. There are three things to try in that case:

1. Update to the latest build of gometalinter and all linters
go get -u github.com/alecthomas/gometalinter
gometalinter --install

If you're lucky, this will fix the problem.

2. Analyse the debug output

If that doesn't help, the problem may be elsewhere (in no particular order):

  1. Upstream linter has changed its output or semantics.
  2. gometalinter is not invoking the tool correctly.
  3. gometalinter regular expression matches are not correct for a linter.
  4. Linter is exceeding the deadline.

To find out what's going on run in debug mode:

gometalinter --debug

This will show all output from the linters and should indicate why it is failing.

3. Report an issue.

Failing all else, if the problem looks like a bug please file an issue and include the output of gometalinter --debug.

Details

Additional linters can be configured via the command line:

$ gometalinter --linter='vet:go tool vet -printfuncs=Infof,Debugf,Warningf,Errorf {path}:PATH:LINE:MESSAGE' .
stutter.go:21:15:warning: error return value not checked (defer a.Close()) (errcheck)
stutter.go:22:15:warning: error return value not checked (defer a.Close()) (errcheck)
stutter.go:27:6:warning: error return value not checked (doit()           // test for errcheck) (errcheck)
stutter.go:9::warning: unused global variable unusedGlobal (varcheck)
stutter.go:13::warning: unused struct field MyStruct.Unused (structcheck)
stutter.go:12:6:warning: exported type MyStruct should have comment or be unexported (golint)
stutter.go:16:6:warning: exported type PublicUndocumented should have comment or be unexported (deadcode)

Checkstyle XML format

gometalinter supports checkstyle compatible XML output format. It is tiggered with --checkstyle flag:

gometalinter --checkstyle

Checkstyle format can be used to integrate gometalinter with Jenkins CI with the help of Checkstyle Plugin.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
_linters
src/github.com/GoASTScanner/gas/core
Package core holds the central scanning logic used by GAS
Package core holds the central scanning logic used by GAS
src/github.com/alecthomas/gocyclo
Gocyclo calculates the cyclomatic complexities of functions and methods in Go source code.
Gocyclo calculates the cyclomatic complexities of functions and methods in Go source code.
src/github.com/golang/lint
Package lint contains a linter for Go source code.
Package lint contains a linter for Go source code.
src/github.com/golang/lint/golint
golint lints the Go source files named on its command line.
golint lints the Go source files named on its command line.
src/github.com/jgautheron/goconst
Package goconst finds repeated strings that could be replaced by a constant.
Package goconst finds repeated strings that could be replaced by a constant.
src/github.com/kisielk/errcheck/internal/errcheck
Package errcheck is the library used to implement the errcheck command-line tool.
Package errcheck is the library used to implement the errcheck command-line tool.
src/github.com/kisielk/gotool
Package gotool contains utility functions used to implement the standard "cmd/go" tool, provided as a convenience to developers who want to write tools with similar semantics.
Package gotool contains utility functions used to implement the standard "cmd/go" tool, provided as a convenience to developers who want to write tools with similar semantics.
src/github.com/mdempsky/unconvert
Unconvert removes redundant type conversions from Go packages.
Unconvert removes redundant type conversions from Go packages.
src/github.com/stripe/safesql
Command safesql is a tool for performing static analysis on programs to ensure that SQL injection attacks are not possible.
Command safesql is a tool for performing static analysis on programs to ensure that SQL injection attacks are not possible.
src/github.com/walle/lll
Package lll provides validation functions regarding line length
Package lll provides validation functions regarding line length
src/golang.org/x/text/internal/gen
Package gen contains common code for the various code generation tools in the text repository.
Package gen contains common code for the various code generation tools in the text repository.
src/golang.org/x/text/internal/triegen
Package triegen implements a code generator for a trie for associating unsigned integer values with UTF-8 encoded runes.
Package triegen implements a code generator for a trie for associating unsigned integer values with UTF-8 encoded runes.
src/golang.org/x/text/internal/ucd
Package ucd provides a parser for Unicode Character Database files, the format of which is defined in http://www.unicode.org/reports/tr44/.
Package ucd provides a parser for Unicode Character Database files, the format of which is defined in http://www.unicode.org/reports/tr44/.
src/golang.org/x/text/transform
Package transform provides reader and writer wrappers that transform the bytes passing through as well as various transformations.
Package transform provides reader and writer wrappers that transform the bytes passing through as well as various transformations.
src/golang.org/x/text/unicode/cldr
Package cldr provides a parser for LDML and related XML formats.
Package cldr provides a parser for LDML and related XML formats.
src/golang.org/x/text/width
Package width provides functionality for handling different widths in text.
Package width provides functionality for handling different widths in text.
src/golang.org/x/tools/cmd/goimports
Command goimports updates your Go import lines, adding missing ones and removing unreferenced ones.
Command goimports updates your Go import lines, adding missing ones and removing unreferenced ones.
src/golang.org/x/tools/cmd/gotype
The gotype command does syntactic and semantic analysis of Go files and packages like the front-end of a Go compiler.
The gotype command does syntactic and semantic analysis of Go files and packages like the front-end of a Go compiler.
src/golang.org/x/tools/container/intsets
Package intsets provides Sparse, a compact and fast representation for sparse sets of int values.
Package intsets provides Sparse, a compact and fast representation for sparse sets of int values.
src/golang.org/x/tools/go/ast/astutil
Package astutil contains common utilities for working with the Go AST.
Package astutil contains common utilities for working with the Go AST.
src/golang.org/x/tools/go/buildutil
Package buildutil provides utilities related to the go/build package in the standard library.
Package buildutil provides utilities related to the go/build package in the standard library.
src/golang.org/x/tools/go/callgraph
Package callgraph defines the call graph and various algorithms and utilities to operate on it.
Package callgraph defines the call graph and various algorithms and utilities to operate on it.
src/golang.org/x/tools/go/callgraph/cha
Package cha computes the call graph of a Go program using the Class Hierarchy Analysis (CHA) algorithm.
Package cha computes the call graph of a Go program using the Class Hierarchy Analysis (CHA) algorithm.
src/golang.org/x/tools/go/callgraph/rta
This package provides Rapid Type Analysis (RTA) for Go, a fast algorithm for call graph construction and discovery of reachable code (and hence dead code) and runtime types.
This package provides Rapid Type Analysis (RTA) for Go, a fast algorithm for call graph construction and discovery of reachable code (and hence dead code) and runtime types.
src/golang.org/x/tools/go/callgraph/static
Package static computes the call graph of a Go program containing only static call edges.
Package static computes the call graph of a Go program containing only static call edges.
src/golang.org/x/tools/go/gcexportdata
Package gcexportdata provides functions for locating, reading, and writing export data files containing type information produced by the gc compiler.
Package gcexportdata provides functions for locating, reading, and writing export data files containing type information produced by the gc compiler.
src/golang.org/x/tools/go/gcimporter15
Package gcimporter15 provides various functions for reading gc-generated object files that can be used to implement the Importer interface defined by the Go 1.5 standard library package.
Package gcimporter15 provides various functions for reading gc-generated object files that can be used to implement the Importer interface defined by the Go 1.5 standard library package.
src/golang.org/x/tools/go/loader
Package loader loads a complete Go program from source code, parsing and type-checking the initial packages plus their transitive closure of dependencies.
Package loader loads a complete Go program from source code, parsing and type-checking the initial packages plus their transitive closure of dependencies.
src/golang.org/x/tools/go/pointer
Package pointer implements Andersen's analysis, an inclusion-based pointer analysis algorithm first described in (Andersen, 1994).
Package pointer implements Andersen's analysis, an inclusion-based pointer analysis algorithm first described in (Andersen, 1994).
src/golang.org/x/tools/go/ssa
Package ssa defines a representation of the elements of Go programs (packages, types, functions, variables and constants) using a static single-assignment (SSA) form intermediate representation (IR) for the bodies of functions.
Package ssa defines a representation of the elements of Go programs (packages, types, functions, variables and constants) using a static single-assignment (SSA) form intermediate representation (IR) for the bodies of functions.
src/golang.org/x/tools/go/ssa/interp
Package ssa/interp defines an interpreter for the SSA representation of Go programs.
Package ssa/interp defines an interpreter for the SSA representation of Go programs.
src/golang.org/x/tools/go/types/typeutil
Package typeutil defines various utilities for types, such as Map, a mapping from types.Type to interface{} values.
Package typeutil defines various utilities for types, such as Map, a mapping from types.Type to interface{} values.
src/golang.org/x/tools/imports
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
src/honnef.co/go/tools/callgraph
Package callgraph defines the call graph and various algorithms and utilities to operate on it.
Package callgraph defines the call graph and various algorithms and utilities to operate on it.
src/honnef.co/go/tools/callgraph/cha
Package cha computes the call graph of a Go program using the Class Hierarchy Analysis (CHA) algorithm.
Package cha computes the call graph of a Go program using the Class Hierarchy Analysis (CHA) algorithm.
src/honnef.co/go/tools/callgraph/rta
This package provides Rapid Type Analysis (RTA) for Go, a fast algorithm for call graph construction and discovery of reachable code (and hence dead code) and runtime types.
This package provides Rapid Type Analysis (RTA) for Go, a fast algorithm for call graph construction and discovery of reachable code (and hence dead code) and runtime types.
src/honnef.co/go/tools/callgraph/static
Package static computes the call graph of a Go program containing only static call edges.
Package static computes the call graph of a Go program containing only static call edges.
src/honnef.co/go/tools/cmd/gosimple
gosimple detects code that could be rewritten in a simpler way.
gosimple detects code that could be rewritten in a simpler way.
src/honnef.co/go/tools/cmd/staticcheck
staticcheck detects a myriad of bugs and inefficiencies in your code.
staticcheck detects a myriad of bugs and inefficiencies in your code.
src/honnef.co/go/tools/cmd/unused
unused reports unused identifiers (types, functions, ...) in your code.
unused reports unused identifiers (types, functions, ...) in your code.
src/honnef.co/go/tools/gcsizes
Package gcsizes provides a types.Sizes implementation that adheres to the rules used by the gc compiler.
Package gcsizes provides a types.Sizes implementation that adheres to the rules used by the gc compiler.
src/honnef.co/go/tools/lint
Package lint provides the foundation for tools like gosimple.
Package lint provides the foundation for tools like gosimple.
src/honnef.co/go/tools/lint/lintutil
Package lintutil provides helpers for writing linter command lines.
Package lintutil provides helpers for writing linter command lines.
src/honnef.co/go/tools/simple
Package simple contains a linter for Go source code.
Package simple contains a linter for Go source code.
src/honnef.co/go/tools/ssa
Package ssa defines a representation of the elements of Go programs (packages, types, functions, variables and constants) using a static single-assignment (SSA) form intermediate representation (IR) for the bodies of functions.
Package ssa defines a representation of the elements of Go programs (packages, types, functions, variables and constants) using a static single-assignment (SSA) form intermediate representation (IR) for the bodies of functions.
src/honnef.co/go/tools/ssa/interp
Package ssa/interp defines an interpreter for the SSA representation of Go programs.
Package ssa/interp defines an interpreter for the SSA representation of Go programs.
src/honnef.co/go/tools/staticcheck
Package staticcheck contains a linter for Go source code.
Package staticcheck contains a linter for Go source code.

Jump to

Keyboard shortcuts

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