]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: lostcancel: suppress the check in the main.main function
authorAlan Donovan <adonovan@google.com>
Fri, 13 Apr 2018 18:27:33 +0000 (14:27 -0400)
committerAlan Donovan <adonovan@google.com>
Fri, 9 Nov 2018 16:11:42 +0000 (16:11 +0000)
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>
src/cmd/vet/lostcancel.go
src/cmd/vet/testdata/lostcancel.go

index ee0342035fe3729efb3774e0d51f1cea10cd0ef3..496e87a5e04990bec751c32260ec35698fd7d56d 100644 (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)
index b7549c00511127819183818ba3895b5c03536ba8..408bed51228760e30a65057ebfab336fcb248580 100644 (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