Skip to content

Style/GuardClause autocorrect results in non-running code #13390

@ilvez

Description

@ilvez

Expected behavior

Since Style/GuardClause is marked as safely autocorrectable rule, it should not replace working code with non-working.

Actual behavior

Please don't mind that the code itself is ugly, I'm working with legacy codebase and trying to apply Rubocop to setup. I created an example based on real-life code.

Non-functioning code is generated when rubocop -a on specific code:

% rubocop -a
Inspecting 1 file
W

Offenses:

test.rb:10:5: C: [Corrected] Style/GuardClause: Use a guard clause (return a if (a = b).positive?) instead of wrapping the code inside a conditional expression.
    if (a = b).positive?
    ^^
test.rb:10:17: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a method call.
    return a if (b).positive?
                ^^^
test.rb:10:18: W: [Corrected] Lint/UselessAssignment: Useless assignment to variable - a.
    return a if (a = b).positive?
                 ^
test.rb:11:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
test.rb:12:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
test.rb:12:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
test.rb:13:7: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
      sleep 1
      ^^^^^^^
test.rb:14:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
test.rb:15:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.

1 file inspected, 9 offenses detected, 9 offenses corrected

% git diff
diff --git a/test.rb b/test.rb
index 94fc3bc..14ddb4f 100644
--- a/test.rb
+++ b/test.rb
@@ -5,11 +5,9 @@ def b
 end
 
 def test
-  if (a = b).positive?
-    return a
-  else
-    sleep 1
-  end
+  return a if b.positive?
+
+  sleep 1
 
   nil
 end

Steps to reproduce the problem

NB! Look at rubocop check output. It suggests correct code in the error report, but autofix replaces it with incorrect version.

% cat test.rb 
# frozen_string_literal: true

# top-level comment
class A
  def b
    1
  end

  def test
    if (a = b).positive?
      return a
    else
      sleep 1
    end

    nil
  end
end

puts(A.new.test)

% ruby test.rb 
1

% rubocop test.rb 
Inspecting 1 file
C

Offenses:

test.rb:10:5: C: [Correctable] Style/GuardClause: Use a guard clause (return a if (a = b).positive?) instead of wrapping the code inside a conditional expression.
    if (a = b).positive?
    ^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

% rubocop -a
Inspecting 1 file
W

Offenses:

test.rb:10:5: C: [Corrected] Style/GuardClause: Use a guard clause (return a if (a = b).positive?) instead of wrapping the code inside a conditional expression.
    if (a = b).positive?
    ^^
test.rb:10:17: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a method call.
    return a if (b).positive?
                ^^^
test.rb:10:18: W: [Corrected] Lint/UselessAssignment: Useless assignment to variable - a.
    return a if (a = b).positive?
                 ^
test.rb:11:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
test.rb:12:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
test.rb:12:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
test.rb:13:7: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
      sleep 1
      ^^^^^^^
test.rb:14:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
test.rb:15:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.

1 file inspected, 9 offenses detected, 9 offenses corrected

% rubocop test.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

% ruby test.rb   
test.rb:10:in `test': undefined local variable or method `a' for an instance of A (NameError)

    return a if b.positive?
           ^
	from test.rb:18:in `<main>'

% git diff
diff --git a/test.rb b/test.rb
index b830448..957ee23 100644
--- a/test.rb
+++ b/test.rb
@@ -7,11 +7,9 @@ class A
   end
 
   def test
-    if (a = b).positive?
-      return a
-    else
-      sleep 1
-    end
+    return a if b.positive?
+
+    sleep 1
 
     nil
   end

RuboCop version

% rubocop -V
1.67.0 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 2.7, running on ruby 3.3.4) [x86_64-linux]

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy