-
Notifications
You must be signed in to change notification settings - Fork 52
fix(diagnostics): compare non-nil with nil #906
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @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
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this 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.
| assert!(!ws.check_code_for( | ||
| DiagnosticCode::UnnecessaryAssert, | ||
| r#" | ||
| assert("abc" ~= nil) | ||
| "# | ||
| )); | ||
| assert!(!ws.check_code_for( | ||
| DiagnosticCode::UnnecessaryAssert, | ||
| r#" | ||
| assert(1 == nil) | ||
| "# | ||
| )); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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) | |
| "# | |
| )); |
| 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 | ||
| "# | ||
| )); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| (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), | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| (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), | |
| }, |
unnecessary if