IDE0305 code fix removes AddRange side effects and changes semantics for EF6-style fluent chains
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
**Version Used**:
Visual Studio 2026 18.5.2
.NET 10 SDK
C# latest language version
Entity Framework 6 (EF6)
**Steps to Reproduce**:
1. Paste the following code into a new .NET 10 console application.
2. Place the cursor on `ToList()`.
3. Apply the `IDE0305: Use collection expression for fluent` code fix.
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
var context = new FakeContext();
var notifications = new List
{
new("A"),
new("B")
};
var result = context.Notifications
.AddRange(notifications)
.ToList();
Console.WriteLine("Context contents:");
Console.WriteLine(string.Join(", ", context.Notifications.Select(x => x.Name)));
Console.WriteLine();
Console.WriteLine("Returned contents:");
Console.WriteLine(string.Join(", ", result.Select(x => x.Name)));
public class FakeContext
{
public NotificationSet Notifications { get; } = new();
}
public class NotificationSet : List
{
public IEnumerable AddRange(IEnumerable notifications)
{
foreach (var notification in notifications)
{
Add(notification);
yield return notification;
}
}
}
public record Notification(string Name);
```
The code fix rewrites:
```csharp
context.Notifications
.AddRange(notifications)
.ToList();
```
into:
```csharp
[.. context.Notifications
, .. notifications];
```
**Diagnostic Id**:
IDE0305: Use collection expression for fluent
**Expected Behavior**:
The code fix should not be offered because `AddRange(...)` has side effects and mutates the target collection.
Original behavior:
- `context.Notifications` is mutated
- returned result contains only the added notifications
Output before code fix:
```txt
Context contents:
A, B
Returned contents:
A, B
```
The generated code should also preserve reasonable formatting/indentation if a fix is applied.
**Actual Behavior**:
The generated collection expression removes the `AddRange(...)` call entirely, changing program semantics.
Behavior after code fix:
- `context.Notifications` is no longer mutated
- returned result now contains a new combined collection instead
Output after code fix:
```txt
Context contents:
Returned contents:
A, B
```
Additionally, the generated code formatting appears incorrect/unexpected:
```csharp
[.. context.Notifications
, .. notifications];
```
This is a semantic behavior change caused by a style code fix.
Contributor guide
Assessment
This issue has not been assessed yet.