Skip to content

Conversation

chavacava
Copy link
Collaborator

@chavacava chavacava commented May 28, 2025

Inspired by this remark this PR adds a new rule to enforce default in switch.

The rule, by default, enforces:

  1. the presence of default, and
  2. the position (last one) of the default

Both checks can be deactivated independently through configuration.

Notes:
1, Yes, there is another linter that does something similar but: a) the implementation feels awkward to me; b) it doesn't checks default position.
2. Yes, something similar could be done for select but IMO it is better to provide 2 different rules because.... TL;TW rule simplicity and ease of use.

@ccoVeille
Copy link
Contributor

ccoVeille commented May 29, 2025

This is also available

https://go-critic.com/overview.html#defaultcaseorder

I feel like adding an option to go-critic to enforce the last position should be preferred

@alexandear alexandear requested a review from Copilot May 29, 2025 15:00
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new rule, enforce-switch-default, that validates the presence and the position of default clauses in switch statements, with configurable options.

  • Added test files demonstrating various switch clause configurations.
  • Implemented the enforce-switch-default rule and integrated it into the core rules and documentation.
  • Updated RULES_DESCRIPTIONS.md and README.md with the new rule's details.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
testdata/enforce_switch_default_allow_not_last.go Test cases for allowing default clauses to appear in non-final positions.
testdata/enforce_switch_default_allow_no_default.go Test cases for allowing switch statements without a default clause.
testdata/enforce_switch_default.go Test cases for enforcing both the presence and last-position of the default clause.
test/enforce_switch_default_test.go Automated tests validating the new rule under various configurations.
rule/enforce_switch_default.go Implementation of the enforce-switch-default rule.
config/config.go Registration of the new rule in the configuration.
RULES_DESCRIPTIONS.md Documentation update for the new rule.
README.md Updated rule summary table in the README.

@chavacava chavacava requested a review from alexandear May 30, 2025 20:30
@chavacava chavacava merged commit cfd6943 into master Jun 1, 2025
8 checks passed
@chavacava chavacava deleted the enforce-switch-default branch June 1, 2025 08:13
@alexandear alexandear mentioned this pull request Jul 1, 2025
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.

3 participants