feat: Detect Implicit Object Creation (Mock<T> myMock = new()) - #1090
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Moq disposable-mock analyzer/code-fix to detect target-typed implicit object creation (new()) and modernizes the Roslyn package versions needed to support that syntax.
Changes:
- Extend
MockDisposableClassesShouldSetupDisposeAnalyzerto analyzeImplicitObjectCreationExpressionin addition to explicitnew Mock<T>(). - Update the Moq code fix and tests to cover target-typed
new()cases. - Bump Roslyn-related NuGet packages (and adapt test helpers) to newer APIs; apply small
IsKind(...)refactors across analyzers/code fixes.
Reviewed changes
Copilot reviewed 29 out of 29 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| Philips.CodeAnalysis.MoqAnalyzers/MockDisposableClassesShouldSetupDisposeAnalyzer.cs | Register syntax actions for implicit object creation and resolve mocked type via semantic model. |
| Philips.CodeAnalysis.MoqAnalyzers/MockDisposableClassesShouldSetupDisposeCodeFixProvider.cs | Broaden code fix node handling to ExpressionSyntax and add a branch for implicit new(). |
| Philips.CodeAnalysis.Test/Moq/MockDisposableClassesShouldSetupDisposeAnalyzerTest.cs | Add analyzer tests for Mock<T> x = new(); patterns. |
| Philips.CodeAnalysis.Test/Moq/MockDisposableClassesShouldSetupDisposeCodeFixProviderTest.cs | Add code-fix tests for target-typed new() and argumented new(...). |
| Philips.CodeAnalysis.Test/Verifiers/DiagnosticVerifier.Helper.cs | Implement AnalyzerConfigOptionsProvider.GlobalOptions for newer Roslyn APIs. |
| Philips.CodeAnalysis.Test/Helpers/TestAnalyzerConfigOptionsProvider.cs | Implement AnalyzerConfigOptionsProvider.GlobalOptions for newer Roslyn APIs. |
| Philips.CodeAnalysis.Test/Philips.CodeAnalysis.Test.csproj | Update Roslyn workspace package reference version. |
| Philips.CodeAnalysis.Common/Philips.CodeAnalysis.Common.csproj | Update Roslyn workspace package reference version. |
| Directory.Build.Analyzer.props | Update Microsoft.CodeAnalysis.Analyzers and Roslyn workspace package reference. |
| Philips.CodeAnalysis.Benchmark/Philips.CodeAnalysis.Benchmark.csproj | Update Microsoft.CodeAnalysis package reference version. |
| Philips.CodeAnalysis.MsTestAnalyzers/TestMethodNameCodeFixProvider.cs | Update rename API usage to SymbolRenameOptions and newer Renamer overload. |
| Philips.CodeAnalysis.MsTestAnalyzers/TestMethodNameAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MsTestAnalyzers/AvoidAssertConditionalAccessAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Maintainability/NoRegionsInMethodAnalyzer.cs | Refactor region detection to traverse directive trivia; add required usings. |
| Philips.CodeAnalysis.Test/Maintainability/Maintainability/NoRegionsInMethodAnalyzerTest.cs | Adjust test inputs/expectations for region directive detection behavior. |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Maintainability/ProhibitDynamicKeywordAnalyzer.cs | Fix message typo and adjust dynamic detection implementation. |
| Philips.CodeAnalysis.Test/Maintainability/Maintainability/ProhibitDynamicKeywordAnalyzerTest.cs | Update test snippets to be compilable as complete types. |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Documentation/XmlDocumentationCodeFixProvider.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.Test/Maintainability/Documentation/XmlDocumentationShouldAddValueAnalyzerTest.cs | Update test content scaffolding to include required type context. |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/RuntimeFailure/DereferenceNullAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Readability/PreventUnnecessaryRangeChecksAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Readability/PreferNamedTuplesAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Naming/VariableNamingConventionAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Naming/EnforceBoolNamingConventionAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Maintainability/MergeIfStatementsAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Maintainability/AvoidStaticMethodAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Maintainability/AvoidStaticClassesAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Maintainability/AvoidPublicMemberVariableAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
| Philips.CodeAnalysis.MaintainabilityAnalyzers/Maintainability/AvoidAsyncVoidAnalyzer.cs | Replace Kind() comparisons with IsKind(...). |
|
…DisposeCodeFixProviderTest.cs Co-authored-by: Alex Barga <99690046+ajbarga@users.noreply.github.com>
…DisposeAnalyzerTest.cs Co-authored-by: Alex Barga <99690046+ajbarga@users.noreply.github.com>
The suggestion was applied inside a string literal, creating syntax errors. Restored the original method and extracted the intended test. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
Brian Collamore (@bcollamore) clearly those suggestions had error, apologies. Resolved. |
|



No description provided.