Skip to content

Fix WTG3014/WTG3013 code-fix to emit explicit type argument when needed#239

Draft
Copilot wants to merge 3 commits into
masterfrom
copilot/fix-wtg3014-bad-code-fix
Draft

Fix WTG3014/WTG3013 code-fix to emit explicit type argument when needed#239
Copilot wants to merge 3 commits into
masterfrom
copilot/fix-wtg3014-bad-code-fix

Conversation

Copilot AI commented May 26, 2026

Copy link
Copy Markdown

The Concat→Prepend/Append code-fix produces uncompilable code when the element type differs from the sequence element type. For example:

// Source: triggers WTG3014
var expected = new object[] { viewModel }.Concat(viewModel.Items);

// Before (broken - CS0411):
var expected = viewModel.Items.Prepend(viewModel);

// After (correct):
var expected = viewModel.Items.Prepend<object>(viewModel);

Changes

  • LinqEnumerableCodeFixProvider now extracts the TypeSyntax directly from the array/object creation expression (e.g., object from new object[] { ... }) to determine the type argument — avoiding the expensive SemanticModel
  • New GetCollectionElementType extracts the element type from ArrayCreationExpression or single-type-argument ObjectCreationExpression
  • GetMethodName emits a GenericName node with Simplifier.Annotation when an explicit type is present, allowing Roslyn to remove it when redundant
  • Added PrependTypeMismatch test data covering both Prepend and Append with mismatched types

Copilot AI linked an issue May 26, 2026 that may be closed by this pull request
…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>
Copilot AI changed the title [WIP] Fix incorrect code-fix for WTG3014 Fix WTG3014/WTG3013 code-fix to emit explicit type argument when needed May 26, 2026
Copilot AI requested a review from brian-reichle May 26, 2026 04:06
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);

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.

@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).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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

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.

@copilot
ViewModel should be placed after Bob so that future changes are less likely to require location updates in Diagnostics.xml.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WTG3014 bad code-fix

2 participants