-
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
Merged
nishanths
merged 8 commits into
nishanths:master
from
navijation:nav/70/stricter-directive-comment-scan
Feb 27, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
65afac4
Resolves #70: use stricter comment directive parsing
navijation 8733b84
Add E2E test cases
navijation cbba3b8
Remove unused code
navijation 7539055
Remove incorrect comment
navijation 1995b79
Address minor feedback
navijation 9cfb907
Address error reporting feedback
navijation 499a89c
add testdata for no-op directive comment
nishanths 74821ea
remove error vars
nishanths File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,34 @@ 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, err := parseDirectives(switchComments) | ||
if err != nil { | ||
pass.Report(makeInvalidDirectiveDiagnostic(sw, err)) | ||
} | ||
|
||
if !cfg.explicit && uDirectives.has(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.has(enforceDirective) { | ||
// Skip checking of this switch statement due to missing | ||
// enforce comment. | ||
return true, resultNoEnforceComment | ||
} | ||
requireDefaultCase := cfg.defaultCaseRequired | ||
if directivesIncludes(uDirectives, ignoreDefaultCaseRequiredComment) { | ||
if uDirectives.has(ignoreDefaultCaseRequiredDirective) { | ||
requireDefaultCase = false | ||
} | ||
if directivesIncludes(uDirectives, enforceDefaultCaseRequiredComment) { | ||
if uDirectives.has(enforceDefaultCaseRequiredDirective) { | ||
// We have "if" instead of "else if" here in case of conflicting ignore/enforce directives. | ||
// 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] | ||
|
@@ -169,6 +137,7 @@ func switchChecker(pass *analysis.Pass, cfg switchConfig, generated boolCache, c | |
// 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 { | ||
|
@@ -234,3 +203,14 @@ func makeMissingDefaultDiagnostic(sw *ast.SwitchStmt, enumTypes []enumType) anal | |
), | ||
} | ||
} | ||
|
||
func makeInvalidDirectiveDiagnostic(node ast.Node, err error) analysis.Diagnostic { | ||
return analysis.Diagnostic{ | ||
Pos: node.Pos(), | ||
End: node.End(), | ||
Message: fmt.Sprintf( | ||
"failed to parse directives: %s", | ||
err.Error(), | ||
), | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
//exhaustive:enfocre
should result in errors.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.
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.