Skip to content

Update ExplicitNumberDeclarationAnalyzer to handle out var and foreach#385

Closed
google-labs-jules[bot] wants to merge 1 commit into
mainfrom
update-explicit-number-analyzer-1733819396258710820
Closed

Update ExplicitNumberDeclarationAnalyzer to handle out var and foreach#385
google-labs-jules[bot] wants to merge 1 commit into
mainfrom
update-explicit-number-analyzer-1733819396258710820

Conversation

@google-labs-jules

Copy link
Copy Markdown
Contributor

This PR updates the ExplicitNumberDeclarationAnalyzer (SMA8001) to correctly identify and warn against the use of var for numeric primitive types in additional contexts. Specifically, it now handles:

  • out var declarations (e.g., TryGetValue(out var value)).
  • foreach loops with var (e.g., foreach (var x in collection)).
  • Deconstruction declarations with var (e.g., var (a, b) = ...).
  • Nested deconstructions and deconstructions within foreach loops.

The implementation was refactored to share common reporting logic and recursively handle tuple deconstructions. Unit tests have been expanded to verify these new scenarios.


PR created automatically by Jules for task 1733819396258710820 started by @sator-imaging

- Register SyntaxNodeAction for DeclarationExpression and ForEachStatement.
- Handle SingleVariableDesignation and ParenthesizedVariableDesignation (for deconstruction) in DeclarationExpressions.
- Refactor reporting logic into a common helper method.
- Add comprehensive test cases covering out var, foreach, and tuple deconstruction.
@google-labs-jules

Copy link
Copy Markdown
Contributor Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 13d80258-c4ec-483b-a795-3c3b74fb5973

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

ExplicitNumberDeclarationAnalyzer is extended to detect primitive-number var usage in declaration expressions (out var, tuple deconstruction) and foreach statements. Reporting logic is extracted into a shared ReportIfPrimitiveNumber helper. A new test asserts six diagnostics across all added patterns.

Changes

SMA8001 Analyzer Extension for Additional var Patterns

Layer / File(s) Summary
New var pattern handlers and shared helper
src/analysis/Analyzers/ExplicitNumberDeclarationAnalyzer.cs
Initialize registers handlers for DeclarationExpressionSyntax and ForEachStatementSyntax. AnalyzeDeclarationExpression handles single and parenthesized variable designations, ReportRecursive traverses nested tuples, AnalyzeForEachStatement checks foreach var type, and ReportIfPrimitiveNumber is introduced as a shared helper that validates ILocalSymbol and emits SMA8001. AnalyzeVariableDeclaration is updated to delegate to this helper.
Violation test for out var, foreach, and tuple deconstruction
test/AnalyzerTests/SMA8001_ExplicitNumberDeclarationAnalyzerTests.cs
Adds SMA8001_Violation_OtherVarDeclarationsWithPrimitiveNumbers, which supplies an inline C# snippet with out var, foreach, and tuple deconstruction patterns and asserts six SMA8001 diagnostics with specific identifier names and source locations.

Estimated code review effort

3 (Moderate) | ~20 minutes

Finishing Touches
Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch update-explicit-number-analyzer-1733819396258710820
Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch update-explicit-number-analyzer-1733819396258710820

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nitpick comments (1)
test/AnalyzerTests/SMA8001_ExplicitNumberDeclarationAnalyzerTests.cs (1)

159-196: 💤 Low value

Consider adding test coverage for edge cases.

The test comprehensively covers the main scenarios. Consider adding optional test cases for:

  • Deeply nested tuple deconstruction: var ((a, b), c) = ((1, 2), 3);
  • Discards in tuple deconstruction: var (_, b) = (1, 2); (should only flag b if it's a primitive number)

These edge cases would verify that the recursive tuple handling and null symbol checks work correctly in all scenarios.

Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/AnalyzerTests/SMA8001_ExplicitNumberDeclarationAnalyzerTests.cs` around
lines 159 - 196, The test method
SMA8001_Violation_OtherVarDeclarationsWithPrimitiveNumbers lacks coverage for
edge cases involving deeply nested tuple deconstruction and discard patterns.
Add test cases within the test string variable that include deeply nested tuple
deconstruction syntax (such as var ((a, b), c) = ((1, 2), 3);) and discard
patterns in tuple deconstruction (such as var (_, b) = (1, 2);), then add the
corresponding expected diagnostic assertions to verify that the analyzer
correctly handles recursive tuple handling and only flags non-discard variables
when they involve primitive numbers.
Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@test/AnalyzerTests/SMA8001_ExplicitNumberDeclarationAnalyzerTests.cs`:
- Around line 159-196: The test method
SMA8001_Violation_OtherVarDeclarationsWithPrimitiveNumbers lacks coverage for
edge cases involving deeply nested tuple deconstruction and discard patterns.
Add test cases within the test string variable that include deeply nested tuple
deconstruction syntax (such as var ((a, b), c) = ((1, 2), 3);) and discard
patterns in tuple deconstruction (such as var (_, b) = (1, 2);), then add the
corresponding expected diagnostic assertions to verify that the analyzer
correctly handles recursive tuple handling and only flags non-discard variables
when they involve primitive numbers.

Review details
Additional comments (7)
src/analysis/Analyzers/ExplicitNumberDeclarationAnalyzer.cs (6)

34-35: LGTM!


38-55: LGTM!


80-93: LGTM!


111-120: LGTM!


57-78: No issues found. The project targets C# 9.0 (as configured in the project file), which fully supports the is not pattern used on line 65. The code is compatible and requires no changes.


95-109: No issues found. Foreach with tuple deconstruction is correctly handled through the DeclarationExpression handler. When encountering foreach (var (x, y) in ...), the syntax tree contains a ForEachVariableStatementSyntax with a Variable property that is a DeclarationExpressionSyntax. This DeclarationExpressionSyntax triggers the AnalyzeDeclarationExpression handler, which detects the ParenthesizedVariableDesignationSyntax and delegates to ReportRecursive to recursively report all tuple variables. The test expectations at line 181 align with this behavior and will pass correctly.

test/AnalyzerTests/SMA8001_ExplicitNumberDeclarationAnalyzerTests.cs (1)

159-196: LGTM!

@sator-imaging sator-imaging force-pushed the update-explicit-number-analyzer-1733819396258710820 branch from c96f7e2 to 7acf9b7 Compare June 16, 2026 08:24
@sator-imaging sator-imaging deleted the update-explicit-number-analyzer-1733819396258710820 branch June 16, 2026 08:25
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