Skip to content

Commit 08e06e0

Browse files
authored
Upgrade golangci-lint, more linters (#65)
1 parent e8536fe commit 08e06e0

16 files changed

+367
-232
lines changed

.golangci.yml

+30-17
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,32 @@ linters-settings:
2525
- ^os.Exit$
2626
- ^panic$
2727
- ^print(ln)?$
28+
varnamelen:
29+
max-distance: 12
30+
min-name-length: 2
31+
ignore-type-assert-ok: true
32+
ignore-map-index-ok: true
33+
ignore-chan-recv-ok: true
34+
ignore-decls:
35+
- i int
36+
- n int
37+
- w io.Writer
38+
- r io.Reader
39+
- b []byte
2840

2941
linters:
3042
enable:
3143
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
3244
- bidichk # Checks for dangerous unicode character sequences
3345
- bodyclose # checks whether HTTP response body is closed successfully
46+
- containedctx # containedctx is a linter that detects struct contained context.Context field
3447
- contextcheck # check the function whether use a non-inherited context
48+
- cyclop # checks function and package cyclomatic complexity
3549
- decorder # check declaration order and count of types, constants, variables and functions
3650
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
3751
- dupl # Tool for code clone detection
3852
- durationcheck # check for two durations multiplied together
53+
- err113 # Golang linter to check the errors handling expressions
3954
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
4055
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted.
4156
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.
@@ -46,66 +61,64 @@ linters:
4661
- forcetypeassert # finds forced type assertions
4762
- gci # Gci control golang package import order and make it always deterministic.
4863
- gochecknoglobals # Checks that no globals are present in Go code
49-
- gochecknoinits # Checks that no init functions are present in Go code
5064
- gocognit # Computes and checks the cognitive complexity of functions
5165
- goconst # Finds repeated strings that could be replaced by a constant
5266
- gocritic # The most opinionated Go source code linter
67+
- gocyclo # Computes and checks the cyclomatic complexity of functions
68+
- godot # Check if comments end in a period
5369
- godox # Tool for detection of FIXME, TODO and other comment keywords
54-
- err113 # Golang linter to check the errors handling expressions
5570
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
5671
- gofumpt # Gofumpt checks whether code was gofumpt-ed.
5772
- goheader # Checks is file header matches to pattern
5873
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
5974
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
60-
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
6175
- goprintffuncname # Checks that printf-like functions are named with `f` at the end
6276
- gosec # Inspects source code for security problems
6377
- gosimple # Linter for Go source code that specializes in simplifying a code
6478
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
6579
- grouper # An analyzer to analyze expression groups.
6680
- importas # Enforces consistent import aliases
6781
- ineffassign # Detects when assignments to existing variables are not used
82+
- lll # Reports long lines
83+
- maintidx # maintidx measures the maintainability index of each function.
84+
- makezero # Finds slice declarations with non-zero initial length
6885
- misspell # Finds commonly misspelled English words in comments
86+
- nakedret # Finds naked returns in functions greater than a specified function length
87+
- nestif # Reports deeply nested if statements
6988
- nilerr # Finds the code that returns nil even if it checks that the error is not nil.
7089
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value.
90+
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
7191
- noctx # noctx finds sending http request without context.Context
7292
- predeclared # find code that shadows one of Go's predeclared identifiers
7393
- revive # golint replacement, finds style mistakes
7494
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
7595
- stylecheck # Stylecheck is a replacement for golint
7696
- tagliatelle # Checks the struct tags.
7797
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
78-
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
98+
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
7999
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
80100
- unconvert # Remove unnecessary type conversions
81101
- unparam # Reports unused function parameters
82102
- unused # Checks Go code for unused constants, variables, functions and types
103+
- varnamelen # checks that the length of a variable's name matches its scope
83104
- wastedassign # wastedassign finds wasted assignment statements
84105
- whitespace # Tool for detection of leading and trailing whitespace
85106
disable:
86107
- depguard # Go linter that checks if package imports are in a list of acceptable packages
87-
- containedctx # containedctx is a linter that detects struct contained context.Context field
88-
- cyclop # checks function and package cyclomatic complexity
89108
- funlen # Tool for detection of long functions
90-
- gocyclo # Computes and checks the cyclomatic complexity of functions
91-
- godot # Check if comments end in a period
92-
- gomnd # An analyzer to detect magic numbers.
109+
- gochecknoinits # Checks that no init functions are present in Go code
110+
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
111+
- interfacebloat # A linter that checks length of interface.
93112
- ireturn # Accept Interfaces, Return Concrete Types
94-
- lll # Reports long lines
95-
- maintidx # maintidx measures the maintainability index of each function.
96-
- makezero # Finds slice declarations with non-zero initial length
97-
- nakedret # Finds naked returns in functions greater than a specified function length
98-
- nestif # Reports deeply nested if statements
99-
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
113+
- mnd # An analyzer to detect magic numbers
100114
- nolintlint # Reports ill-formed or insufficient nolint directives
101115
- paralleltest # paralleltest detects missing usage of t.Parallel() method in your Go test
102116
- prealloc # Finds slice declarations that could potentially be preallocated
103117
- promlinter # Check Prometheus metrics naming via promlint
104118
- rowserrcheck # checks whether Err of rows is checked successfully
105119
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed.
106120
- testpackage # linter that makes you use a separate _test package
107-
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
108-
- varnamelen # checks that the length of a variable's name matches its scope
121+
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
109122
- wrapcheck # Checks that errors returned from external packages are wrapped
110123
- wsl # Whitespace Linter - Forces you to use empty lines!
111124

decoder.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"github.com/pion/opus/internal/silk"
1313
)
1414

