Skip to content

Style/SingleLineMethods safe autocorrect results in incorrect code #14367

@a-lavis

Description

@a-lavis

Expected behavior

When running rubocop -a foo.rb with the following .rubocop.yml:

AllCops:
  NewCops: enable
  TargetRubyVersion: 3.4

and foo.rb is the following file:

# frozen_string_literal: true

foo = Object.new
def foo.bar; 'baz'; end
puts foo.bar

I expect RuboCop to maintain the existing behavior when safe autocorrecting.

Before running rubocop -a foo.rb, running ruby foo.rb just outputs:

baz

Actual behavior

After running rubocop -a foo.rb, foo.rb is:

# frozen_string_literal: true

foo = Object.new
def bar = 'baz'
puts foo.bar

Now running ruby foo.rb outputs:

foo.rb:5:in '<main>': private method 'bar' called for an instance of Object (NoMethodError)

puts foo.bar
        ^^^^

Steps to reproduce the problem

Run rubocop -a foo.rb where foo.rb is the above file.

RuboCop version

> rubocop -V
1.78.0 (using Parser 3.3.8.0, Prism 1.4.0, rubocop-ast 1.45.1, analyzing as Ruby 3.4, running on ruby 3.4.4) [arm64-darwin24]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions