MCPcopy Index your code
hub / github.com/mgechev/revive

github.com/mgechev/revive @v1.15.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.15.0 ↗
2,217 symbols 4,593 edges 572 files 843 documented · 38% 20 cross-repo links
README

revive

Build Status Go Reference Ask DeepWiki

Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. Revive provides a framework for development of custom rules, and lets you define a strict preset for enhancing your development & code review processes.

Logo by Georgi Serev

Here's how revive is different from golint:

  • Allows to enable or disable rules using a configuration file.
  • Allows to configure the linting rules with a TOML file.
  • 2x faster running the same rules as golint.
  • Provides functionality for disabling a specific rule or the entire linter for a file or a range of lines.
  • golint allows this only for generated files.
  • Optional type checking. Most rules in golint do not require type checking. If you disable them in the config file, revive will run over 6x faster than golint.
  • Provides multiple formatters which let us customize the output.
  • Allows to customize the return code for the entire linter or based on the failure of only some rules.
  • Everyone can extend it easily with custom rules or formatters.
  • Revive provides more rules compared to golint.

Installation

revive is available inside the majority of package managers.

Repology packaging status

Homebrew

Install revive using brew:

brew install revive

To upgrade to the latest version:

brew upgrade revive

Install from Sources

Install the latest stable release directly from source:

go install github.com/mgechev/revive@latest

To install the latest commit from the main branch:

go install github.com/mgechev/revive@HEAD

Docker

You can run revive using Docker to avoid installing it directly on your system:

docker run -v "$(pwd)":/var/YOUR_REPOSITORY ghcr.io/mgechev/revive:v1.10.0 -config /var/YOUR_REPOSITORY/revive.toml -formatter stylish ./var/YOUR_REPOSITORY/...

Note: Replace YOUR_REPOSITORY with the path to your repository.

A volume must be mounted to share the current repository with the container. For more details, refer to the bind mounts Docker documentation.

  • -v: Mounts the current directory ($(pwd)) to /var/YOUR_REPOSITORY inside the container.
  • ghcr.io/mgechev/revive:v1.10.0: Specifies the Docker image and its version.
  • revive: The command to run inside the container.
  • Flags like -config and -formatter are the same as when using the binary directly.

Manual Binary Download

Download the precompiled binary from the Releases page:

  1. Select the appropriate binary for your OS and architecture.
  2. Extract the binary and move it to a directory in your PATH (e.g., /usr/local/bin).
  3. Verify installation:
revive -version

Usage

Since the default behavior of revive is compatible with golint, without providing any additional flags, the only difference you'd notice is faster execution.

revive supports a -config flag whose value should correspond to a TOML file describing which rules to use for revive's linting. If not provided, revive will try to use a global config file (assumed to be located at $HOME/revive.toml). Otherwise, if no configuration TOML file is found then revive uses a built-in set of default linting rules.

Text Editors

  • Support for VSCode via vscode-go by changing the go.lintTool setting to revive:
{
  "go.lintTool": "revive",
}

vim let g:ale_linters = { \ 'go': ['revive'], \}

GitHub Actions

Continuous Integration

Codeac.io - Automated code review service integrates with GitHub, Bitbucket and GitLab (even self-hosted) and helps you fight technical debt. Check your pull-requests with revive automatically. (Free for open-source projects)

Linter aggregators

golangci-lint

To enable revive in golangci-lint you need to add revive to the list of enabled linters:

# golangci-lint configuration file
version: "2"
linters:
   enable:
     - revive

Then revive can be configured by adding an entry to the linters.settings section of the configuration, for example:

# golangci-lint configuration file
linters:
  settings:
    revive:
      severity: warning
      rules:
        - name: atomic
        - name: line-length-limit
          severity: error
          arguments: [80]
        - name: unhandled-error
          arguments: ["fmt.Printf", "myFunction"]

The above configuration enables three rules of revive: atomic, line-length-limit and unhandled-error and passes some arguments to the last two. The Configuration section of this document provides details on how to configure revive. Note that while revive configuration is in TOML, that of golangci-lint is in YAML or JSON. See the golangci-lint website for more information about configuring revive.

Please notice that if no particular configuration is provided, revive will behave as golint does, i.e. all golint rules are enabled (the Available Rules table details what are the golint rules). When a configuration is provided, only rules in the configuration are enabled.

Command Line Flags

revive accepts the following command line parameters:

  • -config [PATH] - path to the config file in TOML format, defaults to $HOME/revive.toml if present.
  • -exclude [PATTERN] - pattern for files/directories/packages to be excluded for linting. You can specify the files you want to exclude for linting either as package name (i.e. github.com/mgechev/revive), list them as individual files (i.e. file.go), directories (i.e. ./foo/...), or any combination of the three. If no exclusion patterns are specified, vendor/... will be excluded by default.
  • -formatter [NAME] - formatter to be used for the output. The currently available formatters are:

  • default - will output the failures the same way that golint does.

  • json - outputs the failures in JSON format.
  • ndjson - outputs the failures as a stream in newline delimited JSON (NDJSON) format.
  • friendly - outputs the failures when found. Shows the summary of all the failures.
  • stylish - formats the failures in a table. Keep in mind that it doesn't stream the output so it might be perceived as slower compared to others.
  • checkstyle - outputs the failures in XML format compatible with that of Java's Checkstyle.
  • -max_open_files - maximum number of open files at the same time. Defaults to unlimited.
  • -set_exit_status - set exit status to 1 if any issues are found, overwrites errorCode and warningCode in config.
  • -version - get revive version.

Sample Invocations

revive -config revive.toml -exclude file1.go -exclude file2.go -formatter friendly github.com/mgechev/revive package/...
  • The command above will use the configuration from revive.toml
  • revive will ignore file1.go and file2.go
  • The output will be formatted with the friendly formatter
  • The linter will analyze github.com/mgechev/revive and the files in package

Comment Directives

Using comments, you can disable the linter for the entire file or only a range of lines:

//revive:disable

func Public() {}

//revive:enable

The snippet above, will disable revive between the revive:disable and revive:enable comments. If you skip revive:enable, the linter will be disabled for the rest of the file.

With revive:disable-next-line and revive:disable-line you can disable revive on a particular code line.

You can do the same on a rule level. In case you want to disable only a particular rule, you can use:

//revive:disable:unexported-return
func Public() private {
    return private
}

//revive:enable:unexported-return

This way, revive will not warn you that you're returning an object of an unexported type, from an exported function.

You can document why you disable the linter by adding a trailing text in the directive, for example

//revive:disable Until the code is stable
//revive:disable:cyclomatic High complexity score but easy to understand

You can also configure revive to enforce documenting linter disabling directives by adding

[directive.specify-disable-reason]

in the configuration. You can set the severity (defaults to warning) of the violation of this directive

[directive.specify-disable-reason]
severity = "error"

Configuration

revive can be configured with a TOML file. Here's a sample configuration with an explanation of the individual properties:

# When set to false, ignores files with "GENERATED" header, similar to golint
ignoreGeneratedHeader = true

# Sets the default severity to "warning"
severity = "warning"

# Sets the default failure confidence. This means that linting errors
# with less than 0.8 confidence will be ignored.
confidence = 0.8

# Sets the error code for failures with the "error" severity
errorCode = 0

# Sets the error code for failures with severity "warning"
warningCode = 0

# Configuration of the `cyclomatic` rule. Here we specify that
# the rule should fail if it detects code with higher complexity than 10.
[rule.cyclomatic]
arguments = [10]

# Sets the severity of the `package-comments` rule to "error".
[rule.package-comments]
severity = "error"

By default revive will enable only the linting rules that are named in the configuration file. For example, the previous configuration file makes revive to enable only cyclomatic and package-comments linting rules.

To enable default rules you need to use:

enableDefaultRules = true

This will enable all rules available in golint and use their default configuration (i.e. the way they are hardcoded in golint). The default configuration of revive can be found at defaults.toml.

