Skip to content
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

Conversation

navijation
Copy link
Contributor

@navijation navijation commented Dec 30, 2023

Add directiveSet bitset data structure and directive enum with stricter parsing to not recognize comments like //exhaustive:enforce-none as valid directives. Also return errors on invalid directives or combinations thereof.

Resolves #70, #49

// 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?
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}

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.
Copy link
Contributor Author

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.

Copy link
Owner

@nishanths nishanths Dec 31, 2023

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.

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'm more inclined to believe typos like //exhaustive:enfocre should result in errors.

I agree that reporting an error for typoed and unknown directives (e.g. "enfocre", "enforce-none") is preferable.

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.
Copy link
Owner

@nishanths nishanths Dec 31, 2023

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.

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'm more inclined to believe typos like //exhaustive:enfocre should result in errors.

I agree that reporting an error for typoed and unknown directives (e.g. "enfocre", "enforce-none") is preferable.

comment.go Outdated

type directiveSet int64

func parseDirectiveSet(commentGroups []*ast.CommentGroup) (out directiveSet) {
Copy link
Owner

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.

comment.go Outdated
return
}

func (d directiveSet) hasDirective(directive directive) bool {
Copy link
Owner

Choose a reason for hiding this comment

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

Minor: Shorten the method name to has. The shorter name is sufficiently clear, because checking for the presence of a directive is the apparent operation on this type.

It also avoids stutter in call sites.

directives.hasDirective(enforceDirective)

vs.

directives.has(enforceDirective)

comment.go Outdated

type directiveSet int64

func parseDirectiveSet(commentGroups []*ast.CommentGroup) (out directiveSet) {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
func parseDirectiveSet(commentGroups []*ast.CommentGroup) (out directiveSet) {
func parseDirectiveSet(commentGroups []*ast.CommentGroup) directiveSet {

Minor: Remove the name for the return parameter in the function declaration. The name does not add value here.

Choose a reason for hiding this comment

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

Mostly to avoid the var out directiveSet declaration but fair enough

@nishanths
Copy link
Owner

Thanks for the change. It looks good to me overall. I added a few comments.

@navijation
Copy link
Contributor Author

@nishanths Thanks for the feedback, addressed all.

@nishanths
Copy link
Owner

I added another test and a small change.

@nishanths
Copy link
Owner

Thank you!

@nishanths nishanths merged commit a50e355 into nishanths:master Feb 27, 2024
9 checks passed
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.

stricter requirements for directive comments
3 participants