Skip to content

Commit 27e91f2

Browse files
committed
Do not warn for unused variables on negative lines
Fixes [Bug #20788]
1 parent 18ce4f8 commit 27e91f2

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/prism.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -962,17 +962,19 @@ pm_locals_order(PRISM_ATTRIBUTE_UNUSED pm_parser_t *parser, pm_locals_t *locals,
962962
pm_constant_id_list_insert(list, (size_t) local->index, local->name);
963963

964964
if (warn_unused && local->reads == 0) {
965-
pm_constant_t *constant = pm_constant_pool_id_to_constant(&parser->constant_pool, local->name);
965+
if (pm_newline_list_line(&parser->newline_list, local->location.start, parser->start_line) >= 0) {
966+
pm_constant_t *constant = pm_constant_pool_id_to_constant(&parser->constant_pool, local->name);
966967

967-
if (constant->length >= 1 && *constant->start != '_') {
968-
PM_PARSER_WARN_FORMAT(
969-
parser,
970-
local->location.start,
971-
local->location.end,
972-
PM_WARN_UNUSED_LOCAL_VARIABLE,
973-
(int) constant->length,
974-
(const char *) constant->start
975-
);
968+
if (constant->length >= 1 && *constant->start != '_') {
969+
PM_PARSER_WARN_FORMAT(
970+
parser,
971+
local->location.start,
972+
local->location.end,
973+
PM_WARN_UNUSED_LOCAL_VARIABLE,
974+
(int) constant->length,
975+
(const char *) constant->start
976+
);
977+
}
976978
}
977979
}
978980
}

test/prism/result/warnings_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def test_unused_local_variables
259259

260260
refute_warning("def foo; bar = 1; tap { bar }; end")
261261
refute_warning("def foo; bar = 1; tap { baz = bar; baz }; end")
262+
263+
refute_warning("def foo; bar = 1; end", line: -2, compare: false)
262264
end
263265

264266
def test_void_statements

0 commit comments

Comments
 (0)