Skip to content

go.unchecked_error

Correctness Medium

Detects Go code that ignores error return values.

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.

// ❌ Before
os.ReadFile("config.json") // Error ignored
json.Unmarshal(data, &config) // Error ignored

If the file doesn’t exist or JSON is invalid, you’ll get mysterious failures later.

// ✅ After
data, 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)
}
  • Function calls with error return values that are discarded
  • _ used to explicitly ignore errors
  • Error variables declared but never checked

Unfault adds error handling with appropriate error wrapping.

// Return early on error
data, err := fetch()
if err != nil {
return nil, err
}
// Wrap with context
if 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