go compile_error ai_generated true

missing return at end of function

ID: go/missing-return

Also available as: JSON · Markdown
98%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Function declares return type but not all code paths return a value.

generic

Workarounds

  1. 95% success Ensure every code path (every if/else/switch branch) has a return statement
    every if/else/switch branch

    Sources: https://go.dev/ref/spec#Return_statements

  2. 92% success Add a default return after the if/switch to handle the 'else' case
    func classify(n int) string {
        if n > 0 {
            return "positive"
        } else if n < 0 {
            return "negative"
        }
        return "zero" // default return covers all paths
    }

    Sources: https://go.dev/ref/spec#Return_statements

Dead Ends

Common approaches that don't work:

  1. Add return at the end with zero value 55% fail

    May silently return wrong result — ensure the logic is correct

  2. Change return type to not return anything 75% fail

    If callers use the return value, this breaks them

Error Chain

Frequently confused with: