@@ -266,6 +266,21 @@ func (cmd *mainCmd) processFile(r fileRequest) error {
266266 }
267267 ast .Walk (& w , f )
268268
269+ // Look for unused optouts and warn about them.
270+ if len (w .optouts ) > 0 {
271+ unusedOptouts := make ([]int , 0 , len (w .optouts ))
272+ for line , used := range w .optouts {
273+ if used == 0 {
274+ unusedOptouts = append (unusedOptouts , line )
275+ }
276+ }
277+ sort .Ints (unusedOptouts )
278+
279+ for _ , line := range unusedOptouts {
280+ cmd .log .Printf ("%s:%d:unused errtrace:skip" , r .Filename , line )
281+ }
282+ }
283+
269284 if r .List {
270285 if len (inserts ) > 0 {
271286 _ , err = fmt .Fprintf (cmd .Stdout , "%s\n " , r .Filename )
@@ -420,10 +435,11 @@ type walker struct {
420435 // Inputs
421436
422437 fset * token.FileSet // file set for positional information
423- optouts map [int ]struct {}
424- errtrace string // name of the errtrace package
438+ errtrace string // name of the errtrace package
425439 logger * log.Logger
426440
441+ optouts map [int ]int // map from line to number of uses
442+
427443 // Outputs
428444
429445 // inserts is the list of inserts to make.
@@ -694,14 +710,15 @@ func (t *walker) wrapExpr(n int, expr ast.Expr) {
694710 case t .isErrtraceWrap (expr ):
695711 return // already wrapped
696712
697- case t .optout (expr .Pos ()):
698- return
699-
700713 case isIdent (expr , "nil" ):
701714 // Optimization: ignore if it's "nil".
702715 return
703716 }
704717
718+ if t .optout (expr .Pos ()) {
719+ return
720+ }
721+
705722 * t .inserts = append (* t .inserts ,
706723 & insertWrapOpen {N : n , Before : expr .Pos ()},
707724 & insertWrapClose {After : expr .End ()},
@@ -731,10 +748,13 @@ func (t *walker) isErrtraceWrap(expr ast.Expr) bool {
731748}
732749
733750// optout reports whether the line at the given position
734- // is opted out of tracing.
751+ // is opted out of tracing, incrementing uses if so .
735752func (t * walker ) optout (pos token.Pos ) bool {
736753 line := t .fset .Position (pos ).Line
737754 _ , ok := t .optouts [line ]
755+ if ok {
756+ t .optouts [line ]++
757+ }
738758 return ok
739759}
740760
@@ -857,13 +877,13 @@ func setOf[T comparable](xs []T) map[T]struct{} {
857877func optoutLines (
858878 fset * token.FileSet ,
859879 comments []* ast.CommentGroup ,
860- ) map [int ]struct {} {
861- lines := make (map [int ]struct {} )
880+ ) map [int ]int {
881+ lines := make (map [int ]int )
862882 for _ , cg := range comments {
863883 for _ , c := range cg .List {
864884 if strings .Contains (c .Text , "//errtrace:skip" ) {
865885 lineNo := fset .Position (c .Pos ()).Line
866- lines [lineNo ] = struct {}{}
886+ lines [lineNo ] = 0
867887 }
868888 }
869889 }
0 commit comments