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

Conversation

yoff
Copy link
Contributor

@yoff yoff commented Apr 8, 2025

It finds none of the right things and all of the wrong things...

It finds none of the right things and all of the wrong things...
@Copilot Copilot bot review requested due to automatic review settings April 8, 2025 10:39
@yoff yoff requested a review from a team as a code owner April 8, 2025 10:39
@github-actions github-actions bot added the Ruby label Apr 8, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new Ruby test file aimed at verifying the behavior of uninitialized local variable detection. Key changes include the addition of multiple test cases with alert annotations to check for both missing and spurious alerts in various contexts (basic usage, conditional constructs, assignments, and rescue/ensure blocks).

Files not reviewed (2)
  • ruby/ql/test/query-tests/variables/UninitializedLocal/UninitializedLocal.expected: Language not supported
  • ruby/ql/test/query-tests/variables/UninitializedLocal/UninitializedLocal.qlref: Language not supported

@@ -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_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.

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.

if false
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.

status = i #$ MISSING: Alert
end
puts status #$ SPURIOUS: Alert
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.

rescue SyntaxError
puts "rescue"
puts a #$ SPURIOUS: Alert
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 "rescue"
puts a #$ SPURIOUS: Alert
puts b #$ SPURIOUS: Alert
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.

ensure
puts "ensure"
puts a #$ SPURIOUS: Alert
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 "ensure"
puts a #$ SPURIOUS: Alert
puts b #$ SPURIOUS: Alert
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.

@hvitved
Copy link
Contributor

hvitved commented Apr 9, 2025

It finds none of the right things and all of the wrong things...

The intention behind this query probably deserves a bit of explanation. In Ruby, raw identifiers like x can be both local variable accesses and method calls. It is a local variable access iff it is syntactically preceded by something that binds it (like an assignment). Example:

def m
    puts "m"
end

def foo
    m # calls m above
    if false
        m = 0
        m # reads local variable m
    else
    end
    m # reads uninitialized local variable m, `nil`
    m2 # undefined local variable or method 'm2' for main (NameError)
end

The query is meant to find instances of the last access to m above only. The access to m2, which is not a local variable, would have to be caught by a different query, but I suspect that such a query would be extremely noisy, since we do not have access to entities defined in libraries.

@yoff
Copy link
Contributor Author

yoff commented Apr 9, 2025

Thanks for the explanation! In that case I will proceed with my branch where I filter out a number of reads that the programmer clearly expect could be nil.
I will write a qhelp for this query based on your explanation.

@yoff
Copy link
Contributor Author

yoff commented Apr 9, 2025

but I suspect that such a query would be extremely noisy, since we do not have access to entities defined in libraries.

Indeed, I tried to write that query. While it solved the test cases perfectly, it was extremely noisy on MRVA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants