Skip to content

Commit 3c0cfb8

Browse files
abhinavprashantv
andauthored
cmd/errtrace: Tighten up opt-out matching (#64)
strings.Contains is too wide for this purpose. It trips up even if a doc comment mentions '//errtrace:skip'. Narrow it down to a much more selective criteria: - must be a single line comment - must be at start of the comment (`foo() //errtrace:skip`) or have a space immediately before it (`foo() //nolint:bar //errtrace:skip`) - must reach end of the comment (`foo() //errtrace:skip`) or have a space (`foo() //errtrace:skip //nolint:bar`) or an opening parenthesis (`foo() //errtrace:skip(reason)`) immediately after it. --------- Co-authored-by: Prashant V <[email protected]>
1 parent 5f36dcb commit 3c0cfb8

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ on relevant lines:
352352

353353
```go
354354
//errtrace:skip
355-
//errtrace:skip explanation
355+
//errtrace:skip(explanation)
356+
//errtrace:skip // explanation
356357
```
357358

358359
This can be especially useful if the returned error
@@ -367,7 +368,7 @@ type myReader struct{/* ... */}
367368

368369
func (*myReader) Read(bs []byte) (int, error) {
369370
// ...
370-
return 0, io.EOF //errtrace:skip (io.Reader requires io.EOF)
371+
return 0, io.EOF //errtrace:skip(io.Reader expects io.EOF)
371372
}
372373
```
373374

cmd/errtrace/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"io"
3535
"log"
3636
"os"
37+
"regexp"
3738
"sort"
3839
"strconv"
3940
"strings"
@@ -866,6 +867,8 @@ func setOf[T comparable](xs []T) map[T]struct{} {
866867
return set
867868
}
868869

870+
var _errtraceSkip = regexp.MustCompile(`(^| )//errtrace:skip($|[ \(])`)
871+
869872
// optoutLines returns the line numbers
870873
// that have a comment in the form:
871874
//
@@ -880,11 +883,15 @@ func optoutLines(
880883
) map[int]int {
881884
lines := make(map[int]int)
882885
for _, cg := range comments {
883-
for _, c := range cg.List {
884-
if strings.Contains(c.Text, "//errtrace:skip") {
885-
lineNo := fset.Position(c.Pos()).Line
886-
lines[lineNo] = 0
887-
}
886+
if len(cg.List) > 1 {
887+
// skip multiline comments which are full line comments, not tied to a return.
888+
continue
889+
}
890+
891+
c := cg.List[0]
892+
if _errtraceSkip.MatchString(c.Text) {
893+
lineNo := fset.Position(c.Pos()).Line
894+
lines[lineNo] = 0
888895
}
889896
}
890897
return lines

cmd/errtrace/testdata/golden/optout.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Try(problem bool) (int, error) {
1515
return errors.New("great sadness")
1616
}
1717

18-
return io.EOF //errtrace:skip // expects io.EOF
18+
return io.EOF //nolint:errwrap //errtrace:skip(expects io.EOF)
1919
})
2020
if err != nil {
2121
return 0, err
@@ -32,3 +32,15 @@ func multipleReturns() (a, b error) {
3232
return errors.New("a"),
3333
errors.New("b") //errtrace:skip
3434
}
35+
36+
func multipleReturnsSkipped() (a, b error) {
37+
return errors.New("a"), //errtrace:skip
38+
errors.New("b") //errtrace:skip
39+
}
40+
41+
// Explanation of why this function
42+
// is not using //errtrace:skip should not
43+
// trip up the warning.
44+
func notUsingSkip() error {
45+
return nil
46+
}

cmd/errtrace/testdata/golden/optout.go.golden

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Try(problem bool) (int, error) {
1515
return errtrace.Wrap(errors.New("great sadness"))
1616
}
1717

18-
return io.EOF //errtrace:skip // expects io.EOF
18+
return io.EOF //nolint:errwrap //errtrace:skip(expects io.EOF)
1919
})
2020
if err != nil {
2121
return 0, errtrace.Wrap(err)
@@ -32,3 +32,15 @@ func multipleReturns() (a, b error) {
3232
return errtrace.Wrap(errors.New("a")),
3333
errors.New("b") //errtrace:skip
3434
}
35+
36+
func multipleReturnsSkipped() (a, b error) {
37+
return errors.New("a"), //errtrace:skip
38+
errors.New("b") //errtrace:skip
39+
}
40+
41+
// Explanation of why this function
42+
// is not using //errtrace:skip should not
43+
// trip up the warning.
44+
func notUsingSkip() error {
45+
return nil
46+
}

0 commit comments

Comments
 (0)