Skip to content

Commit 5b85345

Browse files
authored
Synchronize lint rules across repositories (#60)
* Disable low value linters. See related discussion in elastic/beats#31683 * Fix new lint errors.
1 parent 718cb6f commit 5b85345

File tree

10 files changed

+42
-108
lines changed

10 files changed

+42
-108
lines changed

.golangci.yml

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,21 @@ issues:
1919
output:
2020
sort-results: true
2121

22-
# Uncomment and add a path if needed to exclude
23-
# skip-dirs:
24-
# - some/path
25-
# skip-files:
26-
# - ".*\\.my\\.go$"
27-
# - lib/bad.go
28-
2922
# Find the whole list here https://golangci-lint.run/usage/linters/
3023
linters:
3124
disable-all: true
3225
enable:
33-
- deadcode # finds unused code
3426
- errcheck # checking for unchecked errors in go programs
3527
- 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.
36-
- goconst # finds repeated strings that could be replaced by a constant
37-
- dupl # tool for code clone detection
3828
- forbidigo # forbids identifiers matched by reg exps
3929
- gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
4030
- gosimple # linter for Go source code that specializes in simplifying a code
4131
- misspell # finds commonly misspelled English words in comments
4232
- nakedret # finds naked returns in functions greater than a specified function length
43-
- prealloc # finds slice declarations that could potentially be preallocated
4433
- nolintlint # reports ill-formed or insufficient nolint directives
4534
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
4635
- stylecheck # a replacement for golint
47-
- unparam # reports unused function parameters
4836
- unused # checks Go code for unused constants, variables, functions and types
49-
5037
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
5138
- ineffassign # detects when assignments to existing variables are not used
5239
- structcheck # finds unused struct fields
@@ -63,15 +50,20 @@ linters:
6350
- noctx # noctx finds sending http request without context.Context
6451
- unconvert # Remove unnecessary type conversions
6552
- wastedassign # wastedassign finds wasted assignment statements.
66-
- godox # tool for detection of FIXME, TODO and other comment keywords
6753
- gomodguard # check for blocked dependencies
6854

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

7668
errorlint:
7769
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
@@ -81,16 +73,6 @@ linters-settings:
8173
# Check for plain error comparisons
8274
comparison: true
8375

84-
goconst:
85-
# minimal length of string constant, 3 by default
86-
min-len: 3
87-
# minimal occurrences count to trigger, 3 by default
88-
min-occurrences: 2
89-
90-
dupl:
91-
# tokens count to trigger issue, 150 by default
92-
threshold: 100
93-
9476
forbidigo:
9577
# Forbid the following identifiers
9678
forbid:
@@ -106,30 +88,15 @@ linters-settings:
10688
# Select the Go version to target. The default is '1.13'.
10789
go: "1.17.11"
10890

109-
misspell:
110-
# Correct spellings using locale preferences for US or UK.
111-
# Default is to use a neutral variety of English.
112-
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
113-
# locale: US
114-
# ignore-words:
115-
# - IdP
116-
11791
nakedret:
11892
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
11993
max-func-lines: 0
12094

121-
prealloc:
122-
# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
123-
# True by default.
124-
simple: true
125-
range-loops: true # Report preallocation suggestions on range loops, true by default
126-
for-loops: false # Report preallocation suggestions on for loops, false by default
127-
12895
nolintlint:
12996
# Enable to ensure that nolint directives are all used. Default is true.
13097
allow-unused: false
13198
# Disable to ensure that nolint directives don't have a leading space. Default is true.
132-
allow-leading-space: true
99+
allow-leading-space: false
133100
# Exclude following linters from requiring an explanation. Default is [].
134101
allow-no-explanation: []
135102
# Enable to require an explanation of nonzero length after each nolint directive. Default is false.
@@ -147,7 +114,7 @@ linters-settings:
147114
recommendations:
148115
- errors
149116
- fmt
150-
reason: "This package is deprecated"
117+
reason: "This package is deprecated, use fmt.Errorf with %%w instead"
151118
- github.com/elastic/beats/v7:
152119
reason: "There must be no Beats dependency"
153120

@@ -163,13 +130,14 @@ linters-settings:
163130
# https://staticcheck.io/docs/options#checks
164131
checks: ["all"]
165132

166-
unparam:
167-
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
168-
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
169-
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
170-
# with golangci-lint call it on a directory with the changed file.
171-
check-exported: false
172-
173133
unused:
174134
# Select the Go version to target. The default is '1.13'.
175135
go: "1.17.11"
136+
137+
gosec:
138+
excludes:
139+
- G306 # Expect WriteFile permissions to be 0600 or less
140+
- G404 # Use of weak random number generator
141+
- G401 # Detect the usage of DES, RC4, MD5 or SHA1: Used in non-crypto contexts.
142+
- G501 # Import blocklist: crypto/md5: Used in non-crypto contexts.
143+
- G505 # Import blocklist: crypto/sha1: Used in non-crypto contexts.

dev-tools/mage/linter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (Linter) Install() error {
124124
defer installScript.Close()
125125

126126
log.Println("Downloading the linter installation script...")
127-
// nolint: noctx // valid use since there is no context
127+
//nolint:noctx // valid use since there is no context
128128
resp, err := http.Get(linterInstallURL)
129129
if err != nil {
130130
return fmt.Errorf("cannot download the linter installation script from %q: %w", linterInstallURL, err)

dev-tools/templates/.golangci.yml

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,21 @@ issues:
1616
output:
1717
sort-results: true
1818

19-
# Uncomment and add a path if needed to exclude
20-
# skip-dirs:
21-
# - some/path
22-
# skip-files:
23-
# - ".*\\.my\\.go$"
24-
# - lib/bad.go
25-
2619
# Find the whole list here https://golangci-lint.run/usage/linters/
2720
linters:
2821
disable-all: true
2922
enable:
30-
- deadcode # finds unused code
3123
- errcheck # checking for unchecked errors in go programs
3224
- 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.
33-
- goconst # finds repeated strings that could be replaced by a constant
34-
- dupl # tool for code clone detection
3525
- forbidigo # forbids identifiers matched by reg exps
3626
- gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
3727
- gosimple # linter for Go source code that specializes in simplifying a code
3828
- misspell # finds commonly misspelled English words in comments
3929
- nakedret # finds naked returns in functions greater than a specified function length
40-
- prealloc # finds slice declarations that could potentially be preallocated
4130
- nolintlint # reports ill-formed or insufficient nolint directives
4231
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
4332
- stylecheck # a replacement for golint
44-
- unparam # reports unused function parameters
4533
- unused # checks Go code for unused constants, variables, functions and types
46-
4734
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
4835
- ineffassign # detects when assignments to existing variables are not used
4936
- structcheck # finds unused struct fields
@@ -60,15 +47,20 @@ linters:
6047
- noctx # noctx finds sending http request without context.Context
6148
- unconvert # Remove unnecessary type conversions
6249
- wastedassign # wastedassign finds wasted assignment statements.
63-
- godox # tool for detection of FIXME, TODO and other comment keywords
6450
- gomodguard # check for blocked dependencies
6551

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

7365
errorlint:
7466
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
@@ -78,16 +70,6 @@ linters-settings:
7870
# Check for plain error comparisons
7971
comparison: true
8072

81-
goconst:
82-
# minimal length of string constant, 3 by default
83-
min-len: 3
84-
# minimal occurrences count to trigger, 3 by default
85-
min-occurrences: 2
86-
87-
dupl:
88-
# tokens count to trigger issue, 150 by default
89-
threshold: 100
90-
9173
forbidigo:
9274
# Forbid the following identifiers
9375
forbid:
@@ -103,30 +85,15 @@ linters-settings:
10385
# Select the Go version to target. The default is '1.13'.
10486
go: "{{.GoVersion}}"
10587

106-
misspell:
107-
# Correct spellings using locale preferences for US or UK.
108-
# Default is to use a neutral variety of English.
109-
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
110-
# locale: US
111-
# ignore-words:
112-
# - IdP
113-
11488
nakedret:
11589
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
11690
max-func-lines: 0
11791

118-
prealloc:
119-
# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
120-
# True by default.
121-
simple: true
122-
range-loops: true # Report preallocation suggestions on range loops, true by default
123-
for-loops: false # Report preallocation suggestions on for loops, false by default
124-
12592
nolintlint:
12693
# Enable to ensure that nolint directives are all used. Default is true.
12794
allow-unused: false
12895
# Disable to ensure that nolint directives don't have a leading space. Default is true.
129-
allow-leading-space: true
96+
allow-leading-space: false
13097
# Exclude following linters from requiring an explanation. Default is [].
13198
allow-no-explanation: []
13299
# Enable to require an explanation of nonzero length after each nolint directive. Default is false.
@@ -144,7 +111,7 @@ linters-settings:
144111
recommendations:
145112
- errors
146113
- fmt
147-
reason: "This package is deprecated"
114+
reason: "This package is deprecated, use fmt.Errorf with %%w instead"
148115
- github.com/elastic/beats/v7:
149116
reason: "There must be no Beats dependency"
150117

@@ -160,13 +127,14 @@ linters-settings:
160127
# https://staticcheck.io/docs/options#checks
161128
checks: ["all"]
162129

163-
unparam:
164-
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
165-
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
166-
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
167-
# with golangci-lint call it on a directory with the changed file.
168-
check-exported: false
169-
170130
unused:
171131
# Select the Go version to target. The default is '1.13'.
172132
go: "{{.GoVersion}}"
133+
134+
gosec:
135+
excludes:
136+
- G306 # Expect WriteFile permissions to be 0600 or less
137+
- G404 # Use of weak random number generator
138+
- G401 # Detect the usage of DES, RC4, MD5 or SHA1: Used in non-crypto contexts.
139+
- G501 # Import blocklist: crypto/md5: Used in non-crypto contexts.
140+
- G505 # Import blocklist: crypto/sha1: Used in non-crypto contexts.

file/helper_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func SyncParent(path string) error {
4343
parent := filepath.Dir(path)
4444
f, err := os.Open(parent)
4545

46-
// nolint: nilerr // ignore error
46+
//nolint:nilerr // ignore error
4747
if err != nil {
4848
return nil
4949
}

file/helper_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/stretchr/testify/assert"
3030
)
3131

32-
// nolint: gosec // file permissions are valid for test purposes
3332
func TestSafeFileRotateExistingFile(t *testing.T) {
3433
tempdir, err := ioutil.TempDir("", "")
3534
assert.NoError(t, err)

kibana/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func extractMessage(result []byte) error {
110110
}
111111
}
112112
if err := json.Unmarshal(result, &kibanaResult); err != nil {
113-
return nil // nolint: nilerr // we suppress some malformed errors on purpose
113+
return nil //nolint:nilerr // we suppress some malformed errors on purpose
114114
}
115115

116116
if !kibanaResult.Success {

match/matcher_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
// nolint: dupl // test cases are similar
1918
package match
2019

2120
import (

service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"fmt"
2525
"net"
2626
"net/http"
27-
_ "net/http/pprof" // nolint: gosec // we want to expose it
27+
_ "net/http/pprof" //nolint:gosec // we want to expose it
2828
"os"
2929
"os/signal"
3030
"runtime"

transport/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func NewClient(c Config, network, host string, defaultPort int) (*Client, error)
5151
// do some sanity checks regarding network and Config matching +
5252
// address being parseable
5353
switch network {
54-
case "tcp", "tcp4", "tcp6": // nolint: goconst // we do not win anything by making tcp a const
55-
case "udp", "udp4", "udp6": // nolint: goconst // we do not win anything by making udp a const
54+
case "tcp", "tcp4", "tcp6":
55+
case "udp", "udp4", "udp6":
5656
if c.TLS == nil && c.Proxy == nil {
5757
break
5858
}

transport/httpcommon/httpcommon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ func (settings *HTTPTransportSettings) httpRoundTripper(
253253
t := http.DefaultTransport.(*http.Transport).Clone()
254254
t.DialContext = nil
255255
t.DialTLSContext = nil
256-
t.Dial = dialer.Dial // nolint: staticcheck // use deprecated function to preserve functionality
257-
t.DialTLS = tlsDialer.Dial // nolint: staticcheck // use deprecated function to preserve functionality
256+
t.Dial = dialer.Dial //nolint:staticcheck // use deprecated function to preserve functionality
257+
t.DialTLS = tlsDialer.Dial //nolint:staticcheck // use deprecated function to preserve functionality
258258
t.TLSClientConfig = tls.ToConfig()
259259
t.ForceAttemptHTTP2 = false
260260
t.Proxy = settings.Proxy.ProxyFunc()

0 commit comments

Comments
 (0)