errcheck

repository·master·Indexed 25 days ago

https://github.com/kisielk/errcheck

A static analysis tool for Go that identifies silently ignored errors. It ensures that functions returning errors have those errors either assigned to a variable or explicitly discarded using the blank identifier. It supports custom exclude files, build tags, and integration with the golang.org/x/tools/go/analysis API.

Tokens
1.1K
Snippets
3
Records
8
Agent score
32%

What's inside errcheck

  1. Use errcheck to find ignored errors

    master

    Run errcheck by providing a package path. It identifies callables where returned errors are neither assigned to a variable nor explicitly discarded using the blank identifier _.

    Common usage patterns:

    • Check a specific package: errcheck <package_path>
    • Check all packages in the current directory: errcheck ./...
    • Check all packages in your $GOPATH and $GOROOT: errcheck all
    errcheck ./...
  2. Exclude functions from error checking

    master

    To prevent errcheck from complaining about specific functions, use the -exclude flag with a text file containing function signatures.

    Signature Formats

    • Standard function: package.FunctionName (e.g., os.ReadFile)
    • Value receiver method: (package.Receiver).MethodName (e.g., (bytes.Buffer).Write)
    • Pointer receiver method: (*package.Receiver).MethodName (e.g., (*bytes.Buffer).Write)
    • Type-specific exclusion: package.FunctionName(TYPE) excludes the call only if the first argument is of TYPE.
    • Literal argument exclusion: package.FunctionName(os.Stdout) or package.FunctionName(os.Stderr) excludes the call only when the first argument is that specific literal.

    Vendored Dependencies

    When using vendored dependencies, use the full import path including the vendor directory: example.com/yourpkg/vendor/example.net/fmt2.Println

    Example Exclude File

    io.Copy(*bytes.Buffer)
    io.Copy(os.Stdout)
    os.ReadFile
    
    // Sometimes we don't care if a HTTP request fails.
    (*net/http.Client).Do
    errcheck -exclude errcheck_excludes.txt path/to/package
  3. Use the errcheck CLI

    master

    errcheck is a command-line tool used to find unchecked errors in Go code. It scans specified packages and reports locations where an error is returned but not handled.

    By default, it checks the current directory (.) if no paths are provided. It can be configured to ignore specific packages, files (like tests or generated code), or specific function names via an exclude file.

    Exit Codes

    • 0: No unchecked errors found.
    • 1: Unchecked errors were found.
    • 2: A fatal error occurred (e.g., failed to load packages or invalid flags).
  4. Integrate errcheck with go/analysis

    master

    The errcheck package provides an Analyzer instance compatible with the golang.org/x/tools/go/analysis API. This allows you to use errcheck as part of a larger static analysis pipeline.

    Supported flags for the Analyzer:

    • blank
    • assert
    • exclude
    • excludeonly

    Note: The analyzer is experimental and subject to change.

  5. Understand errcheck exit codes

    master

    When running errcheck in CI or scripts, use the following exit codes to determine the result:

    • 1: Problems (unchecked errors) were found in the checked files.
    • 2: Other failures occurred (e.g., tool errors).
  6. Reference: errcheck CLI flags

    master

    The following command-line options are available for errcheck:

    FlagDescription
    -tags <list>A space-separated list of build tags (similar to go build).
    -assertsEnables checking for ignored type assertion results.
    -blankEnables checking for assignments of errors to the blank identifier (_).
    -abspathPrints the absolute paths to files with unchecked errors.
    -mod <mode>Sets the module download mode: readonly or vendor.
    -exclude <file>Specifies a path to a file containing a list of functions to be excluded.
    -excludeonlyDisables the built-in exclude list (standard library functions that never return errors).
    -ignore <regex>Deprecated: Takes a comma-separated list of package:regex pairs to ignore functions.
    -ignorepkg <list>Deprecated: Takes a comma-separated list of package import paths to ignore.
    -ignoretestsDisables checking of _test.go files.
    -ignoregeneratedDisables checking of generated source code.