A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/rubocop/rubocop/issues/13121 below:

detect redundant setting to empty string inside string interpolation · Issue #13121 · rubocop/rubocop · GitHub

Is your feature request related to a problem? Please describe.

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