Skip to content

New unreachable_code_linter #1003

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
Mar 27, 2022
Merged

New unreachable_code_linter #1003

merged 4 commits into from
Mar 27, 2022

Conversation

MichaelChirico
Copy link
Collaborator

@MichaelChirico MichaelChirico commented Mar 27, 2022

Part of #962

The current version of this works best alongside our ExplicitReturnLinter(), where terminal statements will already be return(). The logic of nailing down the last expression in xpath is a bit of a mess so we avoided replicating it inside this linter. Now I'm wondering how useful this linter is without that added logic 🤔

Copy link
Collaborator

@AshesITR AshesITR left a comment

Choose a reason for hiding this comment

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

This one might benefit from running on some packages in the wild. WDYT?

//FUNCTION
/following-sibling::expr
/*[
self::expr
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is equivalent to using /expr instead of /*

Copy link
Collaborator Author

@MichaelChirico MichaelChirico Mar 27, 2022

Choose a reason for hiding this comment

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

not quite, because position() needs to include all nodes, not just expr. I'll add a comment.

/*[
self::expr
and expr[SYMBOL_FUNCTION_CALL[text() = 'return' or text() = 'stop']]
and (position() != last() - 1 or not(following-sibling::OP-RIGHT-BRACE))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious: Does this cope properly with return in switch?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed it doesn't -- just added an example test (commented out) to make sure I captured what you had in mind.

I think tryCatch() is equally affected.

@MichaelChirico
Copy link
Collaborator Author

This one might benefit from running on some packages in the wild. WDYT?

I recommend covering all the new linters together.

FWIW, the linters have all been vetted on our internal code base (O(10k) files) so I'm reasonably confident about false positives. False negatives are something we can always improve iteratively.

@MichaelChirico MichaelChirico merged commit 3aca22f into master Mar 27, 2022
@MichaelChirico MichaelChirico deleted the unreachable_code branch March 27, 2022 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants