Skip to content

Commit

Permalink
Added the selective-enforce and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-mongodb committed Nov 7, 2023
1 parent bc6a183 commit c728146
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
7 changes: 4 additions & 3 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ func isGeneratedFile(file *ast.File) bool {
}

const (
ignoreComment = "//exhaustive:ignore"
enforceComment = "//exhaustive:enforce"
defaultRequireIgnoreComment = "//exhaustive:default-require-ignore"
ignoreComment = "//exhaustive:ignore"
enforceComment = "//exhaustive:enforce"
defaultRequireIgnoreComment = "//exhaustive:default-require-ignore"
defaultRequireEnforceComment = "//exhaustive:default-require-enforce"
)

func hasCommentPrefix(comments []*ast.CommentGroup, comment string) bool {
Expand Down
1 change: 1 addition & 0 deletions exhaustive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestExhaustive(t *testing.T) {

// This tests exercises the default-case-required flag and its escape comment
runTest(t, "default-signifies-exhaustive/default-required/...", func() { fDefaultCaseRequired = true })
runTest(t, "default-signifies-exhaustive/default-not-required/...", func() { fDefaultCaseRequired = false })

// Tests for -ignore-enum-members and -ignore-enum-types flags.
runTest(t, "ignore-pattern/...", func() {
Expand Down
23 changes: 13 additions & 10 deletions switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ func switchChecker(pass *analysis.Pass, cfg switchConfig, generated boolCache, c
// enforce comment.
return true, resultNoEnforceComment
}
requireDefaultCase := cfg.defaultCaseRequired
if hasCommentPrefix(switchComments, defaultRequireIgnoreComment) {
requireDefaultCase = false
} else if hasCommentPrefix(switchComments, defaultRequireEnforceComment) {
requireDefaultCase = true
}

if sw.Tag == nil {
return true, resultNoSwitchTag // never possible for valid Go program?
Expand All @@ -117,16 +123,13 @@ func switchChecker(pass *analysis.Pass, cfg switchConfig, generated boolCache, c
}

defaultCaseExists := analyzeSwitchClauses(sw, pass.TypesInfo, checkl.found)
if !defaultCaseExists && cfg.defaultCaseRequired {
// Don't enforce this if the user opted-out
if !hasCommentPrefix(switchComments, defaultRequireIgnoreComment) {
// Even if the switch explicitly enumerates all the
// enum values, the user has still required all switches
// to have a default case. We check this first to avoid
// early-outs
pass.Report(makeMissingDefaultDiagnostic(sw, dedupEnumTypes(toEnumTypes(es))))
return true, resultMissingDefaultCase
}
if !defaultCaseExists && requireDefaultCase {
// Even if the switch explicitly enumerates all the
// enum values, the user has still required all switches
// to have a default case. We check this first to avoid
// early-outs
pass.Report(makeMissingDefaultDiagnostic(sw, dedupEnumTypes(toEnumTypes(es))))
return true, resultMissingDefaultCase
}
if len(checkl.remaining()) == 0 {
// All enum members accounted for.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package present

import "default-signifies-exhaustive"

func _a(t dse.T) {
// No diagnostic because default-require is not set.
switch t {
case dse.A:
case dse.B:
}
}

func _b(t dse.T) {
//exhaustive:default-require-enforce this is a comment showing that we can turn it on for select switches
switch t { // want "^missing default in switch of type dse.T$"
case dse.A:
case dse.B:
}
}

func _c(t dse.T) {
//exhaustive:default-require-enforce this is happy because it has a default
switch t {
case dse.A:
case dse.B:
default:
}
}

0 comments on commit c728146

Please sign in to comment.