go.unchecked_error
Correctness
Medium
Detects Go code that ignores error return values.
Why It Matters
Section titled “Why It Matters”Go’s error handling philosophy requires explicit handling:
- Silent failures — Operations fail but code continues as if successful
- Data corruption — Writes fail, reads return garbage, code proceeds
- Debugging nightmare — Errors surface far from their origin
- Unexpected panics — Nil results used without checking
Go makes errors explicit for a reason. Ignoring them defeats the language’s safety design.
Example
Section titled “Example”// ❌ Beforeos.ReadFile("config.json") // Error ignoredjson.Unmarshal(data, &config) // Error ignoredIf the file doesn’t exist or JSON is invalid, you’ll get mysterious failures later.
// ✅ Afterdata, err := os.ReadFile("config.json")if err != nil { return fmt.Errorf("reading config: %w", err)}
if err := json.Unmarshal(data, &config); err != nil { return fmt.Errorf("parsing config: %w", err)}What Unfault Detects
Section titled “What Unfault Detects”- Function calls with error return values that are discarded
_used to explicitly ignore errors- Error variables declared but never checked
Auto-Fix
Section titled “Auto-Fix”Unfault adds error handling with appropriate error wrapping.
Common Patterns
Section titled “Common Patterns”// Return early on errordata, err := fetch()if err != nil { return nil, err}
// Wrap with contextif err := save(data); err != nil { return fmt.Errorf("saving data: %w", err)}
// Log and continue (when recovery is possible)if err := sendMetric(m); err != nil { log.Warn().Err(err).Msg("failed to send metric") // Continue - metrics are not critical}
// Explicit ignore (rare, document why)_ = conn.Close() // Best effort, already handling error