Simplecov: Many "instance variable not initialized" warnings

Created on 25 Jan 2012  路  13Comments  路  Source: simplecov-ruby/simplecov

SimpleCov version 0.5.4, when used with ruby 1.9.3-p0, produces a series of warning:

lib/simplecov/configuration.rb:152: warning: instance variable @use_merging not initialized
lib/simplecov/configuration.rb:27: warning: instance variable @coverage_dir not initialized
lib/simplecov-html.rb:36: warning: instance variable @asset_output_path not initialized
lib/simplecov/configuration.rb:141: warning: instance variable @project_name not initialized
lib/simplecov/source_file.rb:86: warning: instance variable @lines not initialized
lib/simplecov/configuration.rb:84: warning: instance variable @nocov_token not initialized
lib/simplecov/source_file.rb:86: warning: instance variable @lines not initialized
lib/simplecov/source_file.rb:86: warning: instance variable @lines not initialized
lib/simplecov/result.rb:61: warning: instance variable @covered_lines not initialized
lib/simplecov/result.rb:73: warning: instance variable @missed_lines not initialized

All 13 comments

I don't think that Ruby 1.9.3 is the issue here as it builds fine on 1.9.3 on Travis CI (see http://travis-ci.org/#!/colszowka/simplecov/jobs/283900) and also works fine for me. Do you run Ruby with a very high warning level? Obviously your Ruby gives warnings for Ruby code that is perfectly valid (in my opinion) as accessing potentially uninitialized instance vars using @foo is a common use case in Ruby.

I am using the standard ruby 1.9.3 provided by rvm install ruby-1.9.3. It looks like $-W is 2, but I have not set it, so I suppose that it is the default value set by rspec.

Hm, for me $-W gives 1.

Please also check out http://www.ruby-forum.com/topic/199672 and http://stackoverflow.com/questions/2098742/ruby-instance-variable-not-initialized-warning for further info.

I'm not sure whether this should be fixed in SimpleCov as this by default does not cause warnings and it's perfectly valid ruby code. Initializing all those variables might be a bigger tradeoff for source code readbility than having it cause harmless warnings in verbose ruby mode. Keeping this open for now.

For some of these instance variable the code was return @var unless @var.nil?. If you use normal variable instead of instance variables (return var unless var.nil?) you will get a fatal error.

In my pull release I did not fix the @use_merging code. I cannot understand what the code wanted to do and any of my solutions resulted in errors being raised by the test suite.

Could you have a look at #use_merging it to make sure that it is actually doing what it is supposed to do?

In my case the default warning level seems to be 2:

$ ruby --help | grep warn
  -w              turn warnings on for your script
  -W[level]       set warning level; 0=silence, 1=medium, 2=verbose (default)

Simplecov should be able to run without generating Ruby warnings. With simplecov triggering Ruby warnings, it makes it impossible for library authors using simplecov to measure both coverage and ensure their own code is free of warnings at the same time. Rails and RSpec have both been able to remove all warnings from their own large code bases.

@colszowka is there a problem with my pull request? Should I change it somehow? The commits in it fix most of these warning with minimal code changes.

What @cgriego said.

@gioele, it might help to set the warning level explicitely in the rake task, so the effect of your changes is more clearly seen.

@mvz, are you referring to my pull request #108? I am using the maximum warning level and the changes contained in that pull request remove most of the warnings. It also passes all tests in the test suit cleanly.

@gioele, yes, I was commenting on #108.

I meant adding something like t.ruby_opts += ["-W2"] to the task definition in the Rakefile, to make sure that everyone else running the tests also uses the maximum warning level, and hence sees the effect of your changes.

How are you currently setting the warning level?

Ok guys, merged @mvz 's pull request, so the warnings should be reduced now. Although I personally think Ruby is being slightly ridiculous with this particular warning I can see that people running their test suite at a higher warning level than I do don't want a test tool like simplecov to mess up their beautiful warning-free output.

v0.6.4 should be released today or tomorrow. Thanks for your patience and effort!

Was this page helpful?
0 / 5 - 0 ratings