Skip to content

Commit 51e00a8

Browse files
committed
lint: reduce warnings from golangci-lint
1 parent f5c6349 commit 51e00a8

14 files changed

+113
-72
lines changed

.vscode/settings.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
"--fast",
77
"--enable-all",
88
"--disable=errcheck",
9+
"--disable=exhaustivestruct",
910
"--disable=gas",
1011
"--disable=gochecknoglobals",
1112
"--disable=gochecknoinits",
1213
"--disable=gocyclo",
13-
"--disable=lll",
14-
"--disable=nakedret",
14+
"--disable=gomnd", // too many false positives
15+
"--disable=lll", // too opiniated
16+
"--disable=nakedret", // too opiniated
17+
"--disable=nlreturn", // too opiniated
18+
"--disable=paralleltest", // defaultTree is a singleton
19+
"--disable=testpackage", // we're testing internals
20+
"--disable=wsl", // too opiniated
1521
],
1622
"go.testFlags": ["-v"]
1723
}

convenience_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func (m *mockTree) Set(config, section, option string, values ...string) bool {
5555
return args.Bool(0)
5656
}
5757

58-
func (m *mockTree) SetType(config, section, option string, OptionType OptionType, values ...string) bool {
59-
args := m.Called(config, section, option, OptionType, values)
58+
func (m *mockTree) SetType(config, section, option string, typ OptionType, values ...string) bool {
59+
args := m.Called(config, section, option, typ, values)
6060
return args.Bool(0)
6161
}
6262

@@ -164,7 +164,7 @@ func TestConvenienceAddSection(t *testing.T) {
164164
assert := assert.New(t)
165165
m := defaultTree.(*mockTree)
166166
m.On("AddSection", "foo", "bar", "system").Return(nil)
167-
m.On("AddSection", "foo", "bar", "interface").Return(errors.New("invalid"))
167+
m.On("AddSection", "foo", "bar", "interface").Return(errors.New("invalid")) //nolint:goerr113
168168
assert.Error(AddSection("foo", "bar", "interface"))
169169
assert.NoError(AddSection("foo", "bar", "system"))
170170
m.AssertExpectations(t)

errors.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func (err ErrConfigAlreadyLoaded) Error() string {
1212
return fmt.Sprintf("%s already loaded", err.Name)
1313
}
1414

15-
// ErrUnknownOptionType is returned when trying to parse an invalid optiontype
15+
// ErrUnknownOptionType is returned when trying to parse an invalid OptionType.
1616
type ErrUnknownOptionType struct {
1717
Type string
1818
}
@@ -22,11 +22,13 @@ func (err ErrUnknownOptionType) Error() string {
2222
}
2323

2424
// IsConfigAlreadyLoaded reports, whether err is of type ErrConfigAlredyLoaded.
25+
//
26+
// Deprecated: use errors.Is or errors.As.
2527
func IsConfigAlreadyLoaded(err error) bool {
2628
if err == nil {
2729
return false
2830
}
29-
_, is := err.(*ErrConfigAlreadyLoaded)
31+
_, is := err.(*ErrConfigAlreadyLoaded) //nolint:errorlint
3032
return is
3133
}
3234

@@ -44,11 +46,13 @@ func (err ErrSectionTypeMismatch) Error() string {
4446
}
4547

4648
// IsSectionTypeMismatch reports, whether err is of type ErrSectionTypeMismatch.
49+
//
50+
// Deprecated: use errors.Is or errors.As.
4751
func IsSectionTypeMismatch(err error) bool {
4852
if err == nil {
4953
return false
5054
}
51-
_, is := err.(*ErrSectionTypeMismatch)
55+
_, is := err.(*ErrSectionTypeMismatch) //nolint:errorlint
5256
return is
5357
}
5458

@@ -58,10 +62,13 @@ func (err ParseError) Error() string {
5862
return fmt.Sprintf("parse error: %s", string(err))
5963
}
6064

65+
// IsParseError reports, whether err is of type ParseError.
66+
//
67+
// Deprecated: use errors.Is or errors.As.
6168
func IsParseError(err error) bool {
6269
if err == nil {
6370
return false
6471
}
65-
_, is := err.(*ParseError)
72+
_, is := err.(*ParseError) //nolint:errorlint
6673
return is
6774
}

items.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (ot *OptionType) UnmarshalJSON(b []byte) error {
5151
// https://talks.golang.org/2011/lex.slide#9
5252
type itemType int
5353

54-
// these items define the UCI language
54+
// These items define the UCI language.
5555
const (
5656
itemError itemType = iota // error occurred; item.val is text of error
5757

@@ -91,7 +91,7 @@ func (t itemType) String() string {
9191
}
9292

9393
// keyword represents a special marker of the input: each (trimmed,
94-
// non-empty) line of the input must start with a keywords
94+
// non-empty) line of the input must start with a keywords.
9595
type keyword string
9696

9797
// these are the recognized keywords.

items_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ func TestItemString(t *testing.T) {
6161
}, {
6262
item{itemError, "foo 0123456789 bar 0123456789", -1},
6363
`(Error "foo 0123456789 bar 0123456789")`,
64-
},
65-
66-
{
64+
}, {
6765
item{itemString, "foo", 42},
6866
`(String "foo" 42)`,
6967
}, {

lexer.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (l *lexer) emit(t itemType) {
9292
}
9393
}
9494

95-
// emitString emits a string token. it removes the surrounding quotes
95+
// emitString emits a string token. it removes the surrounding quotes.
9696
func (l *lexer) emitString(t itemType) {
9797
if l.pos-1 > l.start+1 {
9898
l.items <- item{t, l.input[l.start+1 : l.pos-1], l.pos}
@@ -199,7 +199,7 @@ func (l *lexer) errorf(format string, args ...interface{}) stateFn {
199199
return nil
200200
}
201201

202-
// rest returns the not-yet ingested part of l.input
202+
// rest returns the not-yet ingested part of l.input.
203203
func (l *lexer) rest() string {
204204
return l.input[l.pos:]
205205
}
@@ -222,7 +222,12 @@ func lexKeyword(l *lexer) stateFn {
222222
if l.next() == eof {
223223
l.emit(itemEOF)
224224
} else {
225-
l.errorf("expected keyword (package, config, option, list) or eof")
225+
l.backup()
226+
unexpected := l.rest()
227+
if len(unexpected) > 10 {
228+
unexpected = unexpected[:10] + "…"
229+
}
230+
l.errorf("expected keyword (package, config, option, list) or eof, got %q", unexpected)
226231
}
227232
return nil
228233
}

lexer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ func testLexer(t *testing.T, name, input string, expected []item) {
1818
t.Helper()
1919

2020
if dump["lex"] {
21-
defer fmt.Println("")
21+
defer fmt.Println("") //nolint:forbidigo
2222
}
2323

2424
l := lex(name, input)
2525
var i int
2626
for it := l.nextItem(); it.typ != itemEOF; it = l.nextItem() {
2727
if dump["lex"] {
28-
fmt.Print(it, " ")
28+
fmt.Print(it, " ") //nolint:forbidigo
2929
}
3030

3131
if i >= len(expected) {

parser.go

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package uci
22

33
import (
4-
"errors"
54
"fmt"
65
)
76

@@ -109,7 +108,7 @@ func (s *scanner) errorf(format string, args ...interface{}) scanFn {
109108

110109
// scanStart looks for a "package" or "config" item.
111110
func scanStart(s *scanner) scanFn {
112-
switch it := s.next(); it.typ {
111+
switch it := s.next(); it.typ { //nolint:exhaustive
113112
case itemPackage:
114113
return scanPackage
115114
case itemConfig:
@@ -118,26 +117,28 @@ func scanStart(s *scanner) scanFn {
118117
return s.errorf(it.val)
119118
case itemEOF:
120119
return nil
120+
default:
121+
return s.errorf("expected package or config token, got %s", it)
121122
}
122-
return s.errorf("expected package or config token")
123123
}
124124

125-
// scanPackage looks for a package name
125+
// scanPackage looks for a package name.
126126
func scanPackage(s *scanner) scanFn {
127-
switch it := s.next(); it.typ {
127+
switch it := s.next(); it.typ { //nolint:exhaustive
128128
case itemString:
129129
s.curr = append(s.curr, it)
130130
s.emit(tokPackage)
131131
return scanStart
132132
case itemError:
133133
return s.errorf(it.val)
134+
default:
135+
return s.errorf("expected string value while parsing package, got %s", it)
134136
}
135-
return s.errorf("expected string value while parsing package")
136137
}
137138

138-
// scanSection looks for a section type and optional a name
139+
// scanSection looks for a section type and optional a name.
139140
func scanSection(s *scanner) scanFn {
140-
switch it := s.next(); it.typ {
141+
switch it := s.next(); it.typ { //nolint:exhaustive
141142
case itemIdent:
142143
s.curr = append(s.curr, it)
143144
// the name is optional
@@ -148,15 +149,16 @@ func scanSection(s *scanner) scanFn {
148149
return scanOption
149150
case itemError:
150151
return s.errorf(it.val)
152+
default:
153+
return s.errorf("expected identifier while parsing config section, got %s", it)
151154
}
152-
return s.errorf("expected identifier while parsing config section")
153155
}
154156

155157
// scanOption looks for either an "option" or "list" keyword (with name
156-
// and value), or it falls back to scanStart
158+
// and value), or it falls back to scanStart.
157159
func scanOption(s *scanner) scanFn {
158160
it := s.next()
159-
switch it.typ {
161+
switch it.typ { //nolint:exhaustive
160162
case itemOption:
161163
return scanOptionName
162164
case itemList:
@@ -169,46 +171,48 @@ func scanOption(s *scanner) scanFn {
169171
}
170172
}
171173

172-
// scanOptionName looks for a name of a string option
174+
// scanOptionName looks for a name of a string option.
173175
func scanOptionName(s *scanner) scanFn {
174176
if s.accept(itemIdent) {
175177
return scanOptionValue
176178
}
177179
return s.errorf("expected option name")
178180
}
179181

180-
// scanListName looks for a name of a list option
182+
// scanListName looks for a name of a list option.
181183
func scanListName(s *scanner) scanFn {
182184
if s.accept(itemIdent) {
183185
return scanListValue
184186
}
185187
return s.errorf("expected option name")
186188
}
187189

188-
// scanOptionValue looks for the value associated with an option
190+
// scanOptionValue looks for the value associated with an option.
189191
func scanOptionValue(s *scanner) scanFn {
190-
switch it := s.next(); it.typ {
192+
switch it := s.next(); it.typ { //nolint:exhaustive
191193
case itemString:
192194
s.curr = append(s.curr, it)
193195
s.emit(tokOption)
194196
return scanOption
195197
case itemError:
196198
return s.errorf(it.val)
199+
default:
200+
return s.errorf("expected option value, got %s", it)
197201
}
198-
return s.errorf("expected option value")
199202
}
200203

201-
// scanListValue looks for the value associated with an option
204+
// scanListValue looks for the value associated with an option.
202205
func scanListValue(s *scanner) scanFn {
203-
switch it := s.next(); it.typ {
206+
switch it := s.next(); it.typ { //nolint:exhaustive
204207
case itemString:
205208
s.curr = append(s.curr, it)
206209
s.emit(tokList)
207210
return scanOption
208211
case itemError:
209212
return s.errorf(it.val)
213+
default:
214+
return s.errorf("expected option value, got %s", it)
210215
}
211-
return s.errorf("expected option value")
212216
}
213217

214218
func (s *scanner) each(fn func(token) bool) bool {
@@ -227,14 +231,14 @@ func parse(name, input string) (cfg *config, err error) {
227231
var sec *section
228232

229233
scan(name, input).each(func(tok token) bool {
230-
switch tok.typ {
234+
switch tok.typ { //nolint:exhaustive
231235
case tokError:
232236
perr := ParseError(tok.items[0].val)
233237
err = &perr
234238
return false
235239

236240
case tokPackage:
237-
err = errors.New("UCI imports/exports are not yet supported")
241+
err = ParseError("UCI imports/exports are not yet supported")
238242
return false
239243

240244
case tokSection:

parser_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func testParser(t *testing.T, name, input string, expected []token) {
2020
var i int
2121
ok := scan(name, input).each(func(tok token) bool {
2222
if dump["token"] {
23-
fmt.Println(tok)
23+
fmt.Println(tok) //nolint:forbidigo
2424
}
2525

2626
if i >= len(expected) {

test_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
//
1717
// DUMP="lex,token" go test -v ./...
1818
var dump = func() map[string]bool {
19-
var m = make(map[string]bool)
19+
m := make(map[string]bool)
2020
for _, field := range strings.Split(os.Getenv("DUMP"), ",") {
2121
if field == "all" {
2222
m["json"] = true
@@ -34,15 +34,18 @@ func (t scanToken) mk(items ...item) token {
3434
return token{t, items}
3535
}
3636

37-
func (i itemType) mk(val string) item {
38-
return item{i, val, -1}
37+
func (t itemType) mk(val string) item {
38+
return item{t, val, -1}
3939
}
4040

4141
const tcEmptyInput1 = ""
42+
4243
const tcEmptyInput2 = " \n\t\n\n \n "
44+
4345
const tcSimpleInput = `config sectiontype 'sectionname'
4446
option optionname 'optionvalue'
4547
`
48+
4649
const tcExportInput = `package "pkgname"
4750
config empty
4851
config squoted 'sqname'
@@ -176,7 +179,7 @@ var lexerTests = []struct {
176179
itemOption.mk("option"), itemIdent.mk("opt3"), itemString.mk("hello"),
177180
}},
178181
{"invalid", tcInvalid, []item{
179-
itemError.mk("expected keyword (package, config, option, list) or eof"),
182+
itemError.mk(`expected keyword (package, config, option, list) or eof, got "<?xml vers…"`),
180183
}},
181184
{"pkg invalid", tcIncompletePackage, []item{
182185
itemPackage.mk("package"),
@@ -248,7 +251,7 @@ var parserTests = []struct {
248251
tokOption.mk(itemIdent.mk("opt3"), itemString.mk("hello")),
249252
}},
250253
{"invalid", tcInvalid, []token{
251-
tokError.mk(itemError.mk("expected keyword (package, config, option, list) or eof")),
254+
tokError.mk(itemError.mk(`expected keyword (package, config, option, list) or eof, got "<?xml vers…"`)),
252255
}},
253256
{"pkg invalid", tcIncompletePackage, []token{
254257
tokError.mk(itemError.mk("incomplete package name")),

0 commit comments

Comments
 (0)