Skip to content
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

New cop Style/UnneededInterpolation #2460

Merged
merged 2 commits into from
Dec 1, 2015

Conversation

cgriego
Copy link
Contributor

@cgriego cgriego commented Nov 28, 2015

This cop checks for strings that are just a single interpolated expression, such as "#{@var}" that would be better off as @var.to_s or simply @var if the value is already a string. The intent is better communicated without using interpolation and it allocates less strings.

MSG = 'Prefer `to_s` over string interpolation.'

INTERPOLATION_TYPES = [
:ivar, :cvar, :gvar,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we include lvars here as well? And constants I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't have an lvar or constant directly embedded in a dstr node without also having a begin node. Otherwise you just get the string for a hashtag. :) "#lvar" "#CONSTANT" These are here to catch the types of interpolation that would violate Style/VariableInterpolation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. I didn't read the code carefully enough.

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 28, 2015

Implementing auto-correction for this is pretty simple, so you might add it while you're at it. We can probably add a rule about this in the style guide as well. Doubt many people would argue which approach is clearer.

@philoserf
Copy link

+1

Thus was a common issue in Chef.io code. The community included a rule for it in foodcritic.

@cgriego
Copy link
Contributor Author

cgriego commented Nov 29, 2015

Fixing the violations in the Rubocop code showed me that autocorrect would be trickier than I first thought because it'd have to handle (or skip) cases of implicit string concatenation "foo" "#{@bar}" which has been used in Rubocop code to adhere to the line length limit and that can't be simply swapped out in place or you'd get invalid code. "foo" @bar.to_s

@@ -205,8 +205,8 @@ def dirs_to_search(target_dir)
end

def old_auto_config_file_warning
warn Rainbow('Attention: rubocop-todo.yml has been renamed to ' \
"#{AUTO_GENERATED_FILE}").red
warn Rainbow('Attention: rubocop-todo.yml has been renamed to ' +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this is a case we shouldn't change, as consecutive string literals are processed more efficiently by the interpreter. (and this was simply one big string that didn't fit on a single line)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you thinking the cop should not report an offense for this code? I can get on board with that. Essentially thinking of the code as "Attention: rubocop-todo.yml has been renamed to #{AUTO_GENERATED_FILE}"which doesn't violate the rule. That would also make autocorrect much simpler again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.

# "#{@var}"
#
# # good
# @var.to_s

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might we add

# good
@var

since there are many cases where the result is a string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added! Hopefully that will help someone choose the cleaner option.

bbatsov added a commit that referenced this pull request Dec 1, 2015
New cop Style/UnneededInterpolation
@bbatsov bbatsov merged commit 33aa8bc into rubocop:master Dec 1, 2015
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 1, 2015

👍 Looks good! Thanks!

@aevernon
Copy link

Style/UnneededInterpolation flagged the following line in my code:

puts "#{File.expand_path($0)} finishing at #{`date`}"

Is the intention of this cop for me to rewrite it like this?

puts "#{File.expand_path($0)} finishing at " << `date`

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 15, 2016

Nope, you've discovered a bug. Please, report it as a separate ticket.

@aevernon
Copy link

Reported as #2647.

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.

4 participants