nilcheck
About 237 wordsLess than 1 minute
2025-01-16
Enforces nil checks on pointer parameters before use.
Category
Safety
What It Checks
This analyzer detects pointer parameters that are used without being checked for nil first.
Why It Matters
Nil pointer dereferences cause panics:
panic: runtime error: invalid memory address or nil pointer dereferenceExplicit nil checks provide better error messages and prevent crashes.
Examples
Bad
func ProcessUser(user *User) error {
// Panics if user is nil!
return user.Save()
}Good
func ProcessUser(user *User) error {
if user == nil {
return errors.New("user cannot be nil")
}
return user.Save()
}Bad: Multiple Usages
func UpdateProfile(user *User, profile *Profile) error {
user.Name = profile.Name // Both could panic!
user.Email = profile.Email
return user.Save()
}Good: Check All Pointers
func UpdateProfile(user *User, profile *Profile) error {
if user == nil {
return errors.New("user cannot be nil")
}
if profile == nil {
return errors.New("profile cannot be nil")
}
user.Name = profile.Name
user.Email = profile.Email
return user.Save()
}Trusted Types
The analyzer skips certain types that are guaranteed non-nil by their frameworks:
*testing.T,*testing.B,*testing.M*gin.Context,*gin.Engine*cobra.Command*http.Request,http.ResponseWritercontext.Context
Skipped Files
The analyzer skips generated files:
*_gen.go*.pb.gozz_generated*.go- Files in
mock_*directories
Configuration
# .golint-sl.yaml
analyzers:
nilcheck: true # enabled by defaultWhen to Disable
This analyzer should rarely be disabled. Nil checks are fundamental safety.
analyzers:
nilcheck: false # Not recommended