-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Check for repeated values in case conditionals #3408
Comments
Hi there, I am working on this. Should I be dealing with multi-value cases (such as those that I mention below)? Or just create a PR for the single-value case expressions as noted by @agrimm ? I am running into a bit of trouble dealing multi-value cases such as
and,
Specifically I am not sure how to "extract" the when conditions so that I can perform some validation logic only on them, and not the result statements.
How am I supposed to differentiate between the when conditions and the result? Does anyone have any tips? I am new to contributing to rubocop but would very much like to get started, any help is appreciated. |
@swcraig: The Example:
I prototyped this cop last night, but I'd much rather see a new contributor take this up. Feel free to ping me if you have any more questions. 😀 |
Awesome @Drenmi , thank you! The node structures makes more sense now, and I have multi-value cases working regardless of ordering. I feel that the following should maybe be a new issue (it is checking for redundant operational logic, rather than just checking for repeated values in case conditionals); I have resolved the case presented in the original comment including multi-value. I can create a PR right now that fixes this issue unless the following should also be addressed: I am now considering expressions with logical operators and all of the different cases that these would bring...
This is obviously a very simple, contrived case, but could become very complex using multiple operators and different ordering of variables. I would need to be checking whether the when expressions (or "chunks" of them) are logically equivalent and thus repeated throughout the case expression. Is this out of scope and should be a new ticket? |
Disregard the previous commit. |
Does this PR look correct? |
I think this is a bit out of scope for this cop. It is neat that we get the root node of the expressions, and can compare them directly. It makes the cop easy to understand, and I would personally be happy keeping it to that right now. 😀 (I vaguely recall there being a suggestion for a cop checking for ambiguous or redundant predicates.) |
@Drenmi Thank you again for all your help on this! |
Repeated values should be checked for in case conditionals, because they're code that shouldn't have any effect (if it does, then that's a bad sign!).
Expected behavior
With the following code, RuboCop warns about "bar" appearing in the second
when
conditionalActual behavior
No offense is raised.
Steps to reproduce the problem
Feature request, not bug.
RuboCop version
Include the output of
rubocop -V
:The text was updated successfully, but these errors were encountered: