Fix WTG3014/WTG3013 code-fix to emit explicit type argument when needed#239
Fix WTG3014/WTG3013 code-fix to emit explicit type argument when needed#239Copilot wants to merge 3 commits into
Conversation
…nt type differs from collection type
When converting Concat to Prepend/Append, if the element being
prepended/appended has a different type than the Concat method's type
argument (e.g., new object[] { viewModel }.Concat(viewModel.Items)),
the code fix now emits Prepend<T>/Append<T> with an explicit type
argument to avoid CS0411.
Agent-Logs-Url: https://github.com/WiseTechGlobal/WTG.Analyzers/sessions/f7bd2b00-ecf5-43fe-af98-395e79831d26
Co-authored-by: brian-reichle <18721383+brian-reichle@users.noreply.github.com>
| public static async Task<Document> ReplaceWithAppropriateMethod(Document document, Diagnostic diagnostic, CancellationToken c) | ||
| { | ||
| var root = await document.RequireSyntaxRootAsync(c).ConfigureAwait(true); | ||
| var semanticModel = await document.RequireSemanticModelAsync(c).ConfigureAwait(true); |
There was a problem hiding this comment.
@copilot, using SemanticModel is expensive, so we should avoid it where possible. In this case, we should be able to take the TypeSyntax from the new array expression and use that as the type argument (after trimming trivia and adding Simplifier.Annotation).
There was a problem hiding this comment.
Done in ba72978. Replaced SemanticModel usage with syntactic extraction of the TypeSyntax from the array/object creation expression. The type argument is now annotated with Simplifier.Annotation so Roslyn removes it when it's redundant (e.g., new int[] { 5 }.Concat(a) still produces a.Prepend(5)).
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| public class ViewModel |
There was a problem hiding this comment.
@copilot
ViewModel should be placed after Bob so that future changes are less likely to require location updates in Diagnostics.xml.
There was a problem hiding this comment.
Done in ba72978. Moved ViewModel after Bob in both Source.cs and Result.cs.
…move ViewModel after Bob Address review feedback: - Extract type argument from array/object creation syntax instead of using expensive SemanticModel. Uses Simplifier.Annotation to let Roslyn remove redundant type arguments. - Move ViewModel class after Bob in test files to reduce future diagnostic location churn. Agent-Logs-Url: https://github.com/WiseTechGlobal/WTG.Analyzers/sessions/6e17af6a-5f2e-43d6-9651-5ddffe01a303 Co-authored-by: brian-reichle <18721383+brian-reichle@users.noreply.github.com>
The Concat→Prepend/Append code-fix produces uncompilable code when the element type differs from the sequence element type. For example:
Changes
LinqEnumerableCodeFixProvidernow extracts theTypeSyntaxdirectly from the array/object creation expression (e.g.,objectfromnew object[] { ... }) to determine the type argument — avoiding the expensiveSemanticModelGetCollectionElementTypeextracts the element type fromArrayCreationExpressionor single-type-argumentObjectCreationExpressionGetMethodNameemits aGenericNamenode withSimplifier.Annotationwhen an explicit type is present, allowing Roslyn to remove it when redundantPrependTypeMismatchtest data covering both Prepend and Append with mismatched types