Back to all reviewers

Check nil before access

argoproj/argo-cd
Based on 5 comments
Go

Always verify that pointers and nested struct fields are not nil before accessing their members to prevent runtime panics. This is especially important when dealing with optional configuration fields, middleware components, and API responses where certain fields may not be initialized.

Null Handling Go

Reviewer Prompt

Always verify that pointers and nested struct fields are not nil before accessing their members to prevent runtime panics. This is especially important when dealing with optional configuration fields, middleware components, and API responses where certain fields may not be initialized.

Before accessing nested fields, check each level for nil:

// Bad - can cause nil pointer panic
*spec.SyncPolicy.Automated.Enable = true

// Good - check each level
if spec.SyncPolicy == nil {
    spec.SyncPolicy = &argoappv1.SyncPolicy{}
}
if spec.SyncPolicy.Automated == nil {
    spec.SyncPolicy.Automated = &argoappv1.SyncPolicyAutomated{}
}
*spec.SyncPolicy.Automated.Enable = true

For optional fields that may be missing, return nil gracefully rather than panicking:

// Good - handle missing claims safely
if _, ok := m["iat"]; !ok {
    return nil, nil // Claim is missing
}

Consider creating helper methods on structs to encapsulate nil checks, making the code more readable and preventing repeated nil checking logic throughout the codebase.

5
Comments Analyzed
Go
Primary Language
Null Handling
Category

Source Discussions