Skip to content

feat: detect unused expression errors in statement context#552

Open
aofei wants to merge 1 commit intogoplus:mainfrom
aofei:unusedExpr
Open

feat: detect unused expression errors in statement context#552
aofei wants to merge 1 commit intogoplus:mainfrom
aofei:unusedExpr

Conversation

@aofei
Copy link
Member

@aofei aofei commented Dec 22, 2025

  • Add ElemFlags to track expression kinds (type cast, map index, comma-ok)
  • Add isValidStmtExpr() to validate statements (only calls and receive ops)
  • Add exprDescription() and helpers for Go-compatible error messages
  • Report "xxx (type) is not used" errors in EndStmt()

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @aofei, 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 enhances the compiler's diagnostic capabilities by introducing robust detection and reporting of unused expression errors in statement contexts. It categorizes different types of expressions using new flags and implements validation logic to ensure that only appropriate expressions (like function calls or channel receives) can stand alone as statements, thereby improving code quality and adherence to Go language specifications.

Highlights

  • Expression Kind Tracking: Introduced ElemFlags to categorize expressions, specifically marking type cast, map index, and comma-ok expressions for special handling.
  • Statement Context Validation: Added isValidStmtExpr() to determine if an expression is valid when used as a standalone statement, adhering to Go's rules (only function calls and channel receive operations are permitted).
  • Go-Compatible Error Messages: Implemented exprCode() and exprDescription() helper functions to generate error messages that closely match the Go compiler's format for unused expressions.
  • Unused Expression Error Reporting: Modified EndStmt() to detect and report 'xxx (type) is not used' errors for expressions that are not valid as statements, improving compiler diagnostics.

🧠 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.

@codecov
Copy link

codecov bot commented Dec 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.19%. Comparing base (03c926d) to head (fc79eee).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #552      +/-   ##
==========================================
+ Coverage   96.00%   96.19%   +0.18%     
==========================================
  Files          23       23              
  Lines        6482     6592     +110     
==========================================
+ Hits         6223     6341     +118     
+ Misses        193      183      -10     
- Partials       66       68       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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 introduces a valuable feature to detect and report unused expression values as errors, aligning the behavior more closely with the standard Go compiler. The implementation is thorough, including new flags to track expression kinds, helper functions to generate Go-compatible error messages, and comprehensive tests for both valid and invalid statement expressions. I've identified a few areas for improvement in the helper functions to enhance correctness and maintainability, particularly in isAddressable and exprCode.

- Add `ElemFlags` to track expression kinds (type cast, map index,
  comma-ok)
- Add `isValidStmtExpr()` to validate statements (only calls and receive
  ops)
- Add `exprDescription()` and helpers for Go-compatible error messages
- Report "xxx (type) is not used" errors in `EndStmt()`

Signed-off-by: Aofei Sheng <aofei@aofeisheng.com>
@aofei aofei marked this pull request as ready for review January 7, 2026 00:57
Copilot AI review requested due to automatic review settings January 7, 2026 00:57
Copy link

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 adds compile-time detection of unused expressions in statement context, matching Go compiler behavior. When an expression that produces a value is used as a standalone statement (e.g., a + 1 instead of _ = a + 1), the compiler now reports an error with descriptive messages like "a + 1 (value of type int) is not used".

Key changes:

  • Added ElemFlags enum to track expression operand modes (type cast, map index, comma-ok)
  • Implemented isValidStmtExpr() to validate that only function calls and receive operations can appear as statements
  • Added exprDescription() and helper functions to generate Go-compatible error messages with proper type and value formatting

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/stack.go Adds ElemFlags type and field to Elem struct to track expression kinds for error reporting
codebuild.go Implements core validation logic including isValidStmtExpr(), exprDescription(), and integration into EndStmt()
ast.go Sets ElemFlagTypeCast flag for type conversion expressions
builtin.go Adds clarifying comment about channel receive operations being valid as statements
codebuild_test.go Adds unit tests for helper functions like isValidStmtExpr(), exprCode(), and isAddressable()
error_msg_test.go Adds comprehensive test cases covering various unused expression scenarios with expected error messages
package_test.go Fixes existing test and adds valid statement expression tests for builtin functions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@xgopilot
Copy link
Contributor

xgopilot bot commented Jan 7, 2026

Code Review Summary

Excellent implementation! This PR successfully adds unused expression error detection with Go-compatible error messages. The code demonstrates strong engineering practices with comprehensive test coverage and clean design.

Strengths:

  • Outstanding design: ElemFlags provides a clean, performant way to track expression kinds
  • Comprehensive testing: 30+ test cases with exact error message validation
  • Go-compatible: Error messages accurately match Go compiler format
  • Well-documented: Functions have clear explanations matching implementation

Minor suggestions (2 documentation improvements noted inline - both optional)

All review agents (code quality, performance, documentation, security) found the implementation to be sound with no blocking issues.

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.

2 participants