Rubocop: Rubocop does not flag undeclared variables

Created on 22 Jul 2019  路  2Comments  路  Source: rubocop-hq/rubocop

Given the following code:

def x
  return if a == 1

  y = a + b
  c
end

Rubocop only flags a single issue:

Lint/UselessAssignment: Useless assignment to variable - 'y'

This can lead to subtle errors being raised when the above method is actually run.


Expected behavior

Rubocop should flag that variables 'a', 'b', and 'c' are all undeclared.

Actual behavior

Rubocop does not flag any undeclared variables

Steps to reproduce the problem

Using the following code:

def x
  return if a == 1

  y = a + b
  c
end

RuboCop version

$ [bundle exec] rubocop -V
0.71.0 (using Parser 2.6.3.0, running on ruby 2.6.0 x86_64-darwin18)

Most helpful comment

This is hard because variables are not declared in Ruby. In the example above, a, b, and c are parsed as method calls. And those methods could be inherited and therefore defined in some other source file, making this valid and error-free code.

All 2 comments

This is hard because variables are not declared in Ruby. In the example above, a, b, and c are parsed as method calls. And those methods could be inherited and therefore defined in some other source file, making this valid and error-free code.

You are correct. Thanks for the clarification.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cabello picture cabello  路  3Comments

deivid-rodriguez picture deivid-rodriguez  路  3Comments

bquorning picture bquorning  路  3Comments

lepieru picture lepieru  路  3Comments

bbugh picture bbugh  路  3Comments