Skip to content

New unreachable_code_linter - #1003

Merged
MichaelChirico merged 4 commits into
masterfrom
unreachable_code
Mar 27, 2022
Merged

MichaelChirico merged 4 commits into
masterfrom
unreachable_code

Conversation

@MichaelChirico

@MichaelChirico MichaelChirico commented Mar 27, 2022 •

Copy link
Copy Markdown
Collaborator

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 🤔

@AshesITR AshesITR left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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 /*

@MichaelChirico MichaelChirico Mar 27, 2022 •

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@MichaelChirico MichaelChirico mentioned this pull request Mar 28, 2022
38 tasks done
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