Skip to content

Commit

Permalink
Update the linter configuration. (#1478) (#1538)
Browse files Browse the repository at this point in the history
Sync the configuration with the one used in Beats, which has disabled
the majority of the least useful linters already.

(cherry picked from commit e64ea9a)

Co-authored-by: Craig MacKenzie <[email protected]>
  • Loading branch information
mergify[bot] and cmacknz authored Oct 17, 2022
1 parent 43665a0 commit 3fc6526
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 70 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ jobs:
name: lint
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

# Uses Go version from the repository.
- name: Read .go-version file
id: goversion
run: echo "::set-output name=version::$(cat .go-version)"

- uses: actions/setup-go@v2
- uses: actions/setup-go@v3
with:
go-version: "${{ steps.goversion.outputs.version }}"

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.45.2
version: v1.47.2

# Give the job more time to execute.
# Regarding `--whole-files`, the linter is supposed to support linting of changed a patch only but,
Expand Down
110 changes: 44 additions & 66 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,37 @@ issues:
# Set to 0 to disable.
# Default: 50
max-issues-per-linter: 0
exclude-rules:
# Exclude package name contains '-' issue because we have at least one package with
# it on its name.
- text: "ST1003:"
linters:
- stylecheck
# From mage we are priting to the console to ourselves
- path: (.*magefile.go|.*dev-tools/mage/.*)
linters:
- forbidigo

output:
sort-results: true

# Uncomment and add a path if needed to exclude
# skip-dirs:
# - some/path
# skip-files:
# - ".*\\.my\\.go$"
# - lib/bad.go

# Find the whole list here https://golangci-lint.run/usage/linters/
linters:
disable-all: true
enable:
- deadcode # finds unused code
- errcheck # checking for unchecked errors in go programs
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
- goconst # finds repeated strings that could be replaced by a constant
- dupl # tool for code clone detection
- forbidigo # forbids identifiers matched by reg exps
# 'replace' is used in go.mod for many dependencies that come from libbeat. We should work to remove those,
# so we can re-enable this linter.
# - gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
- gomodguard
- gosimple # linter for Go source code that specializes in simplifying a code
- misspell # finds commonly misspelled English words in comments
- nakedret # finds naked returns in functions greater than a specified function length
- prealloc # finds slice declarations that could potentially be preallocated
- nolintlint # reports ill-formed or insufficient nolint directives
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
- stylecheck # a replacement for golint
- unparam # reports unused function parameters
- unused # checks Go code for unused constants, variables, functions and types

- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- ineffassign # detects when assignments to existing variables are not used
- structcheck # finds unused struct fields
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
- varcheck # Finds unused global variables and constants
- asciicheck # simple linter to check that your code does not contain non-ASCII identifiers
- bodyclose # checks whether HTTP response body is closed successfully
- durationcheck # check for two durations multiplied together
Expand All @@ -63,14 +54,20 @@ linters:
- noctx # noctx finds sending http request without context.Context
- unconvert # Remove unnecessary type conversions
- wastedassign # wastedassign finds wasted assignment statements.
# - godox # tool for detection of FIXME, TODO and other comment keywords
- gomodguard # check for blocked dependencies

# all available settings of specific linters
linters-settings:
errcheck:
# report about not checking of errors in type assertions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
check-type-assertions: true
check-type-assertions: false
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`.
check-blank: false
# List of functions to exclude from checking, where each entry is a single function to exclude.
# See https://github.com/kisielk/errcheck#excluding-functions for details.
exclude-functions:
- (mapstr.M).Delete # Only returns ErrKeyNotFound, can safely be ignored.
- (mapstr.M).Put # Can only fail on type conversions, usually safe to ignore.

errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
Expand All @@ -80,85 +77,66 @@ linters-settings:
# Check for plain error comparisons
comparison: true

goconst:
# minimal length of string constant, 3 by default
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 2

dupl:
# tokens count to trigger issue, 150 by default
threshold: 100

forbidigo:
# Forbid the following identifiers
forbid:
- fmt.Print.* # too much log noise
# Exclude godoc examples from forbidigo checks. Default is true.
exclude_godoc_examples: true

gomoddirectives:
# Allow local `replace` directives. Default is false.
replace-local: false
goimports:
local-prefixes: github.com/elastic

gomodguard:
blocked:
# List of blocked modules.
modules:
- github.com/elastic/beats/v7:
reason: "There must be no Beats dependency, use elastic-agent-libs instead."

# Blocked module.
- github.com/pkg/errors:
# Recommended modules that should be used instead. (Optional)
recommendations:
- errors
- fmt
reason: "This package is deprecated, use `fmt.Errorf` with `%w` instead"

gosimple:
# Select the Go version to target. The default is '1.13'.
go: "1.17"

misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
# locale: US
# ignore-words:
# - IdP
go: "1.18.7"

nakedret:
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
max-func-lines: 0

prealloc:
# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
# True by default.
simple: true
range-loops: true # Report preallocation suggestions on range loops, true by default
for-loops: false # Report preallocation suggestions on for loops, false by default

nolintlint:
# Enable to ensure that nolint directives are all used. Default is true.
allow-unused: false
# Disable to ensure that nolint directives don't have a leading space. Default is true.
allow-leading-space: true
allow-leading-space: false
# Exclude following linters from requiring an explanation. Default is [].
allow-no-explanation: []
# Enable to require an explanation of nonzero length after each nolint directive. Default is false.
require-explanation: true
# Enable to require nolint directives to mention the specific linter being suppressed. Default is false.
require-specific: true
require-specific: false

staticcheck:
# Select the Go version to target. The default is '1.13'.
go: "1.17"
go: "1.18.7"
checks: ["all"]

stylecheck:
# Select the Go version to target. The default is '1.13'.
go: "1.17"

unparam:
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false
go: "1.18.7"
checks: ["all"]

unused:
# Select the Go version to target. The default is '1.13'.
go: "1.17"
go: "1.18.7"

gosec:
excludes:
- G306 # Expect WriteFile permissions to be 0600 or less
- G404 # Use of weak random number generator
- G401 # Detect the usage of DES, RC4, MD5 or SHA1: Used in non-crypto contexts.
- G501 # Import blocklist: crypto/md5: Used in non-crypto contexts.
- G505 # Import blocklist: crypto/sha1: Used in non-crypto contexts.

0 comments on commit 3fc6526

Please sign in to comment.