I sometimes encounter code with an if/else condition that sets a variable to an empty string as one of the outcomes.
This isn't needed if the variable is only being used in string interpolation, and although thats not always something rubocop can detect, I assume it can detect if it the condition is inside of a string.
# bad, but rubocop isn't able to know that. example = [true, false].sample ? '--example-arg' : '' puts "program #{example} --bonus" # manually, this can be corrected if the user can ensure this is the only usage of example. puts "program #{'--example-arg' if [true, false].sample} --bonus"
# bad puts "program #{example = [true, false].sample ? '--example-arg' : ''} --bonus" # good puts "program #{'--example-arg' if [true, false].sample} --bonus"Describe the solution you'd like
For the following code patterns to be flagged as offenses and autocorrectable:
# bad "bar #{condition ? 'foo' : ''}" "bar #{if condition; 'foo' else '' end}" # good "bar #{'foo' if condition}"
(and also the same for cases where the ternary operator is reversed and it should correct to unless
instead of if
, and also for empty strings in the form of ""
-- not sure about whether flagging assigning things to nil
is safe, i think it is?)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4