stringer: don't emit unnecessary variables

Fixes golang/go#23014

Change-Id: I159f83bae0ed632b0b3c00f8ab02f5701acbc4cc
Reviewed-on: https://go-review.googlesource.com/82215
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
diff --git a/cmd/stringer/golden_test.go b/cmd/stringer/golden_test.go
index 2c418d2..12bd538 100644
--- a/cmd/stringer/golden_test.go
+++ b/cmd/stringer/golden_test.go
@@ -110,7 +110,6 @@
 var (
 	_Gap_index_0 = [...]uint8{0, 3, 8}
 	_Gap_index_1 = [...]uint8{0, 4, 7, 12, 17, 21}
-	_Gap_index_2 = [...]uint8{0, 6}
 )
 
 func (i Gap) String() string {
diff --git a/cmd/stringer/stringer.go b/cmd/stringer/stringer.go
index 50dec93..90c81bf 100644
--- a/cmd/stringer/stringer.go
+++ b/cmd/stringer/stringer.go
@@ -487,7 +487,9 @@
 	var indexes, names []string
 	for i, run := range runs {
 		index, name := g.createIndexAndNameDecl(run, typeName, fmt.Sprintf("_%d", i))
-		indexes = append(indexes, index)
+		if len(run) != 1 {
+			indexes = append(indexes, index)
+		}
 		names = append(names, name)
 	}
 	g.Printf("const (\n")
@@ -495,11 +497,14 @@
 		g.Printf("\t%s\n", name)
 	}
 	g.Printf(")\n\n")
-	g.Printf("var (")
-	for _, index := range indexes {
-		g.Printf("\t%s\n", index)
+
+	if len(indexes) > 0 {
+		g.Printf("var (")
+		for _, index := range indexes {
+			g.Printf("\t%s\n", index)
+		}
+		g.Printf(")\n\n")
 	}
-	g.Printf(")\n\n")
 }
 
 // declareIndexAndNameVar is the single-run version of declareIndexAndNameVars