Add analyzer for ignoring string method return values (MSTEST0055)#6482
Add analyzer for ignoring string method return values (MSTEST0055)#6482Youssef1313 merged 7 commits intomainfrom
Conversation
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Contains, StartsWith, EndsWith returns
src/Analyzers/MSTest.Analyzers/IgnoreStringMethodReturnValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
…etadataName Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
|
@Youssef1313: I must be missing something here, but what does this have to do with MSTest? There's already an IDE0058 Roslyn rule that will report a diagnostic if the return value of a method is not "consumed". |
|
@drieseng We have seen this pattern come up few times where people intended to do "assertion" but ended up just calling string method and ignoring the result. This rule is different from any existing IDExxx or CAxxxx in that it's:
|
Evangelink
left a comment
There was a problem hiding this comment.
LGTM for the sake of completeness I would have added a few tests with Assert.IsTrue, Assert.IsFalse but that's optional.
|
IsTrue/IsFalse are irrelevant for this analyzer (analyzer doesn't handle anything related to them). |
Changes Made
Initial Implementation:
IgnoreStringMethodReturnValueAnalyzerthat warns when return values ofstring.Contains(),string.StartsWith(), orstring.EndsWith()are ignoredMSTEST0055with appropriate localization resourcesCode Review Updates:
context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemString, out INamedTypeSymbol? stringSymbol)withcontext.Compilation.GetSpecialType(SpecialType.System_String)GetSpecialTypealways returns a valid symbol for core types likeSystem.Stringusing MSTest.Analyzers.Helpers;importFixes #6213
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.