Skip to content

Commit 9f0a33a

Browse files
author
Denis Krivak
committed
Add more checks to avoid panics.
1 parent b60172c commit 9f0a33a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

checks.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func checkCommentForPeriod(c comment) *Issue {
8080
// attached lines. Use `iss.Pos.Column` because it's a position in
8181
// the original line.
8282
original := c.lines[pos.line-1]
83+
if len(original) < iss.Pos.Column-1 {
84+
// This should never happen. Avoid panics, skip this check.
85+
return nil
86+
}
8387
iss.Replacement = original[:iss.Pos.Column-1] + "." +
8488
original[iss.Pos.Column-1:]
8589

@@ -117,14 +121,18 @@ func checkCommentForCapital(c comment) []Issue {
117121
Message: noCapitalMessage,
118122
}
119123

120-
// Make a replacement. Use `pos.line` to get an original line from
124+
// Make a replacement. Use `pos.original` to get an original original from
121125
// attached lines. Use `iss.Pos.Column` because it's a position in
122-
// the original line.
123-
line := c.lines[pos.line-1]
124-
col := byteToRuneColumn(line, iss.Pos.Column) - 1
125-
rep := string(unicode.ToTitle([]rune(line)[col])) // capital letter
126-
iss.Replacement = line[:iss.Pos.Column-1] + rep +
127-
line[iss.Pos.Column-1+len(rep):]
126+
// the original original.
127+
original := c.lines[pos.line-1]
128+
col := byteToRuneColumn(original, iss.Pos.Column) - 1
129+
rep := string(unicode.ToTitle([]rune(original)[col])) // capital letter
130+
if len(original) < iss.Pos.Column-1+len(rep) {
131+
// This should never happen. Avoid panics, skip this check.
132+
continue
133+
}
134+
iss.Replacement = original[:iss.Pos.Column-1] + rep +
135+
original[iss.Pos.Column-1+len(rep):]
128136

129137
// Save replacement to raw lines to be able to combine it with
130138
// further replacements

0 commit comments

Comments
 (0)