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 #6777] Fix a false positive for `Style/TrivialAccessors` #6795

Merged
merged 1 commit into from Mar 1, 2019

Conversation

Projects
None yet
2 participants
@koic
Copy link
Member

koic commented Feb 28, 2019

Fixes #6777.

This PR fixes a false positive for Style/TrivialAccessors when using reader / writer at the top level.

The following is a reproduction code.

# example.rb
def foo
  @foo
end
% rubocop example.rb --only Style/TrivialAccessors -a
Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/TrivialAccessors: Use attr_reader
to define trivial reader methods.
def response
^^^

1 file inspected, 1 offense detected, 1 offense corrected

An error occurs in the auto-corrected code.

% git diff
diff --git a/example.rb b/example.rb
index b26fec0..1868b1d 100644
--- a/example.rb
+++ b/example.rb
@@ -1,5 +1,3 @@
-def response
-  @response
-end
+attr_reader :response
% ruby example.rb
Traceback (most recent call last):
example.rb:1:in `<main>': undefined method `attr_reader' for main:Object (NoMethodError)

This also occurs with writer. This PR will allow these cases.


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 bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.
@bar = bar
end
RUBY
end

This comment has been minimized.

@koic

koic Feb 28, 2019

Author Member

📝 These two test cases are the main changes.

@koic koic changed the title [Fix #4840] Fix a false positive for `Style/TrivialAccessors` [Fix #6777] Fix a false positive for `Style/TrivialAccessors` Feb 28, 2019

[Fix #6777] Fix a false positive for `Style/TrivialAccessors`
Fixes #6777.

This PR fixes a false positive for `Style/TrivialAccessors` when
using reader / writer at the top level.

The following is a reproduction code.

```ruby
# example.rb
def foo
  @foo
end
```

```console
% rubocop example.rb --only Style/TrivialAccessors -a
Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/TrivialAccessors: Use attr_reader
to define trivial reader methods.
def response
^^^

1 file inspected, 1 offense detected, 1 offense corrected
```

An error occurs in the auto-corrected code.

```diff
% git diff
diff --git a/example.rb b/example.rb
index b26fec0..1868b1d 100644
--- a/example.rb
+++ b/example.rb
@@ -1,5 +1,3 @@
-def response
-  @response
-end
+attr_reader :response
```

```console
% ruby example.rb
Traceback (most recent call last):
example.rb:1:in `<main>': undefined method `attr_reader' for main:Object (NoMethodError)
```

This also occurs with writer. This PR will allow these cases.

@koic koic force-pushed the koic:fix_false_positive_for_trivial_accessors branch from 9d3a77d to c30ecf0 Feb 28, 2019

@Drenmi

Drenmi approved these changes Mar 1, 2019

Copy link
Collaborator

Drenmi left a comment

🚀

@koic koic merged commit abee2ca into rubocop-hq:master Mar 1, 2019

27 checks passed

ci/circleci: cc-setup Your tests passed on CircleCI!
Details
ci/circleci: cc-upload-coverage Your tests passed on CircleCI!
Details
ci/circleci: documentation-checks Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-rubocop Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.2-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.2-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.2-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.3-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.3-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.3-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-rubocop-with-jit Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-spec-with-jit Your tests passed on CircleCI!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details

@koic koic deleted the koic:fix_false_positive_for_trivial_accessors branch Mar 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.