Skip to content

Commit d83e159

Browse files
authored
Enable staticcheck and fix issues (uber-go#302)
This adds staticcheck to our `make lint` and fixes issues reported by it. Of the failures reported by staticcheck, the following were actual issues: - shallowCheckDependencies: addMissingNodes was unused. I think this was intended to go into visualiziation somehow, but we never did it. - digSentinel fields on dig.In/Out can be hidden away - A test validating that we don't allow the `Group` provide option for `dig.Out`-based structs was not actually using the result struct declared there so it wasn't testing what it claimed to test. Besides that, there were a number of unused fields in test structs. These are intentionally unused fields so I've appeased the linter by referencing them with `_ = ..` assignments.
1 parent 9b89655 commit d83e159

File tree

13 files changed

+150
-43
lines changed

13 files changed

+150
-43
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
${{ runner.os }}-go-
3737
3838
- name: Download Dependencies
39-
run: go mod download
39+
run: make install
4040

4141
- name: Lint
4242
if: matrix.latest

Makefile

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
export GOBIN ?= $(shell pwd)/bin
22

3-
BENCH_FLAGS ?= -cpuprofile=cpu.pprof -memprofile=mem.pprof -benchmem
43
GOLINT = $(GOBIN)/golint
4+
STATICCHECK = $(GOBIN)/staticcheck
5+
6+
BENCH_FLAGS ?= -cpuprofile=cpu.pprof -memprofile=mem.pprof -benchmem
57

6-
GO_FILES := $(shell \
8+
GO_FILES = $(shell \
79
find . '(' -path '*/.*' -o -path './vendor' ')' -prune \
810
-o -name '*.go' -print | cut -b3-)
911

12+
MODULES = . ./tools
13+
1014
.PHONY: all
1115
all: build lint test
1216

1317
.PHONY: build
1418
build:
1519
go build ./...
1620

21+
.PHONY: install
22+
install:
23+
$(foreach dir,$(MODULES),( \
24+
cd $(dir) && \
25+
go mod download) && \
26+
) true
27+
1728
.PHONY: lint
18-
lint: $(GOLINT)
29+
lint: $(GOLINT) $(STATICCHECK)
1930
@rm -rf lint.log
2031
@echo "Checking formatting..."
2132
@gofmt -d -s $(GO_FILES) 2>&1 | tee lint.log
2233
@echo "Checking vet..."
2334
@go vet ./... 2>&1 | tee -a lint.log
2435
@echo "Checking lint..."
2536
@$(GOLINT) ./... 2>&1 | tee -a lint.log
37+
@echo "Checking staticcheck..."
38+
@$(STATICCHECK) ./... 2>&1 | tee -a lint.log
2639
@echo "Checking for unresolved FIXMEs..."
2740
@git grep -i fixme | grep -v -e Makefile | tee -a lint.log
2841
@echo "Checking for license headers..."
2942
@./check_license.sh | tee -a lint.log
3043
@[ ! -s lint.log ]
3144

32-
$(GOLINT):
33-
go install golang.org/x/lint/golint
45+
$(GOLINT): tools/go.mod
46+
cd tools && go install golang.org/x/lint/golint
47+
48+
$(STATICCHECK): tools/go.mod
49+
cd tools && go install honnef.co/go/tools/cmd/staticcheck
3450

3551
.PHONY: test
3652
test:
@@ -45,3 +61,7 @@ cover:
4561
BENCH ?= .
4662
bench:
4763
go list ./... | xargs -n1 go test -bench=$(BENCH) -run="^$$" $(BENCH_FLAGS)
64+
65+
.PHONY: tidy
66+
tidy:
67+
$(foreach dir,$(MODULES),(cd $(dir) && go mod tidy) &&) true

dig_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,9 @@ func TestGroups(t *testing.T) {
10671067
}
10681068

10691069
func(i int) {
1070-
require.Error(t, c.Provide(func() {
1070+
require.Error(t, c.Provide(func() out {
10711071
t.Fatal("This should not be called")
1072+
return out{}
10721073
}, Group("val")), "This Provide should fail")
10731074
}(1)
10741075
})
@@ -1425,7 +1426,7 @@ func TestProvideConstructorErrors(t *testing.T) {
14251426

14261427
t.Run("name option cannot be provided for result structs", func(t *testing.T) {
14271428
c := New()
1428-
type A struct{ idx int }
1429+
type A struct{}
14291430

14301431
type out struct {
14311432
Out
@@ -2660,6 +2661,9 @@ func testInvokeFailures(t *testing.T, dryRun bool) {
26602661
A1 A // all is good
26612662
a2 A // oops, unexported type
26622663
}
2664+
2665+
_ = in{}.a2 // unused but needed for the test
2666+
26632667
require.NoError(t, c.Provide(func() A { return A{} }))
26642668

26652669
err := c.Invoke(func(i in) { assert.Fail(t, "should never get in here") })
@@ -2679,6 +2683,8 @@ func testInvokeFailures(t *testing.T, dryRun bool) {
26792683
foo string
26802684
}
26812685

2686+
_ = in{}.foo // unused but needed for the test
2687+
26822688
err := c.Provide(func(in) int { return 0 })
26832689
require.Error(t, err, "Provide must fail")
26842690
assertErrorMatches(t, err,
@@ -2702,6 +2708,9 @@ func testInvokeFailures(t *testing.T, dryRun bool) {
27022708
type in struct {
27032709
Embed
27042710
}
2711+
2712+
_ = in{}.a2 // unused but needed for the test
2713+
27052714
require.NoError(t, c.Provide(func() A { return A{} }))
27062715

27072716
err := c.Invoke(func(i in) { assert.Fail(t, "should never get in here") })
@@ -2721,6 +2730,9 @@ func testInvokeFailures(t *testing.T, dryRun bool) {
27212730

27222731
string // embed an unexported std type
27232732
}
2733+
2734+
_ = param{}.string // unused but needed for the test
2735+
27242736
err := c.Invoke(func(p param) { assert.Fail(t, "should never get here") })
27252737
require.Error(t, err)
27262738
assertErrorMatches(t, err,
@@ -3229,6 +3241,7 @@ func TestUnexportedFieldsFailures(t *testing.T) {
32293241
type type1 struct{}
32303242
type type2 struct{}
32313243
type type3 struct{}
3244+
32323245
constructor := func() (*type1, *type2) {
32333246
return &type1{}, &type2{}
32343247
}
@@ -3241,10 +3254,12 @@ func TestUnexportedFieldsFailures(t *testing.T) {
32413254
T2 *type2 `optional:"true"` // optional type present in the graph
32423255
t3 *type3
32433256
}
3257+
32443258
require.NoError(t, c.Provide(constructor))
32453259
err := c.Invoke(func(p param) {
32463260
require.NotNil(t, p.T1, "whole param struct should not be nil")
32473261
assert.NotNil(t, p.T2, "optional type in the graph should not return nil")
3262+
_ = p.t3 // unused
32483263
})
32493264
require.Error(t, err)
32503265
assert.Contains(t, err.Error(),
@@ -3267,10 +3282,12 @@ func TestUnexportedFieldsFailures(t *testing.T) {
32673282
T2 *type2 `optional:"true"` // optional type present in the graph
32683283
t3 *type3
32693284
}
3285+
32703286
require.NoError(t, c.Provide(constructor))
32713287
err := c.Invoke(func(p param) {
32723288
require.NotNil(t, p.T1, "whole param struct should not be nil")
32733289
assert.NotNil(t, p.T2, "optional type in the graph should not return nil")
3290+
_ = p.t3
32743291
})
32753292
require.Error(t, err)
32763293
assert.Contains(t, err.Error(),

go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@ module go.uber.org/dig
22

33
go 1.13
44

5-
require (
6-
github.com/stretchr/testify v1.4.0
7-
golang.org/x/lint v0.0.0-20190930215403-16217165b5de
8-
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab // indirect
9-
)
5+
require github.com/stretchr/testify v1.4.0

go.sum

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
55
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
66
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
77
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
8-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
9-
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
10-
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
11-
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
12-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
13-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
14-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
15-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
16-
golang.org/x/tools v0.0.0-20190311212946-11955173bddd h1:/e+gpKk9r3dJobndpTytxS2gOy6m5uvpg+ISQoEcusQ=
17-
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
18-
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab h1:tpc/nJ4vD66vAk/2KN0sw/DvQIz2sKmCpWvyKtPmfMQ=
19-
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
20-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
218
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
229
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2310
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

inout.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ var (
3535
_outType = reflect.TypeOf(Out{})
3636
)
3737

38-
// Special interface embedded inside dig sentinel values (dig.In, dig.Out) to
39-
// make their special nature obvious in the godocs. Otherwise they will appear
40-
// as plain empty structs.
41-
type digSentinel interface {
42-
digSentinel()
43-
}
38+
// Placeholder type placed in dig.In/dig.out to make their special nature
39+
// obvious in godocs.
40+
// Otherwise they will appear as plain empty structs.
41+
type digSentinel struct{}
4442

4543
// In may be embedded into structs to request dig to treat them as special
4644
// parameter structs. When a constructor accepts such a struct, instead of the
@@ -58,7 +56,7 @@ type digSentinel interface {
5856
// group Name of the Value Group from which this field will be filled.
5957
// The field must be a slice type. See Value Groups in the
6058
// package documentation for more information.
61-
type In struct{ digSentinel }
59+
type In struct{ _ digSentinel }
6260

6361
// Out is an embeddable type that signals to dig that the returned
6462
// struct should be treated differently. Instead of the struct itself
@@ -79,7 +77,7 @@ type In struct{ digSentinel }
7977
// group Name of the Value Group to which this field's value is being
8078
// sent. See Value Groups in the package documentation for more
8179
// information.
82-
type Out struct{ digSentinel }
80+
type Out struct{ _ digSentinel }
8381

8482
func isError(t reflect.Type) bool {
8583
return t.Implements(_errType)

invoke.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"reflect"
2626

2727
"go.uber.org/dig/internal/digreflect"
28-
"go.uber.org/dig/internal/dot"
2928
"go.uber.org/dig/internal/graph"
3029
)
3130

@@ -95,12 +94,10 @@ func (c *Container) Invoke(function interface{}, opts ...InvokeOption) error {
9594
// the container. Returns an error if not.
9695
func shallowCheckDependencies(c containerStore, pl paramList) error {
9796
var err errMissingTypes
98-
var addMissingNodes []*dot.Param
9997

10098
missingDeps := findMissingDependencies(c, pl.Params...)
10199
for _, dep := range missingDeps {
102100
err = append(err, newErrMissingTypes(c, key{name: dep.Name, t: dep.Type})...)
103-
addMissingNodes = append(addMissingNodes, dep.DotParam()...)
104101
}
105102

106103
if len(err) > 0 {

param_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func TestParamObjectWithUnexportedFieldsSuccess(t *testing.T) {
112112
t2 type2
113113
}
114114

115+
_ = in{}.t2 // unused
116+
115117
po, err := newParamObject(reflect.TypeOf(in{}), New())
116118
require.NoError(t, err)
117119

@@ -134,6 +136,8 @@ func TestParamObjectFailure(t *testing.T) {
134136
a2 A
135137
}
136138

139+
_ = in{}.a2 // unused but needed
140+
137141
_, err := newParamObject(reflect.TypeOf(in{}), New())
138142
require.Error(t, err)
139143
assert.Contains(t, err.Error(),
@@ -149,6 +153,8 @@ func TestParamObjectFailure(t *testing.T) {
149153
a2 A
150154
}
151155

156+
_ = in{}.a2 // unused but needed
157+
152158
_, err := newParamObject(reflect.TypeOf(in{}), New())
153159
require.Error(t, err)
154160
assert.Contains(t, err.Error(),
@@ -164,6 +170,8 @@ func TestParamObjectFailure(t *testing.T) {
164170
a2 A
165171
}
166172

173+
_ = in{}.a2 // unused but needed
174+
167175
_, err := newParamObject(reflect.TypeOf(in{}), New())
168176
require.Error(t, err)
169177
assert.Contains(t, err.Error(),

tools/doc.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2021 Uber Technologies, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
// Package tools exists to make this directory a valid Go package.
22+
// The tools.go has a build tag that excludes it from being considered by Go
23+
// tooling except for dependency constraints.
24+
package tools

tools/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/uber-go/dig/tools
2+
3+
go 1.16
4+
5+
require (
6+
github.com/BurntSushi/toml v0.4.1 // indirect
7+
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
8+
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
9+
golang.org/x/tools v0.1.8 // indirect
10+
honnef.co/go/tools v0.2.2
11+
)

0 commit comments

Comments
 (0)