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 a false positive of Style/FormatStringToken with unrelated format method call #5496

Merged
merged 1 commit into from
Jan 24, 2018

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Jan 22, 2018

Ref: #5483

Problem

Currently Style/FormatStringToken cop adds a falsy offense.

Time.now.strftime('%Y-%m-%d-%H-%M-%S')
format
$ rubocop
Inspecting 1 file
C

Offenses:

test.rb:1:26: C: Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over unannotated tokens (like %s).
Time.now.strftime('%Y-%m-%d-%H-%M-%S')
                         ^^

1 file inspected, 1 offense detected

But the cop does not add any offenses to the following code.

Time.now.strftime('%Y-%m-%d-%H-%M-%S')
$ rubocop
Inspecting 1 file
.

1 file inspected, no offenses detected

Cause

The cause is here.
https://github.com/bbatsov/rubocop/blob/86476f13e8e12e370f84911d82bef8638f3fdf96/lib/rubocop/cop/style/format_string_token.rb#L70-L77

The node.ancestors.last is a root node of ast. In other words, it equals processed_source.ast. So the code checks format method existence from whole a file.

Solution

Check format method existence only from ancestors of the string node.

Replace this text with a summary of the changes in your PR.
The more detailed you are, the better.


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.

…mat` call

Problem
===

Currently `Style/FormatStringToken` cop adds a falsy offense.

```ruby
Time.now.strftime('%Y-%m-%d-%H-%M-%S')
format
```

```bash
$ rubocop
Inspecting 1 file
C

Offenses:

test.rb:1:26: C: Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over unannotated tokens (like %s).
Time.now.strftime('%Y-%m-%d-%H-%M-%S')
                         ^^

1 file inspected, 1 offense detected
```

But the cop does not add any offenses to the following code.

```ruby
Time.now.strftime('%Y-%m-%d-%H-%M-%S')
```

```
$ rubocop
Inspecting 1 file
.

1 file inspected, no offenses detected
```

Cause
===

The cause is here.
https://github.com/bbatsov/rubocop/blob/86476f13e8e12e370f84911d82bef8638f3fdf96/lib/rubocop/cop/style/format_string_token.rb#L70-L77

```ruby
def includes_format_methods?(node)
  root_node = node.ancestors.last
  return unless root_node

  root_node.descendants.any? do |desc_node|
    FORMAT_STRING_METHODS.include?(desc_node.method_name)
  end
end
```

The `node.ancestors.last` is a root node of ast. In other words, it equals `processed_source.ast`. So the code checks `format` method existence from whole a file.

Solution
====

Check `format` method existence only from ancestors of the string node.
@pocke pocke force-pushed the FormatStringToken branch from db285da to 9d3075c Compare January 22, 2018 14:57
@bbatsov bbatsov merged commit df1bb42 into rubocop:master Jan 24, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented Jan 24, 2018

👍

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