Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: Add linter rule to catch missing return after http writes #2702

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

Merged
merged 4 commits into from
Jun 28, 2022

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 28, 2022

Found a missing return again, figured I'd just write a linter rule to catch these going forward.

@Emyrk Emyrk enabled auto-merge (squash) June 28, 2022 14:40
@Emyrk Emyrk disabled auto-merge June 28, 2022 14:52
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

I'm stoked for this linter!

m.Match(`
if $*_ {
httpapi.Write($*a)
}
Copy link
Member

Choose a reason for hiding this comment

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

Thought: Does this, or should we, handle the else case as well? How about switch? Is there any way we can express that httapi.Write is inside a block vs function body?

Copy link
Member Author

@Emyrk Emyrk Jun 28, 2022

Choose a reason for hiding this comment

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

Hmm that is a good question. This is definitely the common case, but you are right about more possible situations.

I am unsure if I can match if OR else 🤔. This does not work for example

$*_ {
  httpapi.Write($*a)
}

I think I will merge this in as is for now. But expanding it would be nice, I just don't think ruleguard can match different AST nodes very well aside from enumerating all cases. Another idea I had was trying to match the correct case like this:

m.Match(`
httpapi.Write($*a)
$r
`).Where(!m["r"].Text.Matches("return"))

So the next line must be return. This is different than how I've used ruleguard in the past, but might be feasible? I was not able to get it to work with some quick tinkering. It's hard to match for the negative case (missing "return"), and when I've done similar match stuff in the past, the linter starts to become incredibly slow.

Copy link
Member

Choose a reason for hiding this comment

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

Ah interesting, thanks for checking into it. Yup feel free to merge as-is, fixing the other cases is more of a nice-to-have anyway, def. don't want it becoming slow.

@Emyrk Emyrk merged commit 576aef4 into main Jun 28, 2022
@Emyrk Emyrk deleted the stevenmasley/http_write_lint branch June 28, 2022 19:13
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.

4 participants