Skip to content

Commit 91eb849

Browse files
authored
Update linter configuration and fix linting issues (#3694)
1 parent 4411458 commit 91eb849

30 files changed

+94
-90
lines changed

.golangci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
version: "2"
12
linters:
23
enable:
34
- errorlint
5+
- errcheck
6+
- staticcheck
7+

Makefile

+1-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ depend:
7070

7171
lint:
7272
ifneq ($(GOOS),windows)
73-
@if [ "`golangci-lint run ./... | grep -v ".pb.go" | tee /dev/stderr`" ]; then \
74-
echo "^ - lint errors!" && echo && exit 1; \
75-
fi
73+
@golangci-lint run ./... || (echo "^ - lint errors!" && echo && exit 1)
7674
endif
7775

7876
test:

cmd/goa/gen.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (g *Generator) Run() ([]string, error) {
226226
// Remove deletes the package files.
227227
func (g *Generator) Remove() {
228228
if g.tmpDir != "" {
229-
os.RemoveAll(g.tmpDir)
229+
_ = os.RemoveAll(g.tmpDir)
230230
g.tmpDir = ""
231231
}
232232
}
@@ -237,7 +237,7 @@ func (g *Generator) runGoCmd(args ...string) error {
237237
return fmt.Errorf(`failed to find a go compiler, looked in "%s"`, os.Getenv("PATH"))
238238
}
239239
if g.DesignVersion > 2 {
240-
os.Setenv("GO111MODULE", "on")
240+
_ = os.Setenv("GO111MODULE", "on")
241241
}
242242
c := exec.Cmd{
243243
Path: gobin,

codegen/file.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ func finalizeGoSource(path string) error {
165165
if err := format.Node(w, fset, file); err != nil {
166166
return err
167167
}
168-
w.Close()
168+
if err := w.Close(); err != nil {
169+
return err
170+
}
169171

170172
// Format code using goimport standard
171173
bs, err := os.ReadFile(path)

codegen/go_transform.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func transformObject(source, target *expr.AttributeExpr, sourceVar, targetVar st
197197
assign = ":="
198198
}
199199
name := ta.TargetCtx.Scope.Name(target, ta.TargetCtx.Pkg(target), ta.TargetCtx.Pointer, ta.TargetCtx.UseDefault)
200-
buffer.WriteString(fmt.Sprintf("%s %s %s%s{%s}\n", targetVar, assign, deref, name, initCode))
200+
fmt.Fprintf(buffer, "%s %s %s%s{%s}\n", targetVar, assign, deref, name, initCode)
201201
buffer.WriteString(postInitCode)
202202

203203
// iterate through attributes to initialize rest of the struct fields and

codegen/service/convert_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func objRecursive() *expr.UserTypeExpr {
308308
},
309309
TypeName: "objRecursiveT",
310310
}
311-
obj := res.AttributeExpr.Type.(*expr.Object)
311+
obj := res.Type.(*expr.Object)
312312
*obj = append(*obj, &expr.NamedAttributeExpr{
313313
Name: "Rec",
314314
Attribute: &expr.AttributeExpr{Type: res}})

codegen/service/service_data.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1018,11 +1018,11 @@ func collectUnionMethods(att *expr.AttributeExpr, scope *codegen.NameScope, loc
10181018

10191019
// buildErrorInitData creates the data needed to generate code around endpoint error return values.
10201020
func buildErrorInitData(er *expr.ErrorExpr, scope *codegen.NameScope) *ErrorInitData {
1021-
_, temporary := er.AttributeExpr.Meta["goa:error:temporary"]
1022-
_, timeout := er.AttributeExpr.Meta["goa:error:timeout"]
1023-
_, fault := er.AttributeExpr.Meta["goa:error:fault"]
1021+
_, temporary := er.Meta["goa:error:temporary"]
1022+
_, timeout := er.Meta["goa:error:timeout"]
1023+
_, fault := er.Meta["goa:error:fault"]
10241024
var pkg string
1025-
if ut, ok := er.AttributeExpr.Type.(expr.UserType); ok {
1025+
if ut, ok := er.Type.(expr.UserType); ok {
10261026
pkg = codegen.UserTypeLocation(ut).PackageName()
10271027
}
10281028
return &ErrorInitData{
@@ -1098,7 +1098,7 @@ func buildMethodData(m *expr.MethodExpr, scope *codegen.NameScope) *MethodData {
10981098
errorLocs = make(map[string]*codegen.Location, len(m.Errors))
10991099
for i, er := range m.Errors {
11001100
errors[i] = buildErrorInitData(er, scope)
1101-
errorLocs[er.Name] = codegen.UserTypeLocation(er.AttributeExpr.Type)
1101+
errorLocs[er.Name] = codegen.UserTypeLocation(er.Type)
11021102
}
11031103
}
11041104
for _, req := range m.Requirements {
@@ -1638,7 +1638,7 @@ func buildProjectedType(projected, att *expr.AttributeExpr, viewspkg string, sco
16381638
func buildViews(rt *expr.ResultTypeExpr, viewScope *codegen.NameScope) []*ViewData {
16391639
views := make([]*ViewData, len(rt.Views))
16401640
for i, view := range rt.Views {
1641-
vatt := expr.AsObject(view.AttributeExpr.Type)
1641+
vatt := expr.AsObject(view.Type)
16421642
attrs := make([]string, len(*vatt))
16431643
for j, nat := range *vatt {
16441644
attrs[j] = nat.Name
@@ -2072,7 +2072,7 @@ func buildConstructorCode(src, tgt *expr.AttributeExpr, sourceVar, targetVar str
20722072
finit := "new" + targetCtx.Scope.Name(nat.Attribute, "", targetCtx.Pointer, targetCtx.UseDefault)
20732073
if view != "" {
20742074
v := ""
2075-
if vatt := rt.View(view).AttributeExpr.Find(nat.Name); vatt != nil {
2075+
if vatt := rt.View(view).Find(nat.Name); vatt != nil {
20762076
if attv, ok := vatt.Meta.Last(expr.ViewMetaKey); ok && attv != expr.DefaultView {
20772077
// view is explicitly set for the result type on the attribute
20782078
v = attv

codegen/testing.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func sectionCodeWithPrefix(t *testing.T, section *SectionTemplate, prefix string
6666
func FormatTestCode(t *testing.T, code string) string {
6767
t.Helper()
6868
tmp := CreateTempFile(t, code)
69-
defer os.Remove(tmp)
69+
defer func() { _ = os.Remove(tmp) }()
7070
require.NoError(t, finalizeGoSource(tmp))
7171
content, err := os.ReadFile(tmp)
7272
require.NoError(t, err)
@@ -81,7 +81,7 @@ func CreateTempFile(t *testing.T, content string) string {
8181
require.NoError(t, err)
8282
_, err = f.WriteString(content)
8383
if err != nil {
84-
os.Remove(f.Name())
84+
require.NoError(t, os.Remove(f.Name()))
8585
t.Fatal(err)
8686
}
8787
require.NoError(t, f.Close())

dsl/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ func Body(args ...any) {
914914
}
915915
kind = "Request"
916916
case *expr.HTTPErrorExpr:
917-
ref = e.ErrorExpr.AttributeExpr
917+
ref = e.AttributeExpr
918918
setter = func(att *expr.AttributeExpr) {
919919
if e.Response == nil {
920920
e.Response = &expr.HTTPResponseExpr{}

dsl/result_type_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestView(t *testing.T) {
8484
found = true
8585
for _, attr := range c.expectedViewAttrs[view] {
8686
found2 := false
87-
for _, attr2 := range *v.AttributeExpr.Type.(*expr.Object) {
87+
for _, attr2 := range *v.Type.(*expr.Object) {
8888
if attr2.Name == attr {
8989
found2 = true
9090
break

expr/attribute.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ func (a *AttributeExpr) debug(prefix string, seen map[*AttributeExpr]int, indent
665665
if rt, ok := a.Type.(*ResultTypeExpr); ok {
666666
fmt.Printf("%s%sviews\n", tabs, tab)
667667
for _, v := range rt.Views {
668-
nats := *AsObject(v.AttributeExpr.Type)
668+
nats := *AsObject(v.Type)
669669
keys := make([]string, len(nats))
670670
for i, n := range nats {
671671
keys[i] = n.Name

expr/http_body_types.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ func httpRequestBody(a *HTTPEndpointExpr) *AttributeExpr {
195195
if t, ok := payload.Type.(UserType); ok {
196196
// Remember openapi typename for example to generate friendly OpenAPI specs.
197197
if m, ok := t.Attribute().Meta["openapi:typename"]; ok {
198-
ut.AttributeExpr.AddMeta("openapi:typename", m...)
198+
ut.AddMeta("openapi:typename", m...)
199199
}
200200

201201
// Remember additionalProperties.
202202
if m, ok := t.Attribute().Meta["openapi:additionalProperties"]; ok {
203-
ut.AttributeExpr.AddMeta("openapi:additionalProperties", m...)
203+
ut.AddMeta("openapi:additionalProperties", m...)
204204
}
205205
}
206206

@@ -262,7 +262,7 @@ func httpResponseBody(a *HTTPEndpointExpr, resp *HTTPResponseExpr) *AttributeExp
262262
// parameters.
263263
func httpErrorResponseBody(e *HTTPEndpointExpr, v *HTTPErrorExpr) *AttributeExpr {
264264
name := e.Name() + "_" + v.ErrorExpr.Name
265-
return buildHTTPResponseBody(name, v.ErrorExpr.AttributeExpr, v.Response, e.Service)
265+
return buildHTTPResponseBody(name, v.AttributeExpr, v.Response, e.Service)
266266
}
267267

268268
func buildHTTPResponseBody(name string, attr *AttributeExpr, resp *HTTPResponseExpr, svc *HTTPServiceExpr) *AttributeExpr {
@@ -342,14 +342,14 @@ func buildHTTPResponseBody(name string, attr *AttributeExpr, resp *HTTPResponseE
342342
if t, ok := attr.Type.(UserType); ok {
343343
// Remember original type name and openapi typename for example
344344
// to generate friendly OpenAPI specs.
345-
userType.AttributeExpr.AddMeta("name:original", t.Name())
345+
userType.AddMeta("name:original", t.Name())
346346
if m, ok := t.Attribute().Meta["openapi:typename"]; ok {
347-
userType.AttributeExpr.AddMeta("openapi:typename", m...)
347+
userType.AddMeta("openapi:typename", m...)
348348
}
349349

350350
// Remember additionalProperties.
351351
if m, ok := t.Attribute().Meta["openapi:additionalProperties"]; ok {
352-
userType.AttributeExpr.AddMeta("openapi:additionalProperties", m...)
352+
userType.AddMeta("openapi:additionalProperties", m...)
353353
}
354354
}
355355

expr/http_endpoint_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestHTTPEndpointPrepare(t *testing.T) {
7272
root := expr.RunDSL(t, c.DSL)
7373
e := root.API.HTTP.Services[len(root.API.HTTP.Services)-1].HTTPEndpoints[0]
7474

75-
ht := expr.AsObject(e.Headers.AttributeExpr.Type)
75+
ht := expr.AsObject(e.Headers.Type)
7676
if len(*ht) != len(c.Headers) {
7777
t.Errorf("got %d headers, expected %d", len(*ht), len(c.Headers))
7878
} else {
@@ -83,7 +83,7 @@ func TestHTTPEndpointPrepare(t *testing.T) {
8383
}
8484
}
8585

86-
ct := expr.AsObject(e.Cookies.AttributeExpr.Type)
86+
ct := expr.AsObject(e.Cookies.Type)
8787
if len(*ct) != len(c.Cookies) {
8888
t.Errorf("got %d cookies, expected %d", len(*ct), len(c.Cookies))
8989
} else {
@@ -94,7 +94,7 @@ func TestHTTPEndpointPrepare(t *testing.T) {
9494
}
9595
}
9696

97-
pt := expr.AsObject(e.Params.AttributeExpr.Type)
97+
pt := expr.AsObject(e.Params.Type)
9898
if len(*pt) != len(c.Params) {
9999
t.Errorf("got %d params, expected %d", len(*pt), len(c.Params))
100100
} else {

expr/mapped_attribute.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (ma *MappedAttributeExpr) Remap() {
7777
}
7878

7979
// Conserve examples defined on user type
80-
ma.AttributeExpr.UserExamples = ma.ExtractUserExamples()
80+
ma.UserExamples = ma.ExtractUserExamples()
8181

8282
ma.Type = n
8383
}

expr/method.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (m *MethodExpr) validateErrors() *eval.ValidationErrors {
227227
return nil
228228
})
229229
if !found {
230-
verr.Add(e, "type %q is used to define multiple errors and must identify the attribute containing the error name with ErrorName", e.AttributeExpr.Type.Name())
230+
verr.Add(e, "type %q is used to define multiple errors and must identify the attribute containing the error name with ErrorName", e.Type.Name())
231231
break
232232
}
233233
}

expr/project_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ func TestProject(t *testing.T) {
5959
t.Fatal(err)
6060
}
6161
if !Equal(projected, k.Expected) {
62-
projected.AttributeExpr.Debug("got")
63-
k.Expected.AttributeExpr.Debug("expected")
62+
projected.Debug("got")
63+
k.Expected.Debug("expected")
6464
t.Errorf("got: %s, expected: %s\n", Hash(projected, false, true, true), Hash(k.Expected, false, true, true))
6565
}
66-
if pobj := AsObject(projected.AttributeExpr.Type); pobj != nil {
66+
if pobj := AsObject(projected.Type); pobj != nil {
6767
for _, att := range *pobj {
68-
att2 := k.Expected.AttributeExpr.Find(att.Name)
68+
att2 := k.Expected.Find(att.Name)
6969
if att2 == nil {
7070
continue
7171
}

expr/result_type.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (rt *ResultTypeExpr) ViewHasAttribute(view, attr string) bool {
170170
if v == nil {
171171
return false
172172
}
173-
return v.AttributeExpr.Find(attr) != nil
173+
return v.Find(attr) != nil
174174
}
175175

176176
// Finalize builds the default view if not explicitly defined and finalizes
@@ -195,7 +195,7 @@ func (rt *ResultTypeExpr) Finalize() {
195195
// useExplicitView projects the result type using the view explicitly set on the
196196
// attribute if any.
197197
func (rt *ResultTypeExpr) useExplicitView() {
198-
if view, ok := rt.AttributeExpr.Meta.Last(ViewMetaKey); ok {
198+
if view, ok := rt.Meta.Last(ViewMetaKey); ok {
199199
p, err := Project(rt, view)
200200
if err != nil {
201201
panic(err) // bug - presence of view meta should have been validated before
@@ -300,8 +300,8 @@ func projectSingle(rt *ResultTypeExpr, view string, seen map[string]*AttributeEx
300300
}
301301
ut.TypeName = typeName
302302
ut.UID = id
303-
ut.AttributeExpr.Type = Dup(v.Type)
304-
ut.AttributeExpr.UserExamples = v.UserExamples
303+
ut.Type = Dup(v.Type)
304+
ut.UserExamples = v.UserExamples
305305
projected := &ResultTypeExpr{
306306
Identifier: id,
307307
UserTypeExpr: ut,

expr/service.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ func (e *ErrorExpr) Validate() error {
109109
walkAttribute(e.AttributeExpr, func(name string, att *AttributeExpr) error { // nolint: errcheck
110110
if _, ok := att.Meta["struct:error:name"]; ok {
111111
if errField != "" {
112-
verr.Add(e, "duplicate error names in type %q", e.AttributeExpr.Type.Name())
112+
verr.Add(e, "duplicate error names in type %q", e.Type.Name())
113113
}
114114
errField = name
115115
if att.Type != String {
116-
verr.Add(e, "error name %q must be a string in type %q", name, e.AttributeExpr.Type.Name())
116+
verr.Add(e, "error name %q must be a string in type %q", name, e.Type.Name())
117117
}
118-
if !e.AttributeExpr.IsRequired(name) {
119-
verr.Add(e, "error name %q must be required in type %q", name, e.AttributeExpr.Type.Name())
118+
if !e.IsRequired(name) {
119+
verr.Add(e, "error name %q must be required in type %q", name, e.Type.Name())
120120
}
121121
}
122122
return nil

expr/testing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func CreateTempFile(t *testing.T, content string) string {
5757
}
5858
_, err = f.WriteString(content)
5959
if err != nil {
60-
os.Remove(f.Name())
60+
_ = os.Remove(f.Name())
6161
t.Fatal(err)
6262
}
6363
if err := f.Close(); err != nil {

expr/user_type.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (u *UserTypeExpr) Name() string {
3333
if u.AttributeExpr == nil {
3434
return u.TypeName
3535
}
36-
if n, ok := u.AttributeExpr.Meta["struct:type:name"]; ok {
36+
if n, ok := u.Meta["struct:type:name"]; ok {
3737
return n[0]
3838
}
3939
return u.TypeName
@@ -42,8 +42,8 @@ func (u *UserTypeExpr) Name() string {
4242
// Rename changes the type name to the given value.
4343
func (u *UserTypeExpr) Rename(n string) {
4444
// Remember original name for example to generate friendly docs.
45-
u.AttributeExpr.AddMeta("name:original", u.TypeName)
46-
delete(u.AttributeExpr.Meta, "struct:type:name")
45+
u.AddMeta("name:original", u.TypeName)
46+
delete(u.Meta, "struct:type:name")
4747
u.TypeName = n
4848
}
4949

grpc/codegen/protobuf_transform.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ func protoBufTransform(source, target *expr.AttributeExpr, sourceVar, targetVar
8585
if proto {
8686
target = expr.DupAtt(target)
8787
removeMeta(target)
88-
ta.TransformAttrs.Prefix = "svc"
88+
ta.Prefix = "svc"
8989
ta.proto = true
9090
} else {
9191
source = expr.DupAtt(source)
9292
removeMeta(source)
93-
ta.TransformAttrs.Prefix = "protobuf"
93+
ta.Prefix = "protobuf"
9494
ta.proto = false
9595
}
9696

@@ -268,7 +268,7 @@ func transformObject(source, target *expr.AttributeExpr, sourceVar, targetVar st
268268
assign = ":="
269269
}
270270
tname := ta.TargetCtx.Scope.Name(target, ta.TargetCtx.Pkg(target), ta.TargetCtx.Pointer, ta.TargetCtx.UseDefault)
271-
buffer.WriteString(fmt.Sprintf("%s %s %s%s{%s}\n", targetVar, assign, deref, tname, initCode))
271+
fmt.Fprintf(buffer, "%s %s %s%s{%s}\n", targetVar, assign, deref, tname, initCode)
272272
buffer.WriteString(postInitCode)
273273

274274
// iterate through attributes to initialize rest of the struct fields and

0 commit comments

Comments
 (0)