Repository: ruby/prism
Pull request #2939 (2024-07-17 19:16:46)

Sync upstream ruby/ruby and remove redundant return


Patch:
diff --git a/config.yml b/config.yml
index 321264e6..ad5d6337 100644
--- a/config.yml
+++ b/config.yml
@@ -735,11 +735,6 @@ flags:
       - name: FORCED_US_ASCII_ENCODING
         comment: "internal bytes forced the encoding to US-ASCII"
     comment: Flags for regular expression and match last line nodes.
-  - name: ReturnNodeFlags
-    values:
-      - name: REDUNDANT
-        comment: "a return statement that is redundant because it is the last statement in a method"
-    comment: Flags for return nodes.
   - name: ShareableConstantNodeFlags
     values:
       - name: LITERAL
@@ -3455,7 +3450,6 @@ nodes:
           retry
           ^^^^^
   - name: ReturnNode
-    flags: ReturnNodeFlags
     fields:
       - name: keyword_loc
         type: location
diff --git a/src/prism.c b/src/prism.c
index 2e1d90ad..427ac49e 100644
--- a/src/prism.c
+++ b/src/prism.c
@@ -3795,113 +3795,6 @@ pm_def_node_receiver_check(pm_parser_t *parser, const pm_node_t *node) {
     }
 }
 
-/**
- * When a method body is created, we want to check if the last statement is a
- * return or a statement that houses a return. If it is, then we want to mark
- * that return as being redundant so that we can compile it differently but also
- * so that we can indicate that to the user.
- */
-static void
-pm_def_node_body_redundant_return(pm_node_t *node) {
-    switch (PM_NODE_TYPE(node)) {
-        case PM_RETURN_NODE:
-            node->flags |= PM_RETURN_NODE_FLAGS_REDUNDANT;
-            break;
-        case PM_BEGIN_NODE: {
-            pm_begin_node_t *cast = (pm_begin_node_t *) node;
-
-            if (cast->statements != NULL && cast->else_clause == NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->statements);
-            }
-            break;
-        }
-        case PM_STATEMENTS_NODE: {
-            pm_statements_node_t *cast = (pm_statements_node_t *) node;
-
-            if (cast->body.size > 0) {
-                pm_def_node_body_redundant_return(cast->body.nodes[cast->body.size - 1]);
-            }
-            break;
-        }
-        case PM_IF_NODE: {
-            pm_if_node_t *cast = (pm_if_node_t *) node;
-
-            if (cast->statements != NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->statements);
-            }
-
-            if (cast->consequent != NULL) {
-                pm_def_node_body_redundant_return(cast->consequent);
-            }
-            break;
-        }
-        case PM_UNLESS_NODE: {
-            pm_unless_node_t *cast = (pm_unless_node_t *) node;
-
-            if (cast->statements != NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->statements);
-            }
-
-            if (cast->consequent != NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->consequent);
-            }
-            break;
-        }
-        case PM_ELSE_NODE: {
-            pm_else_node_t *cast = (pm_else_node_t *) node;
-
-            if (cast->statements != NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->statements);
-            }
-            break;
-        }
-        case PM_CASE_NODE: {
-            pm_case_node_t *cast = (pm_case_node_t *) node;
-            pm_node_t *condition;
-
-            PM_NODE_LIST_FOREACH(&cast->conditions, index, condition) {
-                pm_def_node_body_redundant_return(condition);
-            }
-
-            if (cast->consequent != NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->consequent);
-            }
-            break;
-        }
-        case PM_WHEN_NODE: {
-            pm_when_node_t *cast = (pm_when_node_t *) node;
-
-            if (cast->statements != NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->statements);
-            }
-            break;
-        }
-        case PM_CASE_MATCH_NODE: {
-            pm_case_match_node_t *cast = (pm_case_match_node_t *) node;
-            pm_node_t *condition;
-
-            PM_NODE_LIST_FOREACH(&cast->conditions, index, condition) {
-                pm_def_node_body_redundant_return(condition);
-            }
-
-            if (cast->consequent != NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->consequent);
-            }
-            break;
-        }
-        case PM_IN_NODE: {
-            pm_in_node_t *cast = (pm_in_node_t *) node;
-
-            if (cast->statements != NULL) {
-                pm_def_node_body_redundant_return((pm_node_t *) cast->statements);
-            }
-            break;
-        }
-        default:
-            break;
-    }
-}
-
 /**
  * Allocate and initialize a new DefNode node.
  */
@@ -3934,10 +3827,6 @@ pm_def_node_create(
         pm_def_node_receiver_check(parser, receiver);
     }
 
