[test-improver] test: add edge case tests for IgnoreStringMethodReturnValueAnalyzer#9468
Draft
Evangelink wants to merge 1 commit into
Draft
Conversation
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>
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goal and Rationale
IgnoreStringMethodReturnValueAnalyzer(MSTEST0055) had only 5 tests, all covering the happy path (diagnostic fires / doesn't fire based on whether the result ofContains,StartsWith, orEndsWithis used). Three distinct code paths in the analyzer's guard logic were completely untested:_ = str.Contains(...)expressionStatementOperation.Operation is not IInvocationOperationearly returnIsStringMethodCallon the outer call (not a string method) — should not fireApproach
Added three
[TestMethod]entries toIgnoreStringMethodReturnValueAnalyzerTests.cs:WhenDiscardAssignmentIgnoresReturnValue_NoDiagnostic_ = str.Contains("Hello")→ outer operation isIAssignmentOperation, notIInvocationOperation→ early return, no diagnosticWhenStringMethodReturnValueIgnoredInsideLambda_Diagnostic() => { str.Contains("Hello"); }→ ExpressionStatement inside lambda body triggers diagnosticWhenStringMethodResultUsedAsReceiverForChainedCall_NoDiagnosticstr.Contains("Hello").GetHashCode()→ outermost ExpressionStatement operation isGetHashCode()onSystem.Boolean, not a string method → no diagnostic onContainsTrade-offs
Purely additive — no production code changes, no new dependencies.
Test Status
All 8 tests pass (5 existing + 3 new):
Reproducibility
.dotnet/dotnet run --project test/UnitTests/MSTest.Analyzers.UnitTests/ -f net8.0 --no-build -- \ --filter "FullyQualifiedName~IgnoreStringMethodReturnValueAnalyzerTests"Add this agentic workflows to your repo
To install this agentic workflow, run