Skip to content

ruby: test rb/uninitialized-local-variable #19247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
| UninitializedLocal.rb:17:18:17:18 | x | Local variable $@ may be used before it is initialized. | UninitializedLocal.rb:17:9:17:9 | x | x |
| UninitializedLocal.rb:26:10:26:15 | status | Local variable $@ may be used before it is initialized. | UninitializedLocal.rb:24:9:24:14 | status | status |
| UninitializedLocal.rb:39:10:39:10 | a | Local variable $@ may be used before it is initialized. | UninitializedLocal.rb:35:5:35:5 | a | a |
| UninitializedLocal.rb:40:10:40:10 | b | Local variable $@ may be used before it is initialized. | UninitializedLocal.rb:36:5:36:5 | b | b |
| UninitializedLocal.rb:45:10:45:10 | a | Local variable $@ may be used before it is initialized. | UninitializedLocal.rb:35:5:35:5 | a | a |
| UninitializedLocal.rb:46:10:46:10 | b | Local variable $@ may be used before it is initialized. | UninitializedLocal.rb:36:5:36:5 | b | b |
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
query: queries/variables/UninitializedLocal.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
def test_basic
puts x #$ MISSING: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized variable 'x' is used without prior assignment, yet the expected alert is not triggered. Please verify that the analyzer correctly flags such cases.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

end

def test_nil
x = foo.nil? #$ MISSING: Alert
puts x
end

def test_condition
if x #$ MISSING: Alert
puts x #$ MISSING: Alert
end
end

def test_nested_condition
if (x = 4 || x) #$ SPURIOUS: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alert on this conditional assignment appears to be spurious since assignment within the condition should not be flagged. Consider updating the analyzer to ignore such valid patterns.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

puts x
end
end

def test_conditional_assignment
if false
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of variable 'i' in the assignment should trigger an alert for uninitialized usage, but no alert is generated. Verify that the analyzer processes assignments in unreachable branches correctly.

Suggested change
if false
if false
i = 0

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

status = i #$ MISSING: Alert
end
puts status #$ SPURIOUS: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alert on accessing 'status' appears to be spurious as it is derived from a branch where it was assigned (even if conditionally). Review the analyzer's logic for conditional assignments.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

puts i #$ MISSING: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized variable 'i' is used but the expected alert is missing. Ensure that the analyzer flags such usage consistently.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

end

def get
raise SyntaxError
end

def test_rescue_ensure
a = get()
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of 'c' in an arithmetic operation should trigger an alert for uninitialized variable usage, yet no alert is produced. Confirm that the analyzer captures such cases correctly.

Suggested change
a = get()
a = get()
c = 0

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

b = c + 2 #$ MISSING: Alert
rescue SyntaxError
puts "rescue"
puts a #$ SPURIOUS: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alert on variable 'a' in the rescue block appears spurious since 'a' was successfully assigned before the exception. Consider refining the alert conditions for rescued variables.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

puts b #$ SPURIOUS: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alert on variable 'b' within the rescue block seems incorrect because its assignment might already be handled; review the analyzer's handling of variable scope in rescue clauses.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

puts c #$ MISSING: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized variable 'c' is used in the rescue block but the alert is missing. Ensure that the analyzer reports such variables in all contexts consistently.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

puts "rescue end"
ensure
puts "ensure"
puts a #$ SPURIOUS: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the ensure block, the alert on variable 'a' appears spurious since it remains available from earlier assignments. Please re-check the analyzer's behavior regarding variable availability in ensure blocks.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

puts b #$ SPURIOUS: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alert triggered on 'b' in the ensure block seems unwarranted given its context; consider revising the conditions that lead to this warning.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

puts c #$ MISSING: Alert
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uninitialized variable 'c' in the ensure block is not flagged as expected. Verify that the analyzer consistently reports uninitialized variables across rescue and ensure blocks.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

puts "the end"
end
Loading