Skip to content

Commit

Permalink
go/analysis/passes/lostcancel: add WithCancelCause et al
Browse files Browse the repository at this point in the history
The *Cause variants behave in a similar manner.

Updates golang/go#70185

Change-Id: Ia7eea7a5be8878930505b63fa8222060fef47079
Reviewed-on: https://go-review.googlesource.com/c/tools/+/625115
Reviewed-by: Robert Findley <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Alan Donovan <[email protected]>
  • Loading branch information
adonovan authored and gopherbot committed Nov 4, 2024
1 parent f0379e0 commit 2998e9a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions go/analysis/passes/lostcancel/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// lostcancel: check cancel func returned by context.WithCancel is called
//
// The cancellation function returned by context.WithCancel, WithTimeout,
// and WithDeadline must be called or the new context will remain live
// until its parent context is cancelled.
// WithDeadline and variants such as WithCancelCause must be called,
// or the new context will remain live until its parent context is cancelled.
// (The background context is never cancelled.)
package lostcancel
4 changes: 3 additions & 1 deletion go/analysis/passes/lostcancel/lostcancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ func isContextWithCancel(info *types.Info, n ast.Node) bool {
return false
}
switch sel.Sel.Name {
case "WithCancel", "WithTimeout", "WithDeadline":
case "WithCancel", "WithCancelCause",
"WithTimeout", "WithTimeoutCause",
"WithDeadline", "WithDeadlineCause":
default:
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ package typeparams

import (
"context"
"io"
"time"
)

//
// These comment lines are ballast to ensure
// that this is L17. Add/remove as needed.

var bg = context.Background()

func _[T any]() {
var _, cancel = context.WithCancel(bg) // want `the cancel function is not used on all paths \(possible context leak\)`
if false {
_ = cancel
}
} // want "this return statement may be reached without using the cancel var defined on line 17"
} // want "this return statement may be reached without using the cancel var defined on line 22"

func _[T any]() {
_, cancel := context.WithCancel(bg)
Expand Down Expand Up @@ -55,3 +60,16 @@ func _() {
var x C[int]
x.f()
}

func withCancelCause(maybe bool) {
{
_, cancel := context.WithCancelCause(bg)
defer cancel(io.EOF) // ok
}
{
_, cancel := context.WithCancelCause(bg) // want "the cancel function is not used on all paths \\(possible context leak\\)"
if maybe {
cancel(io.EOF)
}
}
} // want "this return statement may be reached without using the cancel var defined on line 70"
4 changes: 2 additions & 2 deletions gopls/doc/analyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ Package documentation: [loopclosure](https://pkg.go.dev/golang.org/x/tools/go/an


The cancellation function returned by context.WithCancel, WithTimeout,
and WithDeadline must be called or the new context will remain live
until its parent context is cancelled.
WithDeadline and variants such as WithCancelCause must be called,
or the new context will remain live until its parent context is cancelled.
(The background context is never cancelled.)

Default: on.
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/doc/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@
},
{
"Name": "\"lostcancel\"",
"Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nand WithDeadline must be called or the new context will remain live\nuntil its parent context is cancelled.\n(The background context is never cancelled.)",
"Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nWithDeadline and variants such as WithCancelCause must be called,\nor the new context will remain live until its parent context is cancelled.\n(The background context is never cancelled.)",
"Default": "true"
},
{
Expand Down Expand Up @@ -1114,7 +1114,7 @@
},
{
"Name": "lostcancel",
"Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nand WithDeadline must be called or the new context will remain live\nuntil its parent context is cancelled.\n(The background context is never cancelled.)",
"Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nWithDeadline and variants such as WithCancelCause must be called,\nor the new context will remain live until its parent context is cancelled.\n(The background context is never cancelled.)",
"URL": "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/lostcancel",
"Default": true
},
Expand Down

0 comments on commit 2998e9a

Please sign in to comment.