To enable all available rules you need to add:

enableAllRules = true

This will enable all available rules no matter what rules are named in the configuration file.

Options enableAllRules and enableDefaultRules cannot be combined.

To disable a rule, you simply mark it as disabled in the configuration. For example:

[rule.line-length-limit]
Disabled = true

When enabling all rules you still need/can provide specific configurations for rules. The following file is an example configuration where all rules are enabled, except for those that are explicitly disabled, and some rules are configured with particular arguments:

```toml severity = "warning" confidence = 0.8 errorCode = 0 warningCode = 0

Enable all available rules

enableAllRules = true

Disabled rules

[rule.blank-imports] Disabled = true [rule.file-header] Disabled = true [rule.max-public-structs] Disabled = true [rule.line-length-limit] Disabled = true [rule.function-length] Disabled = true [rule.banned-characters] Disabled = true

Rule tuning

[rule.argument

Extension points exported contracts — how you extend this code

Rule (Interface)
Rule defines an abstract rule interface. [104 implementers]
lint/rule.go
Formatter (Interface)
Formatter defines an interface for failure formatters. [9 implementers]
lint/formatter.go
Interface (Interface)
Interface is exported. [1 implementers]
testdata/golint/unexported_return.go
Some (Interface)
by default code below is valid, but if checkPublicInterface is switched on - it should check documentation in interfaces
testdata/exported_issue_1002.go
ReadFile (FuncType)
ReadFile defines an abstraction for reading files.
lint/linter.go
CheckFunc (FuncType)
CheckFunc evaluates a rule against the given if-else chain and returns a message describing the proposed refactor, along
internal/ifelse/rule.go
ConfigurableRule (Interface)
ConfigurableRule defines an abstract configurable rule interface. [39 implementers]
lint/rule.go
Server (Interface)
Server is a test type.
testdata/golint/var_declaration.go

Core symbols most depended-on inside this repo

println
called by 540
testdata/use_fmt_print_with_redefinition.go
append
called by 180
testdata/redefines_builtin_id.go
print
called by 90
testdata/use_fmt_print_with_redefinition.go
Error
called by 69
testdata/golint/exported.go
isRuleOption
called by 47
rule/utils.go
String
called by 37
revivelib/core.go
GoFmt
called by 35
internal/astutils/ast_utils.go
Name
called by 28
lint/rule.go

Shape

Function 1,030
Method 740
Struct 366
TypeAlias 62
Interface 13
FuncType 6

Languages

Go100%

Modules by API surface

rule/struct_tag.go42 symbols
testdata/unconditional_recursion.go27 symbols
testdata/confusing_naming1.go24 symbols
testdata/cognitive_complexity.go24 symbols
rule/exported.go24 symbols
testdata/empty_lines.go22 symbols
testdata/struct_tag.go21 symbols
testdata/unused_param.go20 symbols
testdata/golint/exported.go20 symbols
testdata/golint/unexported_return.go19 symbols
testdata/golint/sort.go19 symbols
testdata/early_return_jump.go19 symbols

Dependencies from manifests, versioned

4d63.com/gocheckcompilerdirectivesv1.3.0 · 1×
4d63.com/gochecknoglobalsv0.2.2 · 1×
codeberg.org/chavacava/garifv0.2.0 · 1×
codeberg.org/polyfloyd/go-errorlintv1.9.0 · 1×
dev.gaijin.team/go/exhaustruct/v4v4.0.0 · 1×
dev.gaijin.team/go/golibv0.7.0 · 1×
github.com/4meepo/tagalignv1.4.3 · 1×
github.com/Abirdcfly/dupwordv0.1.7 · 1×
github.com/AdminBenni/iota-mixingv1.0.0 · 1×
github.com/AlwxSin/noinlineerrv1.0.5 · 1×
github.com/Antonboom/errnamev1.1.1 · 1×
github.com/Antonboom/nilnilv1.1.1 · 1×

For agents

$ claude mcp add revive \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact