Skip to content

Commit

Permalink
Do not warn for unused variables on negative lines
Browse files Browse the repository at this point in the history
Fixes [Bug #20788]
  • Loading branch information
kddnewton committed Oct 10, 2024
1 parent 18ce4f8 commit 27e91f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,17 +962,19 @@ pm_locals_order(PRISM_ATTRIBUTE_UNUSED pm_parser_t *parser, pm_locals_t *locals,
pm_constant_id_list_insert(list, (size_t) local->index, local->name);

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

if (constant->length >= 1 && *constant->start != '_') {
PM_PARSER_WARN_FORMAT(
parser,
local->location.start,
local->location.end,
PM_WARN_UNUSED_LOCAL_VARIABLE,
(int) constant->length,
(const char *) constant->start
);
if (constant->length >= 1 && *constant->start != '_') {
PM_PARSER_WARN_FORMAT(
parser,
local->location.start,
local->location.end,
PM_WARN_UNUSED_LOCAL_VARIABLE,
(int) constant->length,
(const char *) constant->start
);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/prism/result/warnings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def test_unused_local_variables

refute_warning("def foo; bar = 1; tap { bar }; end")
refute_warning("def foo; bar = 1; tap { baz = bar; baz }; end")

refute_warning("def foo; bar = 1; end", line: -2, compare: false)
end

def test_void_statements
Expand Down

0 comments on commit 27e91f2

Please sign in to comment.