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

[Lint/NumericOperationWithConstantResult] incorrectly mistakes format string evaluations as numerical operations #13557

Closed
postmodern opened this issue Dec 7, 2024 · 0 comments · Fixed by #13577

Comments

@postmodern
Copy link

Recently rubocop 1.69.1 flagged an RSpec test which tested the output of a format string (ex: %.2x). However, rubocop mistook the format string evaluation using String#% as a numerical operation.

[Correctable] Lint/NumericOperationWithConstantResult: Numeric operation with a constant result detected.
            number_format % 0x01,
            ^^^^^^^^^^^^^^^^^^^^

Apparently, rubocop is smart enough to detect when a local variable was previously defined and that it is a String. However, in this context number_format was defined dynamically using RSpec's let().


Expected behavior

Ignore number_format; possibly because the variable name contains format or fmt?

Actual behavior

For /data/home/postmodern/code/ronin-rb/ronin-support: configuration from /data/home/postmodern/code/ronin-rb/ronin-support/.rubocop.yml
Inheriting configuration from /data/home/postmodern/code/ronin-rb/vendor/bundle/ruby/3.3.0/gems/rubocop-ronin-0.2.7/rubocop.yml
Default configuration from /data/home/postmodern/code/ronin-rb/vendor/bundle/ruby/3.3.0/gems/rubocop-1.69.1/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /data/home/postmodern/code/ronin-rb/ronin-support/spec/binary/unhexdump/parser_spec.rb
Loading cache from /home/postmodern/.cache/rubocop_cache/d5be16cfb188840558666dc46b3e0676a580509d/6d7a3b621ca1730e04accd938619e4bdab66cfb1/7e7cbcde3e0d8a0272b4d2510a07a0ba01da99ab

spec/binary/unhexdump/parser_spec.rb:320:13: W: [Correctable] Lint/NumericOperationWithConstantResult: Numeric operation with a constant result detected.
            number_format % 0x01,
            ^^^^^^^^^^^^^^^^^^^^

Steps to reproduce the problem

Gemfile:

source 'https://rubygems.org'

gem 'rubocop'

test.rb:

# frozen_string_literal: true

def fmt_string
  '%.2x'
end

fmt_string % 1
  1. bundle install
  2. bundle exec rubocop test.rb

RuboCop version

$ bundle exec rubocop -V
1.69.1 (using Parser 3.3.6.0, rubocop-ast 1.36.2, analyzing as Ruby 2.7, running on ruby 3.3.6) [x86_64-linux]
Earlopain added a commit to Earlopain/rubocop that referenced this issue Dec 12, 2024
Earlopain added a commit to Earlopain/rubocop that referenced this issue Dec 12, 2024
Earlopain added a commit to Earlopain/rubocop that referenced this issue Dec 13, 2024
…hConstantResult`

Subtracting could happen with an array
Modulo is also the string format operator
bbatsov pushed a commit that referenced this issue Dec 13, 2024
…ntResult`

Subtracting could happen with an array
Modulo is also the string format operator
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 a pull request may close this issue.

1 participant