Skip to content

fix: check t[k] should not warn unnecessary-if#972

Closed
EfveZombie wants to merge 2 commits intoEmmyLuaLs:mainfrom
EfveZombie:fix.unnecessary_if
Closed

fix: check t[k] should not warn unnecessary-if#972
EfveZombie wants to merge 2 commits intoEmmyLuaLs:mainfrom
EfveZombie:fix.unnecessary_if

Conversation

@EfveZombie
Copy link

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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

  • Diagnostic Fix: Corrected the "unnecessary-if" diagnostic to no longer warn on conditional checks for values obtained from generic table index access, as these values can legitimately be "nil".
  • Type Inference: Enhanced type inference for generic table member access (t[k]) to explicitly include "LuaType::Nil" as a possible return type, reflecting Lua's behavior where non-existent keys return "nil".
  • Test Coverage: Introduced a new test case (test_table_generic_index_not_unnecessary) to validate that the "unnecessary-if" diagnostic correctly handles generic table index access without false positives.

🧠 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
  • crates/emmylua_code_analysis/src/diagnostic/test/unnecessary_if_test.rs
    • Added a new test case "test_table_generic_index_not_unnecessary" to ensure the "unnecessary-if" diagnostic does not trigger for generic table index access.
  • crates/emmylua_code_analysis/src/semantic/infer/infer_index/mod.rs
    • Modified "infer_member_by_index_table_generic" to union the inferred type with "LuaType::Nil", allowing for "nil" returns on generic table index access.
  • crates/emmylua_code_analysis/src/semantic/member/infer_raw_member.rs
    • Updated "infer_table_generic_raw_member_type" to include "LuaType::Nil" in the returned type for generic table raw member access.
Activity
  • The pull request was opened by EfveZombie.
  • No further activity has been recorded yet.
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
Contributor

@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 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(
Copy link
Contributor

Choose a reason for hiding this comment

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

high

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.

Suggested change
assert!(ws.check_code_for(
assert!(!ws.check_code_for(

@xuhuanzy
Copy link
Member

This change needs a trade-off. I don’t think we need to handle nil here; adding it would introduce a lot of unnecessary annotations.

local t = {}
local k = "hello"
local v = t[k]
if v then
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd argue this is unnecessary. If you want it not to be then the type of t should be table<string,number?>

Copy link
Author

Choose a reason for hiding this comment

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

Thats what I did, quite counterintuitive to me, but u r right

@EfveZombie
Copy link
Author

Current behaviors are same to TypeScript default mode (without noUncheckedIndexedAccess), so I was convinced

Thanks for reviewing /Or2

@EfveZombie EfveZombie closed this Mar 2, 2026
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.

3 participants