modfile: handle indirect dependencies with an empty comment

This was reported in golang/go#35737.

Change-Id: I67383fbf668adea1c22feb7f8d25cd1652edcea5
Reviewed-on: https://go-review.googlesource.com/c/mod/+/208273
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
diff --git a/modfile/rule.go b/modfile/rule.go
index 292d5b6..256ef60 100644
--- a/modfile/rule.go
+++ b/modfile/rule.go
@@ -332,7 +332,7 @@
 		// Insert at beginning of existing comment.
 		com := &line.Suffix[0]
 		space := " "
-		if len(com.Token) > 2 && com.Token[2] == ' ' || com.Token[2] == '\t' {
+		if len(com.Token) > 2 && (com.Token[2] == ' ' || com.Token[2] == '\t') {
 			space = ""
 		}
 		com.Token = "// indirect;" + space + com.Token[2:]
diff --git a/modfile/rule_test.go b/modfile/rule_test.go
index 455e0de..282d290 100644
--- a/modfile/rule_test.go
+++ b/modfile/rule_test.go
@@ -64,8 +64,9 @@
 var setRequireTests = []struct {
 	in   string
 	mods []struct {
-		path string
-		vers string
+		path     string
+		vers     string
+		indirect bool
 	}
 	out string
 }{
@@ -79,12 +80,13 @@
 		)
 		`,
 		[]struct {
-			path string
-			vers string
+			path     string
+			vers     string
+			indirect bool
 		}{
-			{"x.y/a", "v1.2.3"},
-			{"x.y/b", "v1.2.3"},
-			{"x.y/c", "v1.2.3"},
+			{"x.y/a", "v1.2.3", false},
+			{"x.y/b", "v1.2.3", false},
+			{"x.y/c", "v1.2.3", false},
 		},
 		`module m
 		require (
@@ -94,6 +96,31 @@
 		)
 		`,
 	},
+	{
+		`module m
+		require (
+			x.y/b v1.2.3 //
+			x.y/a v1.2.3
+			x.y/d v1.2.3
+		)
+		`,
+		[]struct {
+			path     string
+			vers     string
+			indirect bool
+		}{
+			{"x.y/a", "v1.2.3", false},
+			{"x.y/b", "v1.2.3", true},
+			{"x.y/c", "v1.2.3", false},
+		},
+		`module m
+		require (
+			x.y/a v1.2.3
+			x.y/b v1.2.3 // indirect;
+			x.y/c v1.2.3
+		)
+		`,
+	},
 }
 
 var addGoTests = []struct {
@@ -189,6 +216,7 @@
 						Path:    mod.path,
 						Version: mod.vers,
 					},
+					Indirect: mod.indirect,
 				})
 			}