Skip to content

[test-improver] test: add edge case tests for IgnoreStringMethodReturnValueAnalyzer#9468

Draft
Evangelink wants to merge 1 commit into
mainfrom
test-assist/ignore-string-method-edge-cases-1782516482-fa3ee30ff0b2f876
Draft

[test-improver] test: add edge case tests for IgnoreStringMethodReturnValueAnalyzer#9468
Evangelink wants to merge 1 commit into
mainfrom
test-assist/ignore-string-method-edge-cases-1782516482-fa3ee30ff0b2f876

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Goal and Rationale

IgnoreStringMethodReturnValueAnalyzer (MSTEST0055) had only 5 tests, all covering the happy path (diagnostic fires / doesn't fire based on whether the result of Contains, StartsWith, or EndsWith is used). Three distinct code paths in the analyzer's guard logic were completely untested:

Untested path Guard
Discard assignment _ = str.Contains(...) expressionStatementOperation.Operation is not IInvocationOperation early return
String method call inside a lambda block body ExpressionStatement inside lambda — should still fire
String method result used as a receiver in a chained call IsStringMethodCall on the outer call (not a string method) — should not fire

Approach

Added three [TestMethod] entries to IgnoreStringMethodReturnValueAnalyzerTests.cs:

Test What it covers
WhenDiscardAssignmentIgnoresReturnValue_NoDiagnostic _ = str.Contains("Hello") → outer operation is IAssignmentOperation, not IInvocationOperation → early return, no diagnostic
WhenStringMethodReturnValueIgnoredInsideLambda_Diagnostic () => { str.Contains("Hello"); } → ExpressionStatement inside lambda body triggers diagnostic
WhenStringMethodResultUsedAsReceiverForChainedCall_NoDiagnostic str.Contains("Hello").GetHashCode() → outermost ExpressionStatement operation is GetHashCode() on System.Boolean, not a string method → no diagnostic on Contains

Trade-offs

Purely additive — no production code changes, no new dependencies.

Test Status

All 8 tests pass (5 existing + 3 new):

Test run summary: Passed!
  total: 8
  failed: 0
  succeeded: 8
  duration: 5s 537ms

Reproducibility

.dotnet/dotnet run --project test/UnitTests/MSTest.Analyzers.UnitTests/ -f net8.0 --no-build -- \
  --filter "FullyQualifiedName~IgnoreStringMethodReturnValueAnalyzerTests"

🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Test Improver workflow. · 1.1K AIC · ⌖ 25.3 AIC · ⊞ 58.2K · [◷]( · )

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/test-improver.md@main

Add 3 edge case tests covering untested code paths in the analyzer's
guard conditions:

1. WhenDiscardAssignmentIgnoresReturnValue_NoDiagnostic
   - `_ = str.Contains("test")` wraps an IAssignmentOperation (not
     IInvocationOperation), so the early-return guard fires and no
     diagnostic is reported.

2. WhenStringMethodReturnValueIgnoredInsideLambda_Diagnostic
   - Expression statements inside a lambda block body are still
     ExpressionStatement operations, so the analyzer fires inside
     the lambda just as at method level.

3. WhenStringMethodResultUsedAsReceiverForChainedCall_NoDiagnostic
   - `str.Contains("Hello").GetHashCode()` — the ExpressionStatement's
     outermost operation is GetHashCode() on System.Boolean (not a
     string method), so IsStringMethodCall returns false and no
     diagnostic is reported. The Contains result IS used as a receiver.

All 8 tests pass (5 existing + 3 new).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 26, 2026 23:34
@Evangelink Evangelink added type/automation Created or maintained by an agentic workflow. type/test-gap Missing or insufficient tests. labels Jun 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds additional unit-test coverage for the MSTest analyzer IgnoreStringMethodReturnValueAnalyzer (MSTEST0055) to cover previously untested guard/shape scenarios, without changing production analyzer behavior.

Changes:

  • Added a test ensuring discard assignments (_ = str.Contains(...)) do not produce MSTEST0055 diagnostics.
  • Added a test ensuring ignored string-method return values inside a lambda block body do produce diagnostics.
  • Added a test ensuring chained calls where the string method result is used as a receiver (e.g. str.Contains(...).GetHashCode()) do not produce diagnostics.
Show a summary per file
File Description
test/UnitTests/MSTest.Analyzers.UnitTests/IgnoreStringMethodReturnValueAnalyzerTests.cs Adds three new test cases covering discard assignment, lambda-body expression statements, and chained-call receiver usage scenarios for MSTEST0055.

Review details

  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Low

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/automation Created or maintained by an agentic workflow. type/test-gap Missing or insufficient tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants