fix: check t[k] should not warn unnecessary-if#972
fix: check t[k] should not warn unnecessary-if#972EfveZombie wants to merge 2 commits intoEmmyLuaLs:mainfrom
Conversation
Summary of ChangesHello, 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 resolves an issue where the "unnecessary-if" diagnostic incorrectly flagged conditional checks on values retrieved from generic table index access. The underlying problem was that the type inference system did not account for "nil" as a possible return type when accessing a table with a generic key. By explicitly adding "LuaType::Nil" to the inferred type for such operations, the linter now correctly recognizes these "if" checks as necessary, preventing false positive warnings and improving code analysis accuracy. 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. Changelog
Activity
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.
Code Review
This pull request correctly fixes an issue where an if check on a value retrieved from a generic table was incorrectly flagged as unnecessary. The changes ensure that the inferred type for a generic table access includes nil, which is the correct behavior in Lua when a key might not exist. The logic in infer_index and infer_raw_member is sound. I've found a small issue in the new test case's assertion which I've commented on.
| #[test] | ||
| fn test_table_generic_index_not_unnecessary() { | ||
| let mut ws = VirtualWorkspace::new(); | ||
| assert!(ws.check_code_for( |
There was a problem hiding this comment.
The test name test_table_generic_index_not_unnecessary suggests that this test should pass if the UnnecessaryIf diagnostic is not found. Based on other tests in this file, check_code_for seems to return true when a diagnostic is found. If that's the case, this assertion should be inverted to assert!(!ws.check_code_for(...)) to correctly verify the fix.
| assert!(ws.check_code_for( | |
| assert!(!ws.check_code_for( |
|
This change needs a trade-off. I don’t think we need to handle |
| local t = {} | ||
| local k = "hello" | ||
| local v = t[k] | ||
| if v then |
There was a problem hiding this comment.
I'd argue this is unnecessary. If you want it not to be then the type of t should be table<string,number?>
There was a problem hiding this comment.
Thats what I did, quite counterintuitive to me, but u r right
|
Current behaviors are same to Thanks for reviewing /Or2 |
No description provided.