소스 검색

*: golint, go tool vet, unsued, gosimple, staticcheck (#113)

* `git ls-files -- '*.go' | xargs gofmt -s -w`

* driver: unexport InternalOptions

This was an exported method that returned an internal type, so it's
unlikely that anyone depends on its presence.

* internal/binutils: remove newline in error string

golint reported: error strings should not be capitalized or end with
punctuation or a newline

* internal/driver: s/buildId/buildID/ per golint

* internal/elfexec: remove punctuation in error string

golint reported: error strings should not be capitalized or end with
punctuation or a newline

* internal/graph: correct comment

Found with golint.

* internal/graph: add method comment

golint reported: exported method Edge.WeightValue should have comment
or be unexported

* internal/report: remove newline in error string

golint reported: error strings should not be capitalized or end with
punctuation or a newline

* *: remove unused code

Found with honnef.co/go/tools/cmd/unused:

	internal/binutils/disasm_test.go:137:8: const objdump is unused (U1000)
	internal/driver/driver_test.go:353:6: type testProfile is unused (U1000)
	internal/graph/graph.go:838:6: func countEdges is unused (U1000)
	profile/proto.go:148:6: func encodeStringOpt is unused (U1000)

* *: simply code

Found with honnef.co/go/tools/cmd/gosimple:

	internal/driver/commands.go:471:24: should omit comparison to bool constant, can be simplified to !b (S1002)
	internal/driver/driver.go:163:5: should omit comparison to bool constant, can be simplified to !trim (S1002)
	internal/driver/driver.go:168:5: should omit comparison to bool constant, can be simplified to !focus (S1002)
	internal/driver/driver.go:172:5: should omit comparison to bool constant, can be simplified to !tagfocus (S1002)
	internal/driver/driver.go:176:5: should omit comparison to bool constant, can be simplified to !hide (S1002)
	internal/graph/dotgraph.go:213:34: should use make(map[string][]*Tag) instead (S1019)
	internal/report/report.go:1061:3: should replace loop with label = append(label, rpt.options.ProfileLabels...) (S1011)
	profile/proto.go:157:5: should omit comparison to bool constant, can be simplified to !x (S1002)

* *: correct mistakes

Found with honnef.co/go/tools/cmd/staticcheck:

	driver/driver.go:280:20: infinite recursive call (SA5007)
	internal/driver/driver_focus.go:54:11: this value of err is never used (SA4006)
	profile/proto.go:74:32: x | 0 always equals x (SA4016)

* Add linters to .travis.yml
Tamir Duberstein 8 년 전
부모
커밋
ff2edbdec7

+ 13
- 0
.travis.yml 파일 보기

@@ -2,3 +2,16 @@ language: go
2 2
 go:
3 3
   - 1.7.x
4 4
   - 1.8.x
5
+
6
+before_install:
7
+  - go get -u github.com/golang/lint/golint honnef.co/go/tools/cmd/...
8
+
9
+script:
10
+  - gofmtdiff=$(gofmt -s -d .) && if [ -n "$gofmtdiff" ]; then printf 'gofmt -s found:\n%s\n' "$gofmtdiff" && exit 1; fi
11
+  - golintlint=$(golint ./...) && if [ -n "$golintlint" ]; then printf 'golint found:\n%s\n' "$golintlint" && exit 1; fi
12
+  - go tool vet -all .
13
+  - gosimple ./...
14
+  - staticcheck ./...
15
+  - unused ./...
16
+  - go test -v ./...
17
+  - go test -v -race ./...

+ 4
- 4
driver/driver.go 파일 보기

@@ -29,10 +29,10 @@ import (
29 29
 // manager. Then it generates a report formatted according to the
30 30
 // options selected through the flags package.
31 31
 func PProf(o *Options) error {
32
-	return internaldriver.PProf(o.InternalOptions())
32
+	return internaldriver.PProf(o.internalOptions())
33 33
 }
34 34
 
35
-func (o *Options) InternalOptions() *plugin.Options {
35
+func (o *Options) internalOptions() *plugin.Options {
36 36
 	var obj plugin.ObjTool
37 37
 	if o.Obj != nil {
38 38
 		obj = &internalObjTool{o.Obj}
@@ -273,9 +273,9 @@ type internalSymbolizer struct {
273 273
 }
274 274
 
275 275
 func (s *internalSymbolizer) Symbolize(mode string, srcs plugin.MappingSources, prof *profile.Profile) error {
276
-	isrcs := plugin.MappingSources{}
276
+	isrcs := MappingSources{}
277 277
 	for m, s := range srcs {
278 278
 		isrcs[m] = s
279 279
 	}
280
-	return s.Symbolize(mode, isrcs, prof)
280
+	return s.Symbolizer.Symbolize(mode, isrcs, prof)
281 281
 }

+ 1
- 3
internal/binutils/disasm_test.go 파일 보기

@@ -73,7 +73,7 @@ func TestFindSymbols(t *testing.T) {
73 73
 
74 74
 func checkSymbol(got []*plugin.Sym, want []plugin.Sym) error {
75 75
 	if len(got) != len(want) {
76
-		return fmt.Errorf("unexpected number of symbols %d (want %d)\n", len(got), len(want))
76
+		return fmt.Errorf("unexpected number of symbols %d (want %d)", len(got), len(want))
77 77
 	}
78 78
 
79 79
 	for i, g := range got {
@@ -134,8 +134,6 @@ func TestFunctionAssembly(t *testing.T) {
134 134
 		},
135 135
 	}
136 136
 
137
-	const objdump = "testdata/wrapper/objdump"
138
-
139 137
 	for _, tc := range testcases {
140 138
 		insts, err := disassemble([]byte(tc.asm))
141 139
 		if err != nil {

+ 1
- 1
internal/driver/commands.go 파일 보기

@@ -468,7 +468,7 @@ func (vars variables) set(name, value string) error {
468 468
 	case boolKind:
469 469
 		var b bool
470 470
 		if b, err = stringToBool(value); err == nil {
471
-			if v.group != "" && b == false {
471
+			if v.group != "" && !b {
472 472
 				err = fmt.Errorf("%q can only be set to true", name)
473 473
 			}
474 474
 		}

+ 4
- 4
internal/driver/driver.go 파일 보기

@@ -160,20 +160,20 @@ func applyCommandOverrides(cmd []string, v variables) variables {
160 160
 			v.set("nodecount", "80")
161 161
 		}
162 162
 	}
163
-	if trim == false {
163
+	if !trim {
164 164
 		v.set("nodecount", "0")
165 165
 		v.set("nodefraction", "0")
166 166
 		v.set("edgefraction", "0")
167 167
 	}
168
-	if focus == false {
168
+	if !focus {
169 169
 		v.set("focus", "")
170 170
 		v.set("ignore", "")
171 171
 	}
172
-	if tagfocus == false {
172
+	if !tagfocus {
173 173
 		v.set("tagfocus", "")
174 174
 		v.set("tagignore", "")
175 175
 	}
176
-	if hide == false {
176
+	if !hide {
177 177
 		v.set("hide", "")
178 178
 		v.set("show", "")
179 179
 	}

+ 1
- 1
internal/driver/driver_focus.go 파일 보기

@@ -59,7 +59,7 @@ func applyFocus(prof *profile.Profile, v variables, ui plugin.UI) error {
59 59
 	if prunefrom != nil {
60 60
 		prof.PruneFrom(prunefrom)
61 61
 	}
62
-	return nil
62
+	return err
63 63
 }
64 64
 
65 65
 func compileRegexOption(name, value string, err error) (*regexp.Regexp, error) {

+ 14
- 17
internal/driver/driver_test.go 파일 보기

@@ -350,9 +350,6 @@ func baseFlags() testFlags {
350 350
 	}
351 351
 }
352 352
 
353
-type testProfile struct {
354
-}
355
-
356 353
 const testStart = 0x1000
357 354
 const testOffset = 0x5000
358 355
 
@@ -548,32 +545,32 @@ func cpuProfile() *profile.Profile {
548 545
 				Location: []*profile.Location{cpuL[0], cpuL[1], cpuL[2]},
549 546
 				Value:    []int64{1000, 1000},
550 547
 				Label: map[string][]string{
551
-					"key1": []string{"tag1"},
552
-					"key2": []string{"tag1"},
548
+					"key1": {"tag1"},
549
+					"key2": {"tag1"},
553 550
 				},
554 551
 			},
555 552
 			{
556 553
 				Location: []*profile.Location{cpuL[0], cpuL[3]},
557 554
 				Value:    []int64{100, 100},
558 555
 				Label: map[string][]string{
559
-					"key1": []string{"tag2"},
560
-					"key3": []string{"tag2"},
556
+					"key1": {"tag2"},
557
+					"key3": {"tag2"},
561 558
 				},
562 559
 			},
563 560
 			{
564 561
 				Location: []*profile.Location{cpuL[1], cpuL[4]},
565 562
 				Value:    []int64{10, 10},
566 563
 				Label: map[string][]string{
567
-					"key1": []string{"tag3"},
568
-					"key2": []string{"tag2"},
564
+					"key1": {"tag3"},
565
+					"key2": {"tag2"},
569 566
 				},
570 567
 			},
571 568
 			{
572 569
 				Location: []*profile.Location{cpuL[2]},
573 570
 				Value:    []int64{10, 10},
574 571
 				Label: map[string][]string{
575
-					"key1": []string{"tag4"},
576
-					"key2": []string{"tag1"},
572
+					"key1": {"tag4"},
573
+					"key2": {"tag1"},
577 574
 				},
578 575
 			},
579 576
 		},
@@ -750,28 +747,28 @@ func heapProfile() *profile.Profile {
750 747
 				Location: []*profile.Location{heapL[0], heapL[1], heapL[2]},
751 748
 				Value:    []int64{10, 1024000},
752 749
 				NumLabel: map[string][]int64{
753
-					"bytes": []int64{102400},
750
+					"bytes": {102400},
754 751
 				},
755 752
 			},
756 753
 			{
757 754
 				Location: []*profile.Location{heapL[0], heapL[3]},
758 755
 				Value:    []int64{20, 4096000},
759 756
 				NumLabel: map[string][]int64{
760
-					"bytes": []int64{204800},
757
+					"bytes": {204800},
761 758
 				},
762 759
 			},
763 760
 			{
764 761
 				Location: []*profile.Location{heapL[1], heapL[4]},
765 762
 				Value:    []int64{40, 65536000},
766 763
 				NumLabel: map[string][]int64{
767
-					"bytes": []int64{1638400},
764
+					"bytes": {1638400},
768 765
 				},
769 766
 			},
770 767
 			{
771 768
 				Location: []*profile.Location{heapL[2]},
772 769
 				Value:    []int64{80, 32768000},
773 770
 				NumLabel: map[string][]int64{
774
-					"bytes": []int64{409600},
771
+					"bytes": {409600},
775 772
 				},
776 773
 			},
777 774
 		},
@@ -1051,7 +1048,7 @@ func (m *mockObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, err
1051 1048
 }
1052 1049
 
1053 1050
 type mockFile struct {
1054
-	name, buildId string
1051
+	name, buildID string
1055 1052
 	base          uint64
1056 1053
 }
1057 1054
 
@@ -1067,7 +1064,7 @@ func (m *mockFile) Base() uint64 {
1067 1064
 
1068 1065
 // BuildID returns the GNU build ID of the file, or an empty string.
1069 1066
 func (m *mockFile) BuildID() string {
1070
-	return m.buildId
1067
+	return m.buildID
1071 1068
 }
1072 1069
 
1073 1070
 // SourceLine reports the source line information for a given

+ 1
- 1
internal/elfexec/elfexec.go 파일 보기

@@ -131,7 +131,7 @@ func GetBuildID(binary io.ReaderAt) ([]byte, error) {
131 131
 				if buildID == nil {
132 132
 					buildID = note.Desc
133 133
 				} else {
134
-					return nil, fmt.Errorf("multiple build ids found, don't know which to use!")
134
+					return nil, fmt.Errorf("multiple build ids found, don't know which to use")
135 135
 				}
136 136
 			}
137 137
 		}

+ 2
- 2
internal/graph/dotgraph.go 파일 보기

@@ -50,7 +50,7 @@ type DotConfig struct {
50 50
 	Total       int64                      // The total weight of the graph, used to compute percentages
51 51
 }
52 52
 
53
-// Compose creates and writes a in the DOT format to the writer, using
53
+// ComposeDot creates and writes a in the DOT format to the writer, using
54 54
 // the configurations given.
55 55
 func ComposeDot(w io.Writer, g *Graph, a *DotAttributes, c *DotConfig) {
56 56
 	builder := &builder{w, a, c}
@@ -210,7 +210,7 @@ func (b *builder) addNodelets(node *Node, nodeID int) bool {
210 210
 
211 211
 	// Populate two Tag slices, one for LabelTags and one for NumericTags.
212 212
 	var ts []*Tag
213
-	lnts := make(map[string][]*Tag, 0)
213
+	lnts := make(map[string][]*Tag)
214 214
 	for _, t := range node.LabelTags {
215 215
 		ts = append(ts, t)
216 216
 	}

+ 4
- 4
internal/graph/dotgraph_test.go 파일 보기

@@ -240,19 +240,19 @@ func TestTagCollapse(t *testing.T) {
240 240
 	}
241 241
 
242 242
 	tagWant := [][]*Tag{
243
-		[]*Tag{
243
+		{
244 244
 			makeTag("1B..2GB", "", 0, 2401, 2401),
245 245
 		},
246
-		[]*Tag{
246
+		{
247 247
 			makeTag("2GB", "", 0, 1000, 1000),
248 248
 			makeTag("1B..12MB", "", 0, 1401, 1401),
249 249
 		},
250
-		[]*Tag{
250
+		{
251 251
 			makeTag("2GB", "", 0, 1000, 1000),
252 252
 			makeTag("12MB", "", 0, 100, 100),
253 253
 			makeTag("1B..1MB", "", 0, 1301, 1301),
254 254
 		},
255
-		[]*Tag{
255
+		{
256 256
 			makeTag("2GB", "", 0, 1000, 1000),
257 257
 			makeTag("1MB", "", 0, 1000, 1000),
258 258
 			makeTag("2B..1kB", "", 0, 201, 201),

+ 2
- 11
internal/graph/graph.go 파일 보기

@@ -240,6 +240,8 @@ type Edge struct {
240 240
 	Inline bool
241 241
 }
242 242
 
243
+// WeightValue returns the weight value for this edge, normalizing if a
244
+// divisor is available.
243 245
 func (e *Edge) WeightValue() int64 {
244 246
 	if e.WeightDiv == 0 {
245 247
 		return e.Weight
@@ -832,17 +834,6 @@ func countTags(n *Node) int {
832 834
 	return count
833 835
 }
834 836
 
835
-// countEdges counts the number of edges below the specified cutoff.
836
-func countEdges(el EdgeMap, cutoff int64) int {
837
-	count := 0
838
-	for _, e := range el {
839
-		if e.Weight > cutoff {
840
-			count++
841
-		}
842
-	}
843
-	return count
844
-}
845
-
846 837
 // RemoveRedundantEdges removes residual edges if the destination can
847 838
 // be reached through another path. This is done to simplify the graph
848 839
 // while preserving connectivity.

+ 1
- 3
internal/report/report.go 파일 보기

@@ -1058,9 +1058,7 @@ func reportLabels(rpt *Report, g *graph.Graph, origCount, droppedNodes, droppedE
1058 1058
 
1059 1059
 	var label []string
1060 1060
 	if len(rpt.options.ProfileLabels) > 0 {
1061
-		for _, l := range rpt.options.ProfileLabels {
1062
-			label = append(label, l)
1063
-		}
1061
+		label = append(label, rpt.options.ProfileLabels...)
1064 1062
 	} else if fullHeaders || !rpt.options.CompactLabels {
1065 1063
 		label = ProfileLabels(rpt)
1066 1064
 	}

+ 1
- 1
internal/report/source.go 파일 보기

@@ -167,7 +167,7 @@ func printWebSource(w io.Writer, rpt *Report, obj plugin.ObjTool) error {
167 167
 	}
168 168
 
169 169
 	if len(fileNodes) == 0 {
170
-		return fmt.Errorf("No source information for %s\n", o.Symbol.String())
170
+		return fmt.Errorf("No source information for %s", o.Symbol.String())
171 171
 	}
172 172
 
173 173
 	sourceFiles := make(graph.Nodes, 0, len(fileNodes))

+ 5
- 5
internal/symbolizer/symbolizer_test.go 파일 보기

@@ -207,11 +207,11 @@ func checkSymbolizedLocation(a uint64, got []profile.Line) error {
207 207
 }
208 208
 
209 209
 var mockAddresses = map[uint64][]plugin.Frame{
210
-	1000: []plugin.Frame{frame("fun11", "file11.src", 10)},
211
-	2000: []plugin.Frame{frame("fun21", "file21.src", 20), frame("fun22", "file22.src", 20)},
212
-	3000: []plugin.Frame{frame("fun31", "file31.src", 30), frame("fun32", "file32.src", 30), frame("fun33", "file33.src", 30)},
213
-	4000: []plugin.Frame{frame("fun41", "file41.src", 40), frame("fun42", "file42.src", 40), frame("fun43", "file43.src", 40), frame("fun44", "file44.src", 40)},
214
-	5000: []plugin.Frame{frame("fun51", "file51.src", 50), frame("fun52", "file52.src", 50), frame("fun53", "file53.src", 50), frame("fun54", "file54.src", 50), frame("fun55", "file55.src", 50)},
210
+	1000: {frame("fun11", "file11.src", 10)},
211
+	2000: {frame("fun21", "file21.src", 20), frame("fun22", "file22.src", 20)},
212
+	3000: {frame("fun31", "file31.src", 30), frame("fun32", "file32.src", 30), frame("fun33", "file33.src", 30)},
213
+	4000: {frame("fun41", "file41.src", 40), frame("fun42", "file42.src", 40), frame("fun43", "file43.src", 40), frame("fun44", "file44.src", 40)},
214
+	5000: {frame("fun51", "file51.src", 50), frame("fun52", "file52.src", 50), frame("fun53", "file53.src", 50), frame("fun54", "file54.src", 50), frame("fun55", "file55.src", 50)},
215 215
 }
216 216
 
217 217
 func frame(fname, file string, line int) plugin.Frame {

+ 1
- 1
profile/legacy_java_profile.go 파일 보기

@@ -212,7 +212,7 @@ func parseJavaSamples(pType string, b []byte, p *Profile) ([]byte, map[uint64]*L
212 212
 			switch pType {
213 213
 			case "heap":
214 214
 				const javaHeapzSamplingRate = 524288 // 512K
215
-				s.NumLabel = map[string][]int64{"bytes": []int64{s.Value[1] / s.Value[0]}}
215
+				s.NumLabel = map[string][]int64{"bytes": {s.Value[1] / s.Value[0]}}
216 216
 				s.Value[0], s.Value[1] = scaleHeapSample(s.Value[0], s.Value[1], javaHeapzSamplingRate)
217 217
 			case "contention":
218 218
 				if period := p.Period; period != 0 {

+ 14
- 14
profile/profile_test.go 파일 보기

@@ -230,40 +230,40 @@ var testProfile = &Profile{
230 230
 			Location: []*Location{cpuL[0]},
231 231
 			Value:    []int64{1000, 1000},
232 232
 			Label: map[string][]string{
233
-				"key1": []string{"tag1"},
234
-				"key2": []string{"tag1"},
233
+				"key1": {"tag1"},
234
+				"key2": {"tag1"},
235 235
 			},
236 236
 		},
237 237
 		{
238 238
 			Location: []*Location{cpuL[1], cpuL[0]},
239 239
 			Value:    []int64{100, 100},
240 240
 			Label: map[string][]string{
241
-				"key1": []string{"tag2"},
242
-				"key3": []string{"tag2"},
241
+				"key1": {"tag2"},
242
+				"key3": {"tag2"},
243 243
 			},
244 244
 		},
245 245
 		{
246 246
 			Location: []*Location{cpuL[2], cpuL[0]},
247 247
 			Value:    []int64{10, 10},
248 248
 			Label: map[string][]string{
249
-				"key1": []string{"tag3"},
250
-				"key2": []string{"tag2"},
249
+				"key1": {"tag3"},
250
+				"key2": {"tag2"},
251 251
 			},
252 252
 		},
253 253
 		{
254 254
 			Location: []*Location{cpuL[3], cpuL[0]},
255 255
 			Value:    []int64{10000, 10000},
256 256
 			Label: map[string][]string{
257
-				"key1": []string{"tag4"},
258
-				"key2": []string{"tag1"},
257
+				"key1": {"tag4"},
258
+				"key2": {"tag1"},
259 259
 			},
260 260
 		},
261 261
 		{
262 262
 			Location: []*Location{cpuL[4], cpuL[0]},
263 263
 			Value:    []int64{1, 1},
264 264
 			Label: map[string][]string{
265
-				"key1": []string{"tag4"},
266
-				"key2": []string{"tag1"},
265
+				"key1": {"tag4"},
266
+				"key2": {"tag1"},
267 267
 			},
268 268
 		},
269 269
 	},
@@ -273,10 +273,10 @@ var testProfile = &Profile{
273 273
 }
274 274
 
275 275
 var aggTests = map[string]aggTest{
276
-	"precise":         aggTest{true, true, true, true, 5},
277
-	"fileline":        aggTest{false, true, true, true, 4},
278
-	"inline_function": aggTest{false, true, false, true, 3},
279
-	"function":        aggTest{false, true, false, false, 2},
276
+	"precise":         {true, true, true, true, 5},
277
+	"fileline":        {false, true, true, true, 4},
278
+	"inline_function": {false, true, false, true, 3},
279
+	"function":        {false, true, false, false, 2},
280 280
 }
281 281
 
282 282
 type aggTest struct {

+ 3
- 11
profile/proto.go 파일 보기

@@ -71,7 +71,7 @@ func encodeLength(b *buffer, tag int, len int) {
71 71
 
72 72
 func encodeUint64(b *buffer, tag int, x uint64) {
73 73
 	// append varint to b.data
74
-	encodeVarint(b, uint64(tag)<<3|0)
74
+	encodeVarint(b, uint64(tag)<<3)
75 75
 	encodeVarint(b, x)
76 76
 }
77 77
 
@@ -145,13 +145,6 @@ func encodeStrings(b *buffer, tag int, x []string) {
145 145
 	}
146 146
 }
147 147
 
148
-func encodeStringOpt(b *buffer, tag int, x string) {
149
-	if x == "" {
150
-		return
151
-	}
152
-	encodeString(b, tag, x)
153
-}
154
-
155 148
 func encodeBool(b *buffer, tag int, x bool) {
156 149
 	if x {
157 150
 		encodeUint64(b, tag, 1)
@@ -161,10 +154,9 @@ func encodeBool(b *buffer, tag int, x bool) {
161 154
 }
162 155
 
163 156
 func encodeBoolOpt(b *buffer, tag int, x bool) {
164
-	if x == false {
165
-		return
157
+	if x {
158
+		encodeBool(b, tag, x)
166 159
 	}
167
-	encodeBool(b, tag, x)
168 160
 }
169 161
 
170 162
 func encodeMessage(b *buffer, tag int, m message) {

+ 6
- 6
profile/proto_test.go 파일 보기

@@ -100,8 +100,8 @@ var all = &Profile{
100 100
 		{
101 101
 			Location: []*Location{testL[0], testL[1], testL[2], testL[1], testL[1]},
102 102
 			Label: map[string][]string{
103
-				"key1": []string{"value1"},
104
-				"key2": []string{"value2"},
103
+				"key1": {"value1"},
104
+				"key2": {"value2"},
105 105
 			},
106 106
 			Value: []int64{10, 20},
107 107
 		},
@@ -109,12 +109,12 @@ var all = &Profile{
109 109
 			Location: []*Location{testL[1], testL[2], testL[0], testL[1]},
110 110
 			Value:    []int64{30, 40},
111 111
 			Label: map[string][]string{
112
-				"key1": []string{"value1"},
113
-				"key2": []string{"value2"},
112
+				"key1": {"value1"},
113
+				"key2": {"value2"},
114 114
 			},
115 115
 			NumLabel: map[string][]int64{
116
-				"key1": []int64{1, 2},
117
-				"key2": []int64{3, 4},
116
+				"key1": {1, 2},
117
+				"key2": {3, 4},
118 118
 			},
119 119
 		},
120 120
 	},