-
Notifications
You must be signed in to change notification settings - Fork 11
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
Resolves #70: add stricter comment directive scan #72
Changes from 4 commits
65afac4
8733b84
cbba3b8
7539055
1995b79
9cfb907
499a89c
74821ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,21 +7,52 @@ import ( | |||||
) | ||||||
|
||||||
const ( | ||||||
ignoreComment = "//exhaustive:ignore" | ||||||
enforceComment = "//exhaustive:enforce" | ||||||
ignoreDefaultCaseRequiredComment = "//exhaustive:ignore-default-case-required" | ||||||
enforceDefaultCaseRequiredComment = "//exhaustive:enforce-default-case-required" | ||||||
exhaustiveComment = "//exhaustive:" | ||||||
ignoreComment = "ignore" | ||||||
enforceComment = "enforce" | ||||||
ignoreDefaultCaseRequiredComment = "ignore-default-case-required" | ||||||
enforceDefaultCaseRequiredComment = "enforce-default-case-required" | ||||||
) | ||||||
|
||||||
func hasCommentPrefix(comments []*ast.CommentGroup, comment string) bool { | ||||||
for _, c := range comments { | ||||||
for _, cc := range c.List { | ||||||
if strings.HasPrefix(cc.Text, comment) { | ||||||
return true | ||||||
type directive int64 | ||||||
|
||||||
const ( | ||||||
ignoreDirective = 1 << iota | ||||||
enforceDirective | ||||||
ignoreDefaultCaseRequiredDirective | ||||||
enforceDefaultCaseRequiredDirective | ||||||
) | ||||||
|
||||||
type directiveSet int64 | ||||||
|
||||||
func parseDirectiveSet(commentGroups []*ast.CommentGroup) (out directiveSet) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Minor: Remove the name for the return parameter in the function declaration. The name does not add value here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly to avoid the |
||||||
for _, commentGroup := range commentGroups { | ||||||
for _, comment := range commentGroup.List { | ||||||
commentLine := comment.Text | ||||||
if !strings.HasPrefix(commentLine, exhaustiveComment) { | ||||||
continue | ||||||
} | ||||||
directive := commentLine[len(exhaustiveComment):] | ||||||
if whiteSpaceIndex := strings.IndexAny(directive, " \t"); whiteSpaceIndex != -1 { | ||||||
directive = directive[:whiteSpaceIndex] | ||||||
} | ||||||
switch directive { | ||||||
case ignoreComment: | ||||||
out |= ignoreDirective | ||||||
case enforceComment: | ||||||
out |= enforceDirective | ||||||
case ignoreDefaultCaseRequiredComment: | ||||||
out |= ignoreDefaultCaseRequiredDirective | ||||||
case enforceDefaultCaseRequiredComment: | ||||||
out |= enforceDefaultCaseRequiredDirective | ||||||
} | ||||||
} | ||||||
} | ||||||
return false | ||||||
return | ||||||
} | ||||||
|
||||||
func (d directiveSet) hasDirective(directive directive) bool { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Shorten the method name to It also avoids stutter in call sites.
vs.
|
||||||
return int64(d)&int64(directive) != 0 | ||||||
} | ||||||
|
||||||
func fileCommentMap(fset *token.FileSet, file *ast.File) ast.CommentMap { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ import ( | |
"go/ast" | ||
"go/types" | ||
"regexp" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
@@ -60,41 +59,6 @@ type switchConfig struct { | |
ignoreType *regexp.Regexp // can be nil | ||
} | ||
|
||
// There are few possibilities, and often none, so we use a possibly-nil slice | ||
func userDirectives(comments []*ast.CommentGroup) []string { | ||
var directives []string | ||
for _, c := range comments { | ||
for _, cc := range c.List { | ||
// The order matters here: we always want to check the longest first. | ||
for _, d := range []string{ | ||
enforceDefaultCaseRequiredComment, | ||
ignoreDefaultCaseRequiredComment, | ||
enforceComment, | ||
ignoreComment, | ||
} { | ||
if strings.HasPrefix(cc.Text, d) { | ||
directives = append(directives, d) | ||
// The break here is important: once we associate a comment | ||
// with a particular (longest-possible) directive, we don't want | ||
// to map to another! | ||
break | ||
} | ||
} | ||
} | ||
} | ||
return directives | ||
} | ||
|
||
// Can be replaced with slices.Contains with go1.21 | ||
func directivesIncludes(directives []string, d string) bool { | ||
for _, ud := range directives { | ||
if ud == d { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// switchChecker returns a node visitor that checks exhaustiveness of | ||
// enum switch statements for the supplied pass, and reports | ||
// diagnostics. The node visitor expects only *ast.SwitchStmt nodes. | ||
|
@@ -118,30 +82,31 @@ func switchChecker(pass *analysis.Pass, cfg switchConfig, generated boolCache, c | |
sw := n.(*ast.SwitchStmt) | ||
|
||
switchComments := comments.get(pass.Fset, file)[sw] | ||
uDirectives := userDirectives(switchComments) | ||
if !cfg.explicit && directivesIncludes(uDirectives, ignoreComment) { | ||
uDirectives := parseDirectiveSet(switchComments) | ||
|
||
if !cfg.explicit && uDirectives.hasDirective(ignoreDirective) { | ||
// Skip checking of this switch statement due to ignore | ||
// comment. Still return true because there may be nested | ||
// switch statements that are not to be ignored. | ||
return true, resultIgnoreComment | ||
} | ||
if cfg.explicit && !directivesIncludes(uDirectives, enforceComment) { | ||
if cfg.explicit && !uDirectives.hasDirective(enforceDirective) { | ||
// Skip checking of this switch statement due to missing | ||
// enforce comment. | ||
return true, resultNoEnforceComment | ||
} | ||
requireDefaultCase := cfg.defaultCaseRequired | ||
if directivesIncludes(uDirectives, ignoreDefaultCaseRequiredComment) { | ||
if uDirectives.hasDirective(ignoreDefaultCaseRequiredDirective) { | ||
requireDefaultCase = false | ||
} | ||
if directivesIncludes(uDirectives, enforceDefaultCaseRequiredComment) { | ||
if uDirectives.hasDirective(enforceDefaultCaseRequiredDirective) { | ||
// We have "if" instead of "else if" here in case of conflicting ignore/enforce directives. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether it would make more sense for the analyzer to raise an error in this case. I don't foresee it happening much in practice though. I'm more inclined to believe typos like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If you're referring to the scenario in which valid ignore and enforce comments are both present on the same switch statement — yes, reporting an error is preferable. Related issue
I agree that reporting an error for typoed and unknown directives (e.g. "enfocre", "enforce-none") is preferable. |
||
// In that case, because this is second, we will default to enforcing. | ||
requireDefaultCase = true | ||
} | ||
|
||
if sw.Tag == nil { | ||
return true, resultNoSwitchTag // never possible for valid Go program? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is possible, e.g. switch {
case err == nil:
return result, nil
case errors.Is(err, NotFound):
return result, nil
default:
return nil, err
} |
||
return true, resultNoSwitchTag | ||
} | ||
|
||
t := pass.TypesInfo.Types[sw.Tag] | ||
|
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.
Minor: Rename to
parseDirectives
. The "set" portion of the function name is a detail about the return type that is not necessary here.