Skip to content

Commit 1a7c609

Browse files
authored
refactor: fix gocritic lint issues (#3113)
1 parent 4114515 commit 1a7c609

File tree

24 files changed

+48
-35
lines changed

24 files changed

+48
-35
lines changed

‎.golangci.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ run:
33
timeout: 5m
44

55
linters-settings:
6+
gocritic:
7+
enabled-checks:
8+
- emptyStringTest
9+
- equalFold
10+
- httpNoBody
11+
- nilValReturn
12+
- paramTypeCombine
13+
- preferFprint
14+
- yodaStyleExpr
615
errcheck:
716
exclude-functions:
817
- (io.Writer).Write
@@ -69,6 +78,10 @@ issues:
6978
linters:
7079
- dupl
7180
- errcheck
81+
# It's autogenerated code.
82+
- path: codegen/testserver/.*/resolver\.go
83+
linters:
84+
- gocritic
7285
# Disable revive.use-any for backwards compatibility
7386
- path: graphql/map.go
7487
text: "use-any: since GO 1.18 'interface{}' can be replaced by 'any'"

‎_examples/chat/resolvers.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type Chatroom struct {
6262

6363
type mutationResolver struct{ *resolver }
6464

65-
func (r *mutationResolver) Post(ctx context.Context, text string, username string, roomName string) (*Message, error) {
65+
func (r *mutationResolver) Post(ctx context.Context, text, username, roomName string) (*Message, error) {
6666
room := r.getRoom(roomName)
6767

6868
message := &Message{

‎_examples/scalars/model/model.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (b Banned) MarshalGQL(w io.Writer) {
2525
func (b *Banned) UnmarshalGQL(v any) error {
2626
switch v := v.(type) {
2727
case string:
28-
*b = strings.ToLower(v) == "true"
28+
*b = Banned(strings.EqualFold(v, "true"))
2929
return nil
3030
case bool:
3131
*b = Banned(v)

‎client/client.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ var boundaryRegex = regexp.MustCompile(`multipart/form-data; ?boundary=.*`)
111111
func (p *Client) newRequest(query string, options ...Option) (*http.Request, error) {
112112
bd := &Request{
113113
Query: query,
114-
HTTP: httptest.NewRequest(http.MethodPost, "/", nil),
114+
HTTP: httptest.NewRequest(http.MethodPost, "/", http.NoBody),
115115
}
116116
bd.HTTP.Header.Set("Content-Type", "application/json")
117117

@@ -128,7 +128,7 @@ func (p *Client) newRequest(query string, options ...Option) (*http.Request, err
128128
switch {
129129
case boundaryRegex.MatchString(contentType):
130130
break
131-
case "application/json" == contentType:
131+
case contentType == "application/json":
132132
requestBody, err := json.Marshal(bd)
133133
if err != nil {
134134
return nil, fmt.Errorf("encode: %w", err)
@@ -146,7 +146,7 @@ func (p *Client) SetCustomDecodeConfig(dc *mapstructure.DecoderConfig) {
146146
p.dc = dc
147147
}
148148

149-
func unpack(data any, into any, customDc *mapstructure.DecoderConfig) error {
149+
func unpack(data, into any, customDc *mapstructure.DecoderConfig) error {
150150
dc := &mapstructure.DecoderConfig{
151151
TagName: "json",
152152
ErrorUnused: true,

‎client/client_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func TestSetCustomDecodeConfig(t *testing.T) {
182182
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
183183
w.WriteHeader(http.StatusOK)
184184
w.Header().Set("Content-Type", "application/json")
185-
w.Write([]byte(fmt.Sprintf(`{"data": {"created_at":"%s"}}`, now.Format(time.RFC3339))))
185+
fmt.Fprintf(w, `{"data": {"created_at":"%s"}}`, now.Format(time.RFC3339))
186186
})
187187

188188
dc := &mapstructure.DecoderConfig{

‎client/options.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Path(url string) Option {
3636
}
3737

3838
// AddHeader adds a header to the outgoing request. This is useful for setting expected Authentication headers for example.
39-
func AddHeader(key string, value string) Option {
39+
func AddHeader(key, value string) Option {
4040
return func(bd *Request) {
4141
bd.HTTP.Header.Add(key, value)
4242
}

‎codegen/config/binder.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (b *Binder) FindTypeFromName(name string) (types.Type, error) {
6161
return b.FindType(pkgName, typeName)
6262
}
6363

64-
func (b *Binder) FindType(pkgName string, typeName string) (types.Type, error) {
64+
func (b *Binder) FindType(pkgName, typeName string) (types.Type, error) {
6565
if pkgName == "" {
6666
if typeName == "map[string]interface{}" {
6767
return MapType, nil
@@ -123,7 +123,7 @@ func (b *Binder) DefaultUserObject(name string) (types.Type, error) {
123123
return obj.Type(), nil
124124
}
125125

126-
func (b *Binder) FindObject(pkgName string, typeName string) (types.Object, error) {
126+
func (b *Binder) FindObject(pkgName, typeName string) (types.Object, error) {
127127
if pkgName == "" {
128128
return nil, errors.New("package cannot be nil")
129129
}
@@ -349,7 +349,7 @@ func isIntf(t types.Type) bool {
349349

350350
func unwrapOmittable(t types.Type) (types.Type, bool) {
351351
if t == nil {
352-
return t, false
352+
return nil, false
353353
}
354354
named, ok := t.(*types.Named)
355355
if !ok {

‎codegen/config/config.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ func (tm TypeMap) ReferencedPackages() []string {
644644
return pkgs
645645
}
646646

647-
func (tm TypeMap) Add(name string, goType string) {
647+
func (tm TypeMap) Add(name, goType string) {
648648
modelCfg := tm[name]
649649
modelCfg.Model = append(modelCfg.Model, goType)
650650
tm[name] = modelCfg

‎codegen/templates/templates.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func parseTemplates(cfg Options, t *template.Template) (*template.Template, erro
185185
return t, nil
186186
}
187187

188-
func center(width int, pad string, s string) string {
188+
func center(width int, pad, s string) string {
189189
if len(s)+2 > width {
190190
return s
191191
}

‎graphql/bool.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func MarshalBoolean(b bool) Marshaler {
1515
func UnmarshalBoolean(v any) (bool, error) {
1616
switch v := v.(type) {
1717
case string:
18-
return strings.ToLower(v) == "true", nil
18+
return strings.EqualFold(v, "true"), nil
1919
case int:
2020
return v != 0, nil
2121
case bool:

0 commit comments

Comments
 (0)