Skip to content

Commit

Permalink
Sync upstream ruby/ruby and remove redundant return
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jul 17, 2024
1 parent 165a1a0 commit 67c2a07
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 193 deletions.
6 changes: 0 additions & 6 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3455,7 +3450,6 @@ nodes:
retry
^^^^^
- name: ReturnNode
flags: ReturnNodeFlags
fields:
- name: keyword_loc
type: location
Expand Down
111 changes: 0 additions & 111 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
Expand Down
73 changes: 0 additions & 73 deletions test/prism/result/redundant_return_test.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/parse_line_defn_complex.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/parse_line_return.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/prism/snapshots/whitequark/ruby_bug_9669.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 67c2a07

Please sign in to comment.