Skip to content

Add cop to disallow case structures where all when clauses are proc literals - #797

Open
rwstauner wants to merge 1 commit into
mainfrom
rwstauner/case-when-all-procs
Open

Add cop to disallow case structures where all when clauses are proc literals#797
rwstauner wants to merge 1 commit into
mainfrom
rwstauner/case-when-all-procs

Conversation

@rwstauner

Copy link
Copy Markdown

An if/else tree is equivalent without allocating the procs.

This also triggers if the only non-proc clauses are literal values.

@rwstauner
rwstauner requested a review from a team as a code owner July 15, 2026 19:28
@rwstauner
rwstauner force-pushed the rwstauner/case-when-all-procs branch from 5230147 to f705f00 Compare July 15, 2026 19:29
@sambostock

Copy link
Copy Markdown
Contributor

This feels like one of those things where maybe we should be taking a look at solving it at the interpreter level. Is there a reason an if/elsif/else chain can avoid the allocation, but a case can't?

Comment thread lib/rubocop/cop/lint/proc_case_when.rb Outdated
…iterals

An if/else tree is equivalent without allocating the procs.

This also triggers if the only non-proc clauses are literal values.
@rwstauner
rwstauner force-pushed the rwstauner/case-when-all-procs branch from b932fd3 to c4832ff Compare August 5, 2026 20:49
@rwstauner

Copy link
Copy Markdown
Author

This feels like one of those things where maybe we should be taking a look at solving it at the interpreter level. Is there a reason an if/elsif/else chain can avoid the allocation, but a case can't?

The only reason a proc is used here is to bend the condition to fit into the case statement. Since you wouldn't do that using if/else, there's no special handling needed when using the more natural syntax.

It would be possible to optimize this in the interpreter (I believe we can reduce the allocation count by caching the proc at the parser level), but in order to inline the block to remove all the overhead I'm not convinced the interpeter bloat needed to accomplish the task is worth it.

@rwstauner
rwstauner requested a review from rafaelfranca August 5, 2026 20:53
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.

3 participants