@@ -1569,6 +1569,26 @@ pm_conditional_predicate_warn_write_literal(pm_parser_t *parser, const pm_node_t
15691569 }
15701570}
15711571
1572+ /* Return the statements of a group whose last expression supplies the predicate. */
1573+ static const pm_statements_node_t *
1574+ pm_conditional_predicate_statements(const pm_node_t *node) {
1575+ switch (PM_NODE_TYPE(node)) {
1576+ case PM_PARENTHESES_NODE: {
1577+ const pm_node_t *body = ((const pm_parentheses_node_t *) node)->body;
1578+ if (body != NULL && PM_NODE_TYPE_P(body, PM_STATEMENTS_NODE)) return (const pm_statements_node_t *) body;
1579+ break;
1580+ }
1581+ case PM_BEGIN_NODE: {
1582+ const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
1583+ if (cast->rescue_clause == NULL && cast->else_clause == NULL && cast->ensure_clause == NULL) return cast->statements;
1584+ break;
1585+ }
1586+ default:
1587+ break;
1588+ }
1589+ return NULL;
1590+ }
1591+
15721592/**
15731593 * The predicate of conditional nodes can change what would otherwise be regular
15741594 * nodes into specialized nodes. For example:
@@ -1582,7 +1602,18 @@ pm_conditional_predicate_warn_write_literal(pm_parser_t *parser, const pm_node_t
15821602 * predicate or writing a static literal as the predicate.
15831603 */
15841604static void
1585- pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_predicate_type_t type) {
1605+ pm_conditional_predicate(pm_parser_t *parser, pm_node_t *predicate, pm_conditional_predicate_type_t type) {
1606+ pm_node_t *node = predicate;
1607+ bool allow_flip_flop = true;
1608+ const pm_statements_node_t *statements;
1609+
1610+ while ((statements = pm_conditional_predicate_statements(node)) != NULL) {
1611+ /* A direct range becomes a flip-flop only when it is the sole
1612+ * expression in every enclosing group. Logical operands start fresh. */
1613+ if (!pm_statements_node_single_expression_p(statements)) allow_flip_flop = false;
1614+ node = statements->body.nodes[statements->body.size - 1];
1615+ }
1616+
15861617 switch (PM_NODE_TYPE(node)) {
15871618 case PM_AND_NODE: {
15881619 pm_and_node_t *cast = (pm_and_node_t *) node;
@@ -1596,25 +1627,8 @@ pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_pr
15961627 pm_conditional_predicate(parser, cast->right, PM_CONDITIONAL_PREDICATE_TYPE_CONDITIONAL);
15971628 break;
15981629 }
1599- case PM_PARENTHESES_NODE: {
1600- pm_parentheses_node_t *cast = (pm_parentheses_node_t *) node;
1601-
1602- if ((cast->body != NULL) && PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE)) {
1603- pm_statements_node_t *statements = (pm_statements_node_t *) cast->body;
1604- if (statements->body.size == 1) pm_conditional_predicate(parser, statements->body.nodes[0], type);
1605- }
1606-
1607- break;
1608- }
1609- case PM_BEGIN_NODE: {
1610- pm_begin_node_t *cast = (pm_begin_node_t *) node;
1611- if (cast->statements != NULL) {
1612- pm_statements_node_t *statements = cast->statements;
1613- if (statements->body.size == 1) pm_conditional_predicate(parser, statements->body.nodes[0], type);
1614- }
1615- break;
1616- }
16171630 case PM_RANGE_NODE: {
1631+ if (!allow_flip_flop) break;
16181632 pm_range_node_t *cast = (pm_range_node_t *) node;
16191633
16201634 if (cast->left != NULL) pm_conditional_predicate(parser, cast->left, PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP);
@@ -1664,7 +1678,8 @@ pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_pr
16641678 pm_parser_warn_conditional_predicate_literal(parser, node, type, PM_WARN_LITERAL_IN_CONDITION_VERBOSE, PM_STRING_LITERAL("symbol "));
16651679 break;
16661680 case PM_INTEGER_NODE:
1667- if (type == PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP) {
1681+ /* Only a bare integer endpoint is compared with the line number. */
1682+ if (type == PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP && node == predicate) {
16681683 if (!PM_PARSER_COMMAND_LINE_OPTION_E(parser)) {
16691684 pm_parser_warn_node(parser, node, PM_WARN_INTEGER_IN_FLIP_FLOP);
16701685 }
0 commit comments