-
Notifications
You must be signed in to change notification settings - Fork 305
feature: new rule enforce-switch-default #1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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 |
There was a problem hiding this 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. |
Inspired by this remark this PR adds a new rule to enforce
default
inswitch
.The rule, by default, enforces:
default
, anddefault
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.