KEMBAR78
auto-correct changes behavior of method return value · Issue #14544 · rubocop/rubocop · GitHub
Skip to content

auto-correct changes behavior of method return value #14544

@ngoto

Description

@ngoto

After auto-correct the following script, the corrected script returns incorrect value because lacking the line corresponding to the original script's return @server line, and changing script behavior.

class Report
  def server=(str)
    @server = str
    begin
      m = Servers.const_get(@server.capitalize)
    rescue NameError
      m = nil
    end
    if m and !(self.is_a?(m)) then
      self.class.class_eval { include m }
    end
    return @server
  end
end

The script is auto-corrected to the following.

class Report
  def server=(str)
    @server = str
    begin
      m = Servers.const_get(@server.capitalize)
    rescue NameError
      m = nil
    end
    self.class.class_eval { include m } if m and !is_a?(m)
  end
end

The auto-corrected script should contain a line corresponding to original return @server line, but it lacks the line.

.rubocop.yml:

AllCops:
  NewCops: enable

Expected behavior

rubocop -a does not change method's return value.

Actual behavior

autocorrected script lacks line that is corresponding to original return @server line and thus the behavior of method return value is changed.

diff of original and auto-corrected scripts:

--- tmp13.rb.ORIG       2025-09-20 02:47:40.383765000 +0900
+++ tmp13.rb    2025-09-20 02:55:20.003943000 +0900
@@ -1,15 +1,11 @@
 class Report
   def server=(str)
     @server = str
     begin
       m = Servers.const_get(@server.capitalize)
     rescue NameError
       m = nil
     end
-    if m and !(self.is_a?(m)) then
-      self.class.class_eval { include m }
-    end
-    return @server
+    self.class.class_eval { include m } if m and !is_a?(m)
   end
 end
-

RuboCop log:

$ rubocop -d -a tmp13.rb
For /XXXX/202509: configuration from /XXXX/202509/.rubocop.yml
Default configuration from /YYYY/amd64/3.4.6/lib/ruby/gems/3.4.0/gems/rubocop-1.80.2/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /XXXX/202509/tmp13.rb
Loading cache from /XXXX/.cache/rubocop_cache/178c5ca2f83a8cb4cb3064ce06de0eff8fa19ca9/6d7a3b621ca1730e04accd938619e4bdab66cfb1/0cbb5940a4ee9274729bcfdfe83253fc16f30d47
W

Offenses:

tmp13.rb:1:1: C: Style/Documentation: Missing top-level documentation comment for class Report.
class Report
^^^^^^^^^^^^
tmp13.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
class Report
^
tmp13.rb:9:5: C: [Corrected] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
    if m and !(self.is_a?(m)) then
    ^^
tmp13.rb:9:15: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a method call.
    if m and !(self.is_a?(m)) then
              ^^^^^^^^^^^^^^^
tmp13.rb:9:16: C: [Corrected] Style/RedundantSelf: Redundant self detected.
    if m and !(self.is_a?(m)) then
               ^^^^
tmp13.rb:9:31: C: [Corrected] Style/MultilineIfThen: Do not use then for multi-line if.
    if m and !(self.is_a?(m)) then
                              ^^^^
tmp13.rb:9:46: C: [Correctable] Style/AndOr: Use && instead of and.
    self.class.class_eval { include m } if m and !is_a?(m)
                                             ^^^
tmp13.rb:9:51: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a method call.
    self.class.class_eval { include m } if m and !(self.is_a?(m))
                                                  ^^^^^^^^^^^^^^^
tmp13.rb:9:52: C: [Corrected] Style/RedundantSelf: Redundant self detected.
    self.class.class_eval { include m } if m and !(self.is_a?(m))
                                                   ^^^^
tmp13.rb:10:5: W: [Corrected] Lint/Void: Variable @server used in void context.
    @server
    ^^^^^^^
tmp13.rb:12:5: C: [Corrected] Style/RedundantReturn: Redundant return detected.
    return @server
    ^^^^^^
tmp13.rb:15:1: C: [Corrected] Layout/TrailingEmptyLines: 1 trailing blank lines detected.

1 file inspected, 12 offenses detected, 9 offenses corrected, 2 more offenses can be corrected with `rubocop -A`
Finished in 0.34026799799175933 seconds
$

Steps to reproduce the problem

See the above scripts and RuboCop exec logs.

RuboCop version

$ rubocop -V
1.80.2 (using Parser 3.3.9.0, rubocop-ast 1.46.0, analyzing as Ruby 2.7, running on ruby 3.4.6) [x86_64-linux]
$ ruby -v
ruby 3.4.6 (2025-09-16 revision dbd83256b1) +PRISM [x86_64-linux]
$ cat .rubocop.yml
AllCops:
  NewCops: enable
$

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