-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Received malformed format string ArgumentError from rubocop #42
Comments
What code were you checking with rubocop when this error occurred? Can you investigate if there's a specific violation in the checked code that triggers this bug? That would help a lot. |
Here are the last few lines before the error occurred : C: 82: Prefer single-quoted strings when you don't need string def self.get_attributes(hostname, attrs = ['puppetVar', 'environment'],
Here is the snippet of code it was processing. The line i have *'d is line On Thu, Apr 11, 2013 at 3:42 PM, Jonas Arvidsson
|
I'm sorry but I have not been able to reproduce the error using your code snippet with ruby-1.9.3-p392 and rubocop-0.4.0. The error means that ruby found something in a format string that it couldn't process, in this case a percent sign followed by a left square bracket. Line 17 in "#{encode_severity}:%3d: #{message}" % [line_number] which means that either Is it possible that your installation of rubocop is corrupted? Because I'm drawing a blank. |
Is this problem still present with 0.4.6? |
Minimal test case: v = a_method(:sym) %{:foo => bar} This gives me In this case, changing the line to: h = a_method(:sym) % {:foo => bar} by adding a single space makes rubocop not crash. Another test case for a similar error: a_method(:sym) %{:foo => bar} That last one will give |
Anyway, executing the following code directly with ruby throws the same errors, therefore it's the parser itself that rejects a malformed ruby file. Case 1: #!/usr/bin/env ruby
def a_method(s)
"abc %(foo)s def"
end
bar = "bar"
h = a_method(:sym) % {:foo => bar} Case 2: #!/usr/bin/env ruby
def a_method(s)
"abc %(foo)s def"
end
bar = "bar"
a_method(:sym) % {:foo => bar} I'm not sure rubocop should crash like this, but rather catch the parsing error and not try to analyze the file, display output similar to the following, and continue with the next one.
I don't know if it's even possible, but I venture this is how I'd like rubocop to behave in this case. |
I think we should go with the second option - intercepting the exception and converting it to an offence. Would you like to take a stab at this, @lloeki? |
Hmm, I tried all the suggest code snippets and none of them seems to break RuboCop |
|
Wow, I'm always running |
Just tested it. Looks fine. |
This PR resolves the following error when running `rake changelog:merge` in RuboCop Rails. So it will prevent the same error from occurring in this repository. ```conosle $ bundle exec rake changelog:merge rake aborted! NoMethodError: undefined method `[]' for nil /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:139:in `block in contributors' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `each' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `flat_map' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `contributors' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:132:in `new_contributor_lines' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:126:in `merge_content' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:115:in `merge!' tasks/changelog.rake:21:in `block (2 levels) in <top (required)>' /Users/koic/.rbenv/versions/3.3.0-dev/bin/bundle:25:in `load' /Users/koic/.rbenv/versions/3.3.0-dev/bin/bundle:25:in `<main>' Tasks: TOP => changelog:merge (See full trace by running task with --trace) ``` This error was caused by the absence of a space after the period: ```diff -* [#42](rubocop/rubocop-rails#42): Fix an error for `Rails/RedundantActiveRecordAllMethod`.([contributor_name]) +* [#42](rubocop/rubocop-rails#42): Fix an error for `Rails/RedundantActiveRecordAllMethod`. ([contributor_name]) ``` The added test verifies that there is a single space between the period and the parentheses. And tweaks changelog entries NOTE: For compatibility with outdated formats, if there's no contributor name, it checks that the line ends with a period.
'/Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cop/offence.rb:17:in
%': malformed format string - %[ (ArgumentError)from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cop/offence.rb:17:in
to_s' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:10:in
join'from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:10:in
generate' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:14:in
display'from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:67:in
block in run' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:48:in
each'from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:48:in
run' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/bin/rubocop:12:in
block in <top (required)>'from /Users/michael.hogg/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/benchmark.rb:295:in
realtime' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/bin/rubocop:11:in
<top (required)>'from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/rubocop:19:in
load' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/rubocop:19:in
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/ruby_noexec_wrapper:14:in
eval' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/ruby_noexec_wrapper:14:in
'`
The text was updated successfully, but these errors were encountered: