-
-
Notifications
You must be signed in to change notification settings - Fork 398
Open
Description
Summary
The SA5000 check for "assignment to nil map" currently detects direct nil map assignments but misses assignments to nil map fields within structs, including nested struct fields.
Current Behavior
Staticcheck only detects case 1 but misses cases 2 and 3:
package main
type StructWithMap struct {
Map map[string]string
}
type NestedStruct struct {
Inner StructWithMap
}
func testNilMapAssignments() {
// Case 1: Direct nil map assignment - ✅ DETECTED by staticcheck
var m map[string]string
m["key"] = "value" // staticcheck catches this (SA5000)
// Case 2: Struct field nil map assignment - ❌ NOT DETECTED
var swm StructWithMap
swm.Map["key1"] = "value1" // staticcheck misses this
// Case 3: Nested struct field nil map assignment - ❌ NOT DETECTED
var ns NestedStruct
ns.Inner.Map["key2"] = "value2" // staticcheck misses this
// Case 4: Initialized map (should NOT be detected)
m2 := make(map[string]string)
m2["key"] = "value"
// Case 5: Map literal (should NOT be detected)
m3 := map[string]string{"existing": "value"}
m3["key"] = "value"
}Test Results
Running staticcheck test-nilmap.go only reports:
test-nilmap.go:14:2: assignment to nil map (SA5000)
It misses the struct field assignments on lines 18 and 22.
Expected Behavior
SA5000 should detect all three cases of nil map assignments:
- Direct nil map assignments ✅ (already works)
- Assignments to nil map fields in structs ❌ (missing)
- Assignments to nil map fields in nested structs ❌ (missing)
Cases 4 and 5 should correctly NOT be flagged since those maps are properly initialized.
Runtime Behavior
All three problematic cases cause the same runtime panic:
panic: assignment to entry in nil map
Environment
- Go version: go1.23.4
- Staticcheck version: (staticcheck 2025.1.1 (0.6.1))
- Operating System: Linux
ccoVeille