Skip to content

Conversation

@anupriyakkumari
Copy link
Contributor

@anupriyakkumari anupriyakkumari commented Oct 20, 2025

Description

This PR improves the useless-fallthrough rule to allow an empty fallthrough before a default case in switch statements, especially for enum-like constants.

Background:

  • The exhaustive linter requires all enum-like constants to be covered in switch cases.
  • revive currently requires a default case in switches.
  • Go doesn't support exhaustive enums natively, so a default: case is often necessary even when all constants are handled.
  • Combining a default with a case that only contains fallthrough is invalid.
  • This change prevents the rule from flagging such valid fallthroughs used to account for exhaustive switches.

To Reproduce

type SomeType int

const (
    One SomeType = iota
    Two
    Three
    Unknown
)

func CheckParsedVal(i SomeType) int {
    switch i {
    case One:
        return 1
    case Two:
        return 2
    case Three:
        return 3
    case Unknown:
        fallthrough
    default:
        return -1
    }
}

Expected behavior

  • The fallthrough before the default case should not be reported as useless.
  • This allows exhaustive switches to use a default case gracefully without requiring comments or lowering confidence scores.

Checklist

  • Added tests to cover the new behavior
  • Code follows the coding style of the repository
  • GitHub Action build passes

Footer

Closes #1538

@anupriyakkumari anupriyakkumari changed the title Handling useless-fallthrough rule conflict fix(useless-fallthrough): ignore fallthrough before default to support exhaustive switches Oct 20, 2025
@anupriyakkumari anupriyakkumari force-pushed the useless-fallthrough-conflict branch from fa8d222 to dcdcbbd Compare October 24, 2025 10:24
@alexandear alexandear changed the title fix(useless-fallthrough): ignore fallthrough before default to support exhaustive switches useless-fallthrough: ignore fallthrough before default to support exhaustive switches Oct 24, 2025
@alexandear alexandear changed the title useless-fallthrough: ignore fallthrough before default to support exhaustive switches useless-fallthrough: do not report fallthrough before default Oct 24, 2025
@alexandear alexandear merged commit cc04b17 into mgechev:master Oct 24, 2025
10 checks passed
@anupriyakkumari anupriyakkumari deleted the useless-fallthrough-conflict branch October 26, 2025 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New "useless-fallthrough" rule conflicts with "exhaustive"

4 participants