Skip to content

Conversation

@phanen
Copy link
Contributor

@phanen phanen commented Dec 27, 2025

unnecessary if

if 1 == nil then
end

@gemini-code-assist
Copy link

Summary of Changes

Hello @phanen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the diagnostic system to accurately handle comparisons between "nil" and constant values in Lua code. Previously, such comparisons might have been misidentified as 'unnecessary'. The changes update the semantic inference engine to correctly evaluate these expressions, ensuring that diagnostics like 'unnecessary if' and 'unnecessary assert' are only triggered when truly applicable, thereby reducing false positives and improving the precision of code analysis.

Highlights

  • Diagnostic Logic Enhancement: Improved the semantic inference logic for binary comparisons involving "nil" and constant values. The system now correctly determines the boolean outcome (always "false" for equality, "true" for inequality) when "nil" is compared with any non-nil constant.
  • New Test Cases for Diagnostics: Added new unit tests for "UnnecessaryAssert" and "UnnecessaryIf" diagnostics. These tests specifically verify that comparisons between non-nil constants and "nil" are not incorrectly flagged as unnecessary, reflecting the updated inference logic.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly adds logic to infer constant boolean results from comparisons between non-nil constants and nil. This is a good improvement for diagnostics. However, the tests added to validate this change appear to have incorrect assertions; they check that no diagnostic is produced, when the new logic should indeed produce diagnostics. I've provided suggestions to correct the tests. Additionally, I've pointed out a minor typo and an opportunity to refactor some duplicated code for better maintainability.

Comment on lines +134 to +145
assert!(!ws.check_code_for(
DiagnosticCode::UnnecessaryAssert,
r#"
assert("abc" ~= nil)
"#
));
assert!(!ws.check_code_for(
DiagnosticCode::UnnecessaryAssert,
r#"
assert(1 == nil)
"#
));

Choose a reason for hiding this comment

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

critical

The logic of these tests is incorrect. With the changes in infer_binary/mod.rs, the expressions inside assert() will be evaluated as constant booleans (true for "abc" ~= nil and false for 1 == nil). This should trigger the UnnecessaryAssert diagnostic for both cases (an unnecessary and an impossible assert, respectively).

Therefore, ws.check_code_for(...) should return true, and you should be using assert!(...) to verify that the diagnostic is correctly reported.

Suggested change
assert!(!ws.check_code_for(
DiagnosticCode::UnnecessaryAssert,
r#"
assert("abc" ~= nil)
"#
));
assert!(!ws.check_code_for(
DiagnosticCode::UnnecessaryAssert,
r#"
assert(1 == nil)
"#
));
assert!(ws.check_code_for(
DiagnosticCode::UnnecessaryAssert,
r#"
assert("abc" ~= nil)
"#
));
assert!(ws.check_code_for(
DiagnosticCode::UnnecessaryAssert,
r#"
assert(1 == nil)
"#
));

Comment on lines +33 to +48
assert!(!ws.check_code_for(
DiagnosticCode::UnnecessaryIf,
r#"
if 1 ~= nil then
error("Impossible")
end
"#
));
assert!(!ws.check_code_for(
DiagnosticCode::UnnecessaryIf,
r#"
if "abc" == nil then
error("Impossible")
end
"#
));

Choose a reason for hiding this comment

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

critical

The logic of these tests is incorrect. With the changes in infer_binary/mod.rs, the conditions in the if statements will be evaluated as constant booleans (true for 1 ~= nil and false for "abc" == nil). This should trigger the UnnecessaryIf diagnostic for both cases (an unnecessary and an impossible if-condition, respectively).

Therefore, ws.check_code_for(...) should return true, and you should be using assert!(...) to verify that the diagnostic is correctly reported.

Suggested change
assert!(!ws.check_code_for(
DiagnosticCode::UnnecessaryIf,
r#"
if 1 ~= nil then
error("Impossible")
end
"#
));
assert!(!ws.check_code_for(
DiagnosticCode::UnnecessaryIf,
r#"
if "abc" == nil then
error("Impossible")
end
"#
));
assert!(ws.check_code_for(
DiagnosticCode::UnnecessaryIf,
r#"
if 1 ~= nil then
error("Impossible")
end
"#
));
assert!(ws.check_code_for(
DiagnosticCode::UnnecessaryIf,
r#"
if "abc" == nil then
error("Impossible")
end
"#
));

}

#[test]
fn test_nonnli_with_nil() {

Choose a reason for hiding this comment

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

medium

There's a typo in the test function name. It should be test_nonnil_with_nil to match the other test file and for clarity.

Suggested change
fn test_nonnli_with_nil() {
fn test_nonnil_with_nil() {

Comment on lines +509 to +518
(LuaType::Nil, right) if right.is_const() => match op {
BinaryOperator::OpEq => Ok(LuaType::BooleanConst(false)),
BinaryOperator::OpNe => Ok(LuaType::BooleanConst(true)),
_ => Ok(LuaType::Boolean),
},
(left, LuaType::Nil) if left.is_const() => match op {
BinaryOperator::OpEq => Ok(LuaType::BooleanConst(false)),
BinaryOperator::OpNe => Ok(LuaType::BooleanConst(true)),
_ => Ok(LuaType::Boolean),
},

Choose a reason for hiding this comment

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

medium

These two match arms handle the cases (nil, const) and (const, nil) respectively, and their logic is identical. You can combine them into a single arm to reduce code duplication and improve maintainability.

Suggested change
(LuaType::Nil, right) if right.is_const() => match op {
BinaryOperator::OpEq => Ok(LuaType::BooleanConst(false)),
BinaryOperator::OpNe => Ok(LuaType::BooleanConst(true)),
_ => Ok(LuaType::Boolean),
},
(left, LuaType::Nil) if left.is_const() => match op {
BinaryOperator::OpEq => Ok(LuaType::BooleanConst(false)),
BinaryOperator::OpNe => Ok(LuaType::BooleanConst(true)),
_ => Ok(LuaType::Boolean),
},
(left, right) if (left.is_nil() && right.is_const()) || (right.is_nil() && left.is_const()) => match op {
BinaryOperator::OpEq => Ok(LuaType::BooleanConst(false)),
BinaryOperator::OpNe => Ok(LuaType::BooleanConst(true)),
_ => Ok(LuaType::Boolean),
},

@phanen phanen marked this pull request as draft December 27, 2025 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant