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

Style/MutableConstant doesn't detect mutable strings if constructed by concatenation #3695

Closed
patcox opened this issue Nov 1, 2016 · 3 comments

Comments

@patcox
Copy link

patcox commented Nov 1, 2016

rubocop doesn't give a warning if you construct a string from concatenated strings. See example below.


Code, Expected behavior

#!/opt/chef/embedded/bin/ruby

# Not constants. Correct no warning.
non_const1 = 'foo'
non_const2 = 'foo'.freeze

# Mutable constant. Correct warning.
CONST1 = 'foo'

# Immutable constant. Correct no warning.
CONST2 = 'foo'.freeze

# Mutable constant. rubocop *should* warn, but does not.
CONST3 = 'foo' + 'bar'

# Mutable constant. rubocop *should* warn, but does not.
CONST5 = 'foo' + 'bar'.freeze

# Mutable constant. rubocop *should* warn, but does not.
CONST6 = 'foo'.freeze + 'bar'

# Immutable constant. Correct no warning.
CONST7 = ('foo' + 'bar').freeze

Actual behavior


$ rubocop test.rb --only Style/MutableConstant
Inspecting 1 file
C

Offenses:

test.rb:8:10: C: Freeze mutable objects assigned to constants.
CONST1 = 'foo'
         ^^^^^

1 file inspected, 1 offense detected

RuboCop version

$ rubocop -V
0.45.0 (using Parser 2.3.1.4, running on ruby 2.0.0 universal.x86_64-darwin15)
@rrosenblum
Copy link
Contributor

The same issue holds true for other types of variables such as arrays and hashes. It is also true for classes that are instantiated as constants.

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 19, 2018

There hasn't been much activity on this ticket and our Core Team is spread too thin on so many tasks, so I'll just close it for the sake of having a cleaner lists of tasks to focus on. We'd gladly review a PR, but it's unlikely that'd we're going to tackle this ourselves in the foreseeable future.

@bbatsov bbatsov closed this as completed Sep 19, 2018
@rrosenblum
Copy link
Contributor

This will actually be taken care of by the Strict mode that I am working on in #6126.

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

No branches or pull requests

3 participants