# S4158 FP when modifying collection via alias

**URL:** <https://community.sonarsource.com/t/s4158-fp-when-modifying-collection-via-alias/142631>\
**Category:** Report False-positive / False-negative...\
**Tags:** dotnet\
**Created:** [June 11, 2025, 2:50pm UTC](https://community.sonarsource.com/t/s4158-fp-when-modifying-collection-via-alias/142631 "2025-06-11T14:50:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![a10r](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@a10r](https://community.sonarsource.com/u/a10r)\
**Post date:** [June 11, 2025, 2:50pm UTC](https://community.sonarsource.com/t/s4158-fp-when-modifying-collection-via-alias/142631/1 "2025-06-11T14:50:34Z")

</div>

- **What language is this for?** C#
- **Which rule?** S4158 “Empty collections should not be accessed or iterated”
- **Why do you believe it’s a false-positive/false-negative?** The collection is not always empty.
- **Versions:** VS 17.13.4, SonarQube for VS 8.20.0.13314, connected mode with SonarQube Server v2025.1.1

Here is a reduced example of this FP that we found in our production code base:

```auto
public static void Repro(string[] args)
{
	var set = new HashSet<string>();

	foreach (var arg in args)
	{
		var target = set;

		if (target.Contains(arg)) // S4158
		{
			throw new ArgumentException("duplicate");
		}

		target.Add(arg);
	}
}

```

`set` is added to through the alias `target`, so it is not guaranteed to be empty.

This example is simple to work around of course just by removing the alias; in our production code we have multiple collections and one of them is assigned to `target` based on some condition within the loop.

---

<div class="post-metadata">

**Author:** ![Mary](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mary/32/59106_2.png) [@Mary](https://community.sonarsource.com/u/Mary)\
**Post date:** [June 13, 2025, 7:11am UTC](https://community.sonarsource.com/t/s4158-fp-when-modifying-collection-via-alias/142631/4 "2025-06-13T07:11:43Z")

</div>

Hello @a10r and thank you for reporting this.

This particular example that you provided us with, is a true positive - since the set is always empty. For each `arg` it’s being reassigned to the `target`, without it being updated somehow.

I assume this is not the case in your code. Could you provide me with a reproducer closer to your actual code so I understand why our symbolic execution engine considers it empty?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Corniel](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/corniel/32/10101_2.png) [@Corniel](https://community.sonarsource.com/u/Corniel)\
**Post date:** [June 13, 2025, 8:26am UTC](https://community.sonarsource.com/t/s4158-fp-when-modifying-collection-via-alias/142631/5 "2025-06-13T08:26:33Z")

</div>

As far as I can tell, this is not the case. This reproducer results in a FP:

```csharp
public static class S4158
{
	public static HashSet<string> Repro(string[] args)
	{
		var set = new HashSet<string>();

		foreach (var arg in args)
		{
			var target = set;

			if (target.Contains(arg)) // S4158
			{
				throw new ArgumentException("duplicate");
			}

			target.Add(arg);
		}

		return set;
	}
}

public class S4158Test
{
	[Test]
	public void Not_empty()
	{
		var x = S4158.Repro(["a", "b", "c"]);
		x.Should().HaveCount(3);
	}
}

```

This test succeeds.

---

<div class="post-metadata">

**Author:** ![Mary](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mary/32/59106_2.png) [@Mary](https://community.sonarsource.com/u/Mary)\
**Post date:** [June 13, 2025, 8:31am UTC](https://community.sonarsource.com/t/s4158-fp-when-modifying-collection-via-alias/142631/6 "2025-06-13T08:31:56Z")

</div>

Yes - it adds the elements to the set via reference!  
I confirm this is a false positive.

Thanks Corniel for the heads up.

@a10r I’ll add a ticket to our backlog to fix this in a future sprint.  
Unfortunately the links are internal so I cannot share something with you to follow.
