1
0
mirror of https://github.com/golang/go synced 2024-11-23 04:10:04 -07:00

cmd/vet: lostcancel: suppress the check in the main.main function

When main.main returns, the process exits, so there's no need to cancel contexts.

This change was initially reviewed as
https://go-review.googlesource.com/c/go/+/106915/4
but somehow I messed up and committed patchset 5, which was
effectively empty.

Change-Id: Ic4250eb6563af9bc734e429aafc7081ca7d0e012
Reviewed-on: https://go-review.googlesource.com/c/148758
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Alan Donovan 2018-04-13 14:27:33 -04:00
parent 7d6b5e340c
commit 5e17ce22ec
2 changed files with 12 additions and 1 deletions

View File

@ -104,6 +104,11 @@ func checkLostCancel(f *File, node ast.Node) {
var sig *types.Signature
switch node := node.(type) {
case *ast.FuncDecl:
if node.Name.Name == "main" && node.Recv == nil && f.file.Name.Name == "main" {
// Returning from main.main terminates the process,
// so there's no need to cancel contexts.
return
}
obj := f.pkg.defs[node.Name]
if obj == nil {
return // type error (e.g. duplicate function declaration)

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testdata
package main
import (
"context"
@ -33,6 +33,12 @@ func _() {
ctx, _ = context.WithDeadline() // ERROR "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak"
}
// Return from main is handled specially.
// Since the program exits, there's no need to call cancel.
func main() {
var ctx, cancel = context.WithCancel()
}
func _() {
ctx, cancel := context.WithCancel()
defer cancel() // ok