@@ -6,12 +6,12 @@ import (
66)
77
88func closeFileDeferred (f * os.File ) {
9- defer f .Close () // NOT OK, if `f` is writable
9+ defer f .Close () // $ Alert=w Alert=rw
1010}
1111
1212func closeFileDeferredIndirect (f * os.File ) {
1313 var cont = func () {
14- f .Close () // NOT OK, if `f` is writable
14+ f .Close () // $ Alert=w Alert=rw
1515 }
1616
1717 defer cont ()
@@ -28,7 +28,7 @@ func closeFileDeferredIndirectReturn(f *os.File) {
2828
2929func deferredCalls () {
3030 // open file for writing
31- if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
31+ if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source=w
3232 closeFileDeferred (f ) // NOT OK
3333 closeFileDeferredIndirect (f ) // NOT OK
3434 closeFileDeferredIndirectReturn (f ) // OK - the error is not discarded at the call to Close (though it is discarded later)
@@ -42,7 +42,7 @@ func deferredCalls() {
4242 }
4343
4444 // open file for reading and writing
45- if f , err := os .OpenFile ("foo.txt" , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
45+ if f , err := os .OpenFile ("foo.txt" , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source=rw
4646 closeFileDeferred (f ) // NOT OK
4747 closeFileDeferredIndirect (f ) // NOT OK
4848 closeFileDeferredIndirectReturn (f ) // OK - the error is not discarded at the call to Close (though it is discarded later)
@@ -51,9 +51,9 @@ func deferredCalls() {
5151
5252func notDeferred () {
5353 // open file for writing
54- if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
54+ if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source
5555 // the handle is write-only and we don't check if `Close` succeeds
56- f .Close () // NOT OK
56+ f .Close () // $ Alert
5757 }
5858
5959 // open file for reading
@@ -63,9 +63,9 @@ func notDeferred() {
6363 }
6464
6565 // open file for reading and writing
66- if f , err := os .OpenFile ("foo.txt" , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
66+ if f , err := os .OpenFile ("foo.txt" , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source
6767 // the handle is read-write and we don't check if `Close` succeeds
68- f .Close () // NOT OK
68+ f .Close () // $ Alert
6969 }
7070}
7171
@@ -105,9 +105,9 @@ func deferredCloseWithSync() {
105105
106106func deferredCloseWithSyncEarlyReturn (n int ) {
107107 // open file for writing
108- if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
108+ if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source
109109 // a call to `Close` is deferred
110- defer f .Close () // NOT OK
110+ defer f .Close () // $ Alert
111111
112112 if n > 100 {
113113 return
@@ -122,10 +122,10 @@ func deferredCloseWithSyncEarlyReturn(n int) {
122122
123123func unhandledSync () {
124124 // open file for writing
125- if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
125+ if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source
126126 // we have a call to `Sync` which precedes the call to `Close`, but there is no check
127127 // to see if `Sync` may have failed
128128 f .Sync ()
129- f .Close () // NOT OK
129+ f .Close () // $ Alert
130130 }
131131}
0 commit comments