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

FrozenStringLiteralComment's 'when_needed' style is confusing #2739

Closed
mvz opened this issue Jan 29, 2016 · 9 comments · Fixed by #2802
Closed

FrozenStringLiteralComment's 'when_needed' style is confusing #2739

mvz opened this issue Jan 29, 2016 · 9 comments · Fixed by #2802

Comments

@mvz
Copy link
Contributor

mvz commented Jan 29, 2016

As I understand it, the 'when_needed' style only requires a frozen string literal comment if a call to String#freeze is present in the file. This seems weird, since once the comment is in place, the call is no longer needed.

Am I missing something?

$ rubocop -V
0.36.0 (using Parser 2.3.0.2, running on ruby 2.3.0 x86_64-linux)
@alexdowad
Copy link
Contributor

@rrosenblum Any comment?

@rrosenblum
Copy link
Contributor

@mvz sorry for not getting back to you sooner. You are right about the frozen string literal comment not being needed in a file if the file contains a call to String#freeze. I was trying to mimic the Style/Encoding cop, and I made use of some weak assumptions with that configuration that may not really make sense. The idea was to try and only add the comment to files that may need it. Targeting Ruby 2.3+ and files that contain a call to String#freeze or String#<< were the only criteria that we can check for. Even then, this does not catch all situations.

In hind site, I think this configuration is going to prove fairly useless and add more confusion than positive gain. I will put in a PR to remove both configurations, make the code function as the always configuration, and leave the cop disabled by default.

I don't know how many users actually dig through the documentation and read up on disabled cops. It might be nice if we had a way to enable this cop by default if the target Ruby version is 2.3+ or provide a message suggesting that this cop be enabled.

@alexdowad
Copy link
Contributor

@rrosenblum You could just ensure that the file has at least one string literal in it.

@rrosenblum
Copy link
Contributor

You could just ensure that the file has at least one string literal in it.

It seems so simple. I like it. What do you think is the percentage of files that contain no string literals compared to those that do? Is it worth it?

@alexdowad
Copy link
Contributor

(Shrugs)

@rrosenblum
Copy link
Contributor

Haha. We can wait for @bbatsov and others to chime in with their opinions.

@mvz
Copy link
Contributor Author

mvz commented Feb 5, 2016

@rrosenblum thanks for the explanation. I'll leave the cop disabled for now and keep an eye on the developments.

@rrosenblum
Copy link
Contributor

Welcome. The always configuration works well if you want to use the cop.

@mvz
Copy link
Contributor Author

mvz commented Feb 6, 2016

@rrosenblum yes, I actually went ahead and used always instead. Worked like a charm.

rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Feb 8, 2016
bbatsov added a commit that referenced this issue Feb 9, 2016
…eeded

[Fix #2739] FrozenStringLiteralComment when_needed adds a comment to all files targeted to Ruby 2.3+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants