Handle assignments to global variables from the init function#253
Handle assignments to global variables from the init function#253k4n4ry wants to merge 8 commits intouber-go:mainfrom
Conversation
|
@k4n4ry, thank you for submitting the PR! We'll take a look soon and follow up with you. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #253 +/- ##
==========================================
+ Coverage 87.16% 87.26% +0.09%
==========================================
Files 72 72
Lines 8291 8336 +45
==========================================
+ Hits 7227 7274 +47
+ Misses 870 869 -1
+ Partials 194 193 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
assertion/global/analyzer.go
Outdated
|
|
||
| // getInitFuncDecl searches for the init function declaration in the given *ast.File. | ||
| // It returns the *ast.FuncDecl representing the init function if found, or nil otherwise. | ||
| func getInitFuncDecl(file *ast.File) *ast.FuncDecl { |
There was a problem hiding this comment.
This only returns the first init function. I believe there can be an arbitrary number of them.
There was a problem hiding this comment.
Thank you. I have made the changes you suggested and added additional tests.
|
@SongShawn |
|
@sonalmahajan15 |
|
I'm updating your branch since we recently merged CI fixes, which are required for the CI to pass on forked repositories. I'll review the code this week :) |
Golden TestWarning ❌ NilAway errors reported on stdlib are different 📉. 2567 errors on base branch (main, 99d94ca) Diffs+ /opt/hostedtoolcache/go/1.25.6/x64/src/archive/tar/writer.go:543:7: Potential nil panic detected. Observed nil flow from source to dereference point:
+ - tar/writer.go:304:27: unassigned variable `spb` passed as arg `p` to `Write()`
+ - tar/writer.go:540:26: passed as parameter `b` to `regFileWriter.Write()` (implementing `Writer.Write()`)
+ - tar/writer.go:543:7: function parameter `b` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/archive/tar/writer.go:583:7: Potential nil panic detected. Observed nil flow from source to dereference point:
+ - tar/writer.go:304:27: unassigned variable `spb` passed as arg `p` to `Write()`
+ - tar/writer.go:580:29: passed as parameter `b` to `sparseFileWriter.Write()` (implementing `Writer.Write()`)
+ - tar/writer.go:583:7: function parameter `b` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/archive/zip/zip_test.go:431:7: Potential nil panic detected. Observed nil flow from source to dereference point:
+ - flate/deflate.go:171:33: unassigned variable `window` passed as arg `input` to `writeBlock()`
+ - flate/huffman_bit_writer.go:478:16: function parameter `input` passed as arg `bytes` to `writeBytes()`
+ - flate/huffman_bit_writer.go:185:10: function parameter `bytes` passed as arg `b` to `write()`
+ - flate/huffman_bit_writer.go:136:28: function parameter `b` passed as arg `p` to `Write()`
+ - zip/zip_test.go:421:24: passed as parameter `p` to `suffixSaver.Write()` (implementing `Writer.Write()`)
+ - zip/zip_test.go:431:7: function parameter `p` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/bytes/bytes.go:404:7: Potential nil panic detected. Observed nil flow from source to dereference point:
+ - decodemeta/decodefile.go:175:11: result 0 of `GetPackagePayload()` lacking guarding; passed as arg `p` to `Write()` via the assignment(s):
+ - `r.GetPackagePayload(...)` to `pp` at decodemeta/decodefile.go:172:2
+ - io/multi.go:83:23: passed as parameter `p` to `multiWriter.Write()` (implementing `Writer.Write()`)
+ - io/multi.go:85:20: function parameter `p` passed as arg `p` to `Write()`
+ - testing/testing.go:1127:24: passed as parameter `p` to `outputWriter.Write()` (implementing `Writer.Write()`)
+ - testing/testing.go:1141:28: function parameter `p` passed as arg `s` to `SplitAfter()`
+ - bytes/bytes.go:446:18: function parameter `s` passed as arg `s` to `genSplit()`
+ - bytes/bytes.go:404:7: function parameter `s` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/bytes/bytes.go:404:7: Potential nil panic detected. Observed nil flow from source to dereference point:
+ - decodemeta/decodefile.go:175:11: result 0 of `GetPackagePayload()` lacking guarding; passed as arg `p` to `Write()` via the assignment(s):
+ - `r.GetPackagePayload(...)` to `pp` at decodemeta/decodefile.go:172:2
+ - testing/testing.go:1127:24: passed as parameter `p` to `outputWriter.Write()` (implementing `Writer.Write()`)
+ - testing/testing.go:1141:28: function parameter `p` passed as arg `s` to `SplitAfter()`
+ - bytes/bytes.go:446:18: function parameter `s` passed as arg `s` to `genSplit()`
+ - bytes/bytes.go:404:7: function parameter `s` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/bytes/bytes.go:404:7: Potential nil panic detected. Observed nil flow from source to dereference point:
+ - flate/deflate.go:171:33: unassigned variable `window` passed as arg `input` to `writeBlock()`
+ - flate/huffman_bit_writer.go:478:16: function parameter `input` passed as arg `bytes` to `writeBytes()`
+ - flate/huffman_bit_writer.go:185:10: function parameter `bytes` passed as arg `b` to `write()`
+ - flate/huffman_bit_writer.go:136:28: function parameter `b` passed as arg `p` to `Write()`
+ - io/multi.go:83:23: passed as parameter `p` to `multiWriter.Write()` (implementing `Writer.Write()`)
+ - io/multi.go:85:20: function parameter `p` passed as arg `p` to `Write()`
+ - testing/testing.go:1127:24: passed as parameter `p` to `outputWriter.Write()` (implementing `Writer.Write()`)
+ - testing/testing.go:1141:28: function parameter `p` passed as arg `s` to `SplitAfter()`
+ - bytes/bytes.go:446:18: function parameter `s` passed as arg `s` to `genSplit()`
+ - bytes/bytes.go:404:7: function parameter `s` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/bytes/bytes.go:404:7: Potential nil panic detected. Observed nil flow from source to dereference point:
+ - flate/deflate.go:171:33: unassigned variable `window` passed as arg `input` to `writeBlock()`
+ - flate/huffman_bit_writer.go:478:16: function parameter `input` passed as arg `bytes` to `writeBytes()`
+ - flate/huffman_bit_writer.go:185:10: function parameter `bytes` passed as arg `b` to `write()`
+ - flate/huffman_bit_writer.go:136:28: function parameter `b` passed as arg `p` to `Write()`
+ - io/multi.go:83:23: passed as parameter `p` to `multiWriter.Write()` (implementing `Writer.Write()`)
+ - io/multi.go:85:20: function parameter `p` passed as arg
...(truncated)...
.go:7230:22: nilable value assigned into global variable `Tptr`
- - reflect/all_test.go:7315:31: global variable `Tptr` passed as arg `t` to `SliceOf()`
- - reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/reflect/type.go:2077:9: Potential nil panic detected. Observed nil flow from source to dereference point:
- - reflect/all_test.go:7230:28: nilable value assigned into global variable `Tscalarptr`
- - reflect/all_test.go:7338:39: global variable `Tscalarptr` passed as arg `elem` to `ArrayOf()`
- - reflect/type.go:2649:26: function parameter `elem` passed as arg `t` to `SliceOf()`
- - reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/reflect/type.go:2077:9: Potential nil panic detected. Observed nil flow from source to dereference point:
- - reflect/all_test.go:7230:40: nilable value assigned into global variable `Tptrscalar`
- - reflect/all_test.go:7327:46: global variable `Tptrscalar` passed as arg `elem` to `ArrayOf()`
- - reflect/type.go:2649:26: function parameter `elem` passed as arg `t` to `SliceOf()`
- - reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/reflect/type.go:2077:9: Potential nil panic detected. Observed nil flow from source to dereference point:
- - reflect/all_test.go:7230:52: nilable value assigned into global variable `Tbigptrscalar`
- - reflect/all_test.go:7329:31: global variable `Tbigptrscalar` passed as arg `t` to `SliceOf()`
- - reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/reflect/type.go:2077:9: Potential nil panic detected. Observed nil flow from source to dereference point:
- - reflect/all_test.go:7230:5: nilable value assigned into global variable `Tscalar`
- - reflect/all_test.go:7344:41: global variable `Tscalar` passed as arg `elem` to `ArrayOf()`
- - reflect/type.go:2649:26: function parameter `elem` passed as arg `t` to `SliceOf()`
- - reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/runtime/unsafepoint_test.go:43:90: Potential nil panic detected. Observed nil flow from source to dereference point:
- - os/proc.go:16:5: nilable value assigned into global variable `Args`
- - runtime/unsafepoint_test.go:43:90: global variable `Args` sliced into
- /opt/hostedtoolcache/go/1.25.6/x64/src/sync/example_pool_test.go:38:2: Potential nil panic detected. Observed nil flow from source to dereference point:
- - os/file_unix.go:90:10: literal `nil` returned from `newFileFromNewFile()` in position 0
- - os/file.go:134:9: result 0 of `newFileFromNewFile()` returned from `NewFile()` in position 0
- - os/file.go:73:2: result 0 of `NewFile()` assigned into global variable `Stdout`
- - sync/example_pool_test.go:43:6: global variable `Stdout` passed as arg `w` to `Log()`
- - sync/example_pool_test.go:38:2: function parameter `w` called `Write()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/syscall/exec_pdeathsig_test.go:40:52: Potential nil panic detected. Observed nil flow from source to dereference point:
- - os/proc.go:16:5: nilable value assigned into global variable `Args`
- - syscall/exec_pdeathsig_test.go:40:52: global variable `Args` sliced into
-
- (Same nil source could also cause potential nil panic(s) at 5 other place(s): "syscall/exec_pdeathsig_test.go:42:22", "syscall/exec_unix_test.go:338:19", "syscall/exec_unix_test.go:339:15", "syscall/syscall_linux_test.go:197:52", and "syscall/syscall_linux_test.go:199:22".)
- /opt/hostedtoolcache/go/1.25.6/x64/src/text/template/funcs.go:642:2: Potential nil panic detected. Observed nil flow from source to dereference point:
- - os/file_unix.go:90:10: literal `nil` returned from `newFileFromNewFile()` in position 0
- - os/file.go:134:9: result 0 of `newFileFromNewFile()` returned from `NewFile()` in position 0
- - os/file.go:73:2: result 0 of `NewFile()` assigned into global variable `Stdout`
- - template/example_test.go:104:22: global variable `Stdout` passed as arg `w` to `HTMLEscape()`
- - template/escape.go:963:22: function parameter `w` passed as arg `w` to `HTMLEscape()`
- - template/funcs.go:642:2: function parameter `w` called `Write()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/text/template/funcs.go:725:2: Potential nil panic detected. Observed nil flow from source to dereference point:
- - os/file_unix.go:90:10: literal `nil` returned from `newFileFromNewFile()` in position 0
- - os/file.go:134:9: result 0 of `newFileFromNewFile()` returned from `NewFile()` in position 0
- - os/file.go:73:2: result 0 of `NewFile()` assigned into global variable `Stdout`
- - template/example_test.go:109:20: global variable `Stdout` passed as arg `w` to `JSEscape()`
- - template/escape.go:979:20: function parameter `w` passed as arg `w` to `JSEscape()`
- - template/funcs.go:725:2: function parameter `w` called `Write()` |
|
Hi, any news on landing this? I've been running this locally and it's very useful 😄 |

fixes #56
run()to retrieve the init function from the file.getGlobalConsumers(), analyzed assignments to global variables from the init function and used it as one of the criteria for determining consumer creation.Please provide comments if this implementation differs from nilaway's coding practices or design principles. Thank you.