15-
// Decoder decodes the Opus bitstream into PCM
15+
// Decoder decodes the Opus bitstream into PCM.
1616
type Decoder struct {
1717
silkDecoder silk.Decoder
1818
silkBuffer []float32
1919
}
2020

21-
// NewDecoder creates a new Opus Decoder
21+
// NewDecoder creates a new Opus Decoder.
2222
func NewDecoder() Decoder {
2323
return Decoder{
2424
silkDecoder: silk.NewDecoder(),
@@ -47,7 +47,13 @@ func (d *Decoder) decode(in []byte, out []float32) (bandwidth Bandwidth, isStere
4747
}
4848

4949
for _, encodedFrame := range encodedFrames {
50-
err := d.silkDecoder.Decode(encodedFrame, out, tocHeader.isStereo(), cfg.frameDuration().nanoseconds(), silk.Bandwidth(cfg.bandwidth()))
50+
err := d.silkDecoder.Decode(
51+
encodedFrame,
52+
out,
53+
tocHeader.isStereo(),
54+
cfg.frameDuration().nanoseconds(),
55+
silk.Bandwidth(cfg.bandwidth()),
56+
)
5157
if err != nil {
5258
return 0, false, err
5359
}
@@ -56,24 +62,26 @@ func (d *Decoder) decode(in []byte, out []float32) (bandwidth Bandwidth, isStere
5662
return cfg.bandwidth(), tocHeader.isStereo(), nil
5763
}
5864

59-
// Decode decodes the Opus bitstream into S16LE PCM
65+
// Decode decodes the Opus bitstream into S16LE PCM.
6066
func (d *Decoder) Decode(in, out []byte) (bandwidth Bandwidth, isStereo bool, err error) {
6167
bandwidth, isStereo, err = d.decode(in, d.silkBuffer)
6268
if err != nil {
6369
return
6470
}
6571

6672
err = bitdepth.ConvertFloat32LittleEndianToSigned16LittleEndian(d.silkBuffer, out, 3)
73+
6774
return
6875
}
6976

70-
// DecodeFloat32 decodes the Opus bitstream into F32LE PCM
77+
// DecodeFloat32 decodes the Opus bitstream into F32LE PCM.
7178
func (d *Decoder) DecodeFloat32(in []byte, out []float32) (bandwidth Bandwidth, isStereo bool, err error) {
7279
bandwidth, isStereo, err = d.decode(in, d.silkBuffer)
7380
if err != nil {
7481
return
7582
}
7683

7784
resample.Up(d.silkBuffer, out, 3)
85+
7886
return
7987
}

decoder_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var (
2727
)
2828

2929
func loadTestOgg(tb testing.TB) []byte {
30+
tb.Helper()
31+
3032
if *testoggfile == "" {
3133
tb.Skip("-oggfile not specified")
3234
}
@@ -37,6 +39,7 @@ func loadTestOgg(tb testing.TB) []byte {
3739
if _testogg.err != nil {
3840
tb.Fatal("unable to load -oggfile", _testogg.err)
3941
}
42+
4043
return _testogg.data
4144
}
4245

@@ -49,6 +52,8 @@ func BenchmarkDecode(b *testing.B) {
4952
}
5053

5154
func benchmarkData(b *testing.B, data []byte) {
55+
b.Helper()
56+
5257
var out [1920]byte
5358
ogg, _, err := oggreader.NewWith(bytes.NewReader(data))
5459
if err != nil {

examples/decode/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/pion/opus/pkg/oggreader"
1515
)
1616

17-
func main() {
17+
func main() { // nolint:cyclop
1818
if len(os.Args) != 3 {
1919
panic("Usage: <in-file> <out-file>")
2020
}
@@ -30,7 +30,7 @@ func main() {
3030
}
3131

3232
out := make([]byte, 1920)
33-
f, err := os.Create(os.Args[2])
33+
fd, err := os.Create(os.Args[2])
3434
if err != nil {
3535
panic(err)
3636
}
@@ -54,7 +54,7 @@ func main() {
5454
panic(err)
5555
}
5656

57-
if _, err := f.Write(out); err != nil {
57+
if _, err := fd.Write(out); err != nil {
5858
panic(err)
5959
}
6060
}

internal/bitdepth/bitdepth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"math"
99
)
1010

11-
// ConvertFloat32LittleEndianToSigned16LittleEndian converts a f32le to s16le
11+
// ConvertFloat32LittleEndianToSigned16LittleEndian converts a f32le to s16le.
1212
func ConvertFloat32LittleEndianToSigned16LittleEndian(in []float32, out []byte, resampleCount int) error {
1313
currIndex := 0
1414
for i := range in {

internal/rangecoding/decoder.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,28 @@ func (r *Decoder) Init(data []byte) {
103103
//
104104
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.1.3.3
105105
func (r *Decoder) DecodeSymbolWithICDF(cumulativeDistributionTable []uint) uint32 {
106-
var k, scale, total, symbol, low, high uint32
106+
var k, scale, total, symbol, low, high uint32 //nolint:varnamelen
107107

108-
total = uint32(cumulativeDistributionTable[0])
108+
total = uint32(cumulativeDistributionTable[0]) //nolint:gosec // G115
109109
cumulativeDistributionTable = cumulativeDistributionTable[1:]
110110

111111
scale = r.rangeSize / total
112112
symbol = r.highAndCodedDifference/scale + 1
113-
symbol = total - uint32(localMin(uint(symbol), uint(total)))
113+
symbol = total - uint32(localMin(uint(symbol), uint(total))) //nolint:gosec // G115
114114

115115
// nolint: revive
116-
for k = 0; uint32(cumulativeDistributionTable[k]) <= symbol; k++ {
116+
for k = 0; uint32(cumulativeDistributionTable[k]) <= symbol; k++ { //nolint:gosec // G115
117117
}
118118

119-
high = uint32(cumulativeDistributionTable[k])
119+
high = uint32(cumulativeDistributionTable[k]) //nolint:gosec // G115
120120
if k != 0 {
121-
low = uint32(cumulativeDistributionTable[k-1])
121+
low = uint32(cumulativeDistributionTable[k-1]) //nolint:gosec // G115
122122
} else {
123123
low = 0
124124
}
125125

126126
r.update(scale, low, high, total)
127+
127128
return k
128129
}
129130

@@ -134,7 +135,7 @@ func (r *Decoder) DecodeSymbolWithICDF(cumulativeDistributionTable []uint) uint3
134135
//
135136
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.1.3.2
136137
func (r *Decoder) DecodeSymbolLogP(logp uint) uint32 {
137-
var k uint32
138+
var k uint32 //nolint:varnamelen
138139
scale := r.rangeSize >> logp
139140

140141
if r.highAndCodedDifference >= scale {
@@ -154,11 +155,12 @@ func (r *Decoder) getBit() uint32 {
154155
index := r.bitsRead / 8
155156
offset := r.bitsRead % 8
156157

157-
if index > uint(len(r.data)-1) {
158+
if index > uint(len(r.data)-1) { //nolint:gosec // G115
158159
return 0
159160
}
160161

161162
r.bitsRead++
163+
162164
return uint32((r.data[index] >> (7 - offset)) & 1)
163165
}
164166

@@ -213,7 +215,7 @@ func (r *Decoder) update(scale, low, high, total uint32) {
213215
r.normalize()
214216
}
215217

216-
// SetInternalValues is used when using the RangeDecoder when testing
218+
// SetInternalValues is used when using the RangeDecoder when testing.
217219
func (r *Decoder) SetInternalValues(data []byte, bitsRead uint, rangeSize uint32, highAndCodedDifference uint32) {
218220
r.data = data
219221
r.bitsRead = bitsRead
@@ -225,5 +227,6 @@ func localMin(a, b uint) uint {
225227
if a < b {
226228
return a
227229
}
230+
228231
return b
229232
}

0 commit comments

Comments
 (0)