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

Fix falsy offense for Style/RedundantSelf with pseudo variables #5603

Merged
merged 1 commit into from
Feb 26, 2018

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Feb 26, 2018

Problem

RuboCop says "Redundant self detected" for self.__FILE__, but the self cannot be omitted.

class A
  def __FILE__
    '__FILE__ method'
  end

  def __LINE__
    '__LINE__ method'
  end

  def __ENCODING__
    '__ENCODING__ method'
  end

  def a
    p self.__FILE__
    p __FILE__
    p self.__LINE__
    p __LINE__
    p self.__ENCODING__
    p __ENCODING__
  end
end

A.new.a
$ ruby test.rb
"__FILE__ method"
"test.rb"
"__LINE__ method"
18
"__ENCODING__ method"
#<Encoding:UTF-8>
$ rubocop --only RedundantSelf
Inspecting 1 file
C

Offenses:

test.rb:15:7: C: Style/RedundantSelf: Redundant self detected.
    p self.__FILE__
      ^^^^^^^^^^^^^
test.rb:17:7: C: Style/RedundantSelf: Redundant self detected.
    p self.__LINE__
      ^^^^^^^^^^^^^
test.rb:19:7: C: Style/RedundantSelf: Redundant self detected.
    p self.__ENCODING__
      ^^^^^^^^^^^^^^^^^

1 file inspected, 3 offenses detected

Solution

Add pseudo variables to the keyword list.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run rake default or rake parallel. It executes all tests and RuboCop for itself, and generates the documentation.

@pocke pocke force-pushed the redundant-self-special-variable branch from d0b038d to 0ad9f6f Compare February 26, 2018 09:43
Problem
=====

RuboCop says "Redundant self detected" for `self.__FILE__`, but the `self` cannot be omitted.

```ruby
class A
  def __FILE__
    '__FILE__ method'
  end

  def __LINE__
    '__LINE__ method'
  end

  def __ENCODING__
    '__ENCODING__ method'
  end

  def a
    p self.__FILE__
    p __FILE__
    p self.__LINE__
    p __LINE__
    p self.__ENCODING__
    p __ENCODING__
  end
end

A.new.a
```

```bash
$ ruby test.rb
"__FILE__ method"
"test.rb"
"__LINE__ method"
18
"__ENCODING__ method"
 #<Encoding:UTF-8>
```

```bash
$ rubocop --only RedundantSelf
Inspecting 1 file
C

Offenses:

test.rb:15:7: C: Style/RedundantSelf: Redundant self detected.
    p self.__FILE__
      ^^^^^^^^^^^^^
test.rb:17:7: C: Style/RedundantSelf: Redundant self detected.
    p self.__LINE__
      ^^^^^^^^^^^^^
test.rb:19:7: C: Style/RedundantSelf: Redundant self detected.
    p self.__ENCODING__
      ^^^^^^^^^^^^^^^^^

1 file inspected, 3 offenses detected
```

Solution
===

Add pseudo variables to the keyword list.
Copy link
Contributor

@garettarrowood garettarrowood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me.

@bbatsov bbatsov merged commit 7e8d087 into rubocop:master Feb 26, 2018
@pocke pocke deleted the redundant-self-special-variable branch February 27, 2018 01:31
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

Successfully merging this pull request may close these issues.

3 participants