Skip to content
Open
11 changes: 10 additions & 1 deletion src/Build/Instance/ProjectItemInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,16 @@ private void CommonConstructor(
if (itemDefinitions == null || !useItemDefinitionsWithoutModification)
{
// TaskItems don't have an item type. So for their benefit, we have to lookup and add the regular item definition.
inheritedItemDefinitions = (itemDefinitions == null) ? null : new List<ProjectItemDefinitionInstance>(itemDefinitions);
if (itemDefinitions == null)
{
inheritedItemDefinitions = null;
}
else
{
List<ProjectItemDefinitionInstance> list = new List<ProjectItemDefinitionInstance>(itemDefinitions.Count + 1); // account for possible addition below to avoid resizing
Copy link
Member

Choose a reason for hiding this comment

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

There is no need to have this list variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think AI agent used a list variable to be able to call AddRange(), which is not available on inheritedItemDefinitions which is IList.
I agree it can be done without the variable by casting since we just created it as a List. Were you suggesting something like this?

inheritedItemDefinitions = (itemDefinitions == null) ? null : new List<ProjectItemDefinitionInstance>(itemDefinitions.Count + 1);
((List<ProjectItemDefinitionInstance>)inheritedItemDefinitions)?.AddRange(itemDefinitions);

list.AddRange(itemDefinitions);
inheritedItemDefinitions = list;
}

ProjectItemDefinitionInstance itemDefinition;
if (projectToUse.ItemDefinitions.TryGetValue(itemTypeToUse, out itemDefinition))
Expand Down