-    if (body != NULL) {
-        pm_def_node_body_redundant_return(body);
-    }
-
     *node = (pm_def_node_t) {
         {
             .type = PM_DEF_NODE,


Test patch:
diff --git a/test/prism/result/redundant_return_test.rb b/test/prism/result/redundant_return_test.rb
deleted file mode 100644
index 3b20aeba..00000000
--- a/test/prism/result/redundant_return_test.rb
+++ /dev/null
@@ -1,73 +0,0 @@
-# frozen_string_literal: true
-
-require_relative "../test_helper"
-
-module Prism
-  class RedundantReturnTest < TestCase
-    def test_statements
-      assert_redundant_return("def foo; return; end")
-      refute_redundant_return("def foo; return; 1; end")
-    end
-
-    def test_begin_implicit
-      assert_redundant_return("def foo; return; rescue; end")
-      refute_redundant_return("def foo; return; 1; rescue; end")
-      refute_redundant_return("def foo; return; rescue; else; end")
-    end
-
-    def test_begin_explicit
-      assert_redundant_return("def foo; begin; return; rescue; end; end")
-      refute_redundant_return("def foo; begin; return; 1; rescue; end; end")
-      refute_redundant_return("def foo; begin; return; rescue; else; end; end")
-    end
-
-    def test_if
-      assert_redundant_return("def foo; return if bar; end")
-    end
-
-    def test_unless
-      assert_redundant_return("def foo; return unless bar; end")
-    end
-
-    def test_else
-      assert_redundant_return("def foo; if bar; baz; else; return; end; end")
-    end
-
-    def test_case_when
-      assert_redundant_return("def foo; case bar; when baz; return; end; end")
-    end
-
-    def test_case_else
-      assert_redundant_return("def foo; case bar; when baz; else; return; end; end")
-    end
-
-    def test_case_match_in
-      assert_redundant_return("def foo; case bar; in baz; return; end; end")
-    end
-
-    def test_case_match_else
-      assert_redundant_return("def foo; case bar; in baz; else; return; end; end")
-    end
-
-    private
-
-    def assert_redundant_return(source)
-      assert find_return(source).redundant?
-    end
-
-    def refute_redundant_return(source)
-      refute find_return(source).redundant?
-    end
-
-    def find_return(source)
-      queue = [Prism.parse(source).value]
-
-      while (current = queue.shift)
-        return current if current.is_a?(ReturnNode)
-        queue.concat(current.compact_child_nodes)
-      end
-
-      flunk "Could not find return node in #{node.inspect}"
-    end
-  end
-end
diff --git a/test/prism/snapshots/seattlerb/parse_line_defn_complex.txt b/test/prism/snapshots/seattlerb/parse_line_defn_complex.txt
index 2a91fc6b..fb4d1940 100644
--- a/test/prism/snapshots/seattlerb/parse_line_defn_complex.txt
+++ b/test/prism/snapshots/seattlerb/parse_line_defn_complex.txt
@@ -56,7 +56,7 @@
             │       │   ├── binary_operator: :*
             │       │   └── depth: 0
             │       └── @ ReturnNode (location: (4,2)-(4,10))
-            │           ├── flags: newline, redundant
+            │           ├── flags: newline
             │           ├── keyword_loc: (4,2)-(4,8) = "return"
             │           └── arguments:
             │               @ ArgumentsNode (location: (4,9)-(4,10))
diff --git a/test/prism/snapshots/seattlerb/parse_line_return.txt b/test/prism/snapshots/seattlerb/parse_line_return.txt
index 70a72867..14d18e71 100644
--- a/test/prism/snapshots/seattlerb/parse_line_return.txt
+++ b/test/prism/snapshots/seattlerb/parse_line_return.txt
@@ -27,7 +27,7 @@
             │           │   ├── flags: ∅
             │           │   └── body: (length: 1)
             │           │       └── @ ReturnNode (location: (3,10)-(3,19))
-            │           │           ├── flags: newline, redundant
+            │           │           ├── flags: newline
             │           │           ├── keyword_loc: (3,10)-(3,16) = "return"
             │           │           └── arguments:
             │           │               @ ArgumentsNode (location: (3,17)-(3,19))
diff --git a/test/prism/snapshots/whitequark/ruby_bug_9669.txt b/test/prism/snapshots/whitequark/ruby_bug_9669.txt
index 3d2e3056..c771c50c 100644
--- a/test/prism/snapshots/whitequark/ruby_bug_9669.txt
+++ b/test/prism/snapshots/whitequark/ruby_bug_9669.txt
@@ -29,7 +29,7 @@
         │   │   ├── flags: ∅
         │   │   └── body: (length: 1)
         │   │       └── @ ReturnNode (location: (2,0)-(2,6))
-        │   │           ├── flags: newline, redundant
+        │   │           ├── flags: newline
         │   │           ├── keyword_loc: (2,0)-(2,6) = "return"
         │   │           └── arguments: ∅
         │   ├── locals: [:b]