# S4158 and Actions

**URL:** <https://community.sonarsource.com/t/s4158-and-actions/117822>\
**Category:** Report False-positive / False-negative...\
**Created:** [June 25, 2024, 8:44am UTC](https://community.sonarsource.com/t/s4158-and-actions/117822 "2024-06-25T08:44:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MBender](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mbender/32/59283_2.png) [@MBender](https://community.sonarsource.com/u/MBender)\
**Post date:** [June 25, 2024, 8:44am UTC](https://community.sonarsource.com/t/s4158-and-actions/117822/1 "2024-06-25T08:44:39Z")

</div>

Then tell us:

- What language is this for?  
C#
- Which rule?  
S4158
- Why do you believe it’s a false-positive/false-negative?  
The collection will not be empty (assuming code works correctly of course)
- Are you using
  - SonarLint - 8.0.0.92083 for VS 2022

- How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)  
A crude example showcasing how the S4158 will be triggered, even if the collection is guaranteed to contain elements at that point.

```auto
        private void S4158Test()
        {
            var collection = new List<string>();
            var action = () => collection.Add("test");
            action();
            Console.WriteLine(collection.Exists(x => x == "test"));
        }

```

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [June 25, 2024, 8:51am UTC](https://community.sonarsource.com/t/s4158-and-actions/117822/2 "2024-06-25T08:51:26Z")

</div>

Hey there.

Thanks for the report. I think you’re facing one of these known false-postives:

> <https://github.com/SonarSource/sonar-dotnet/issues/4478>
>
> \### Description
> 
> A S4158 violation is incorrectly reported when elements to a …collection are added in a local function.
> 
> \### Repro steps
> 
> \`\`\`
> public static class Program
> {
> public static void Main()
> {
> var list = new List\<String\>();
> 
> void AddElement()
> {
> list.Add("Item1");
> }
> 
> AddElement();
> 
> for (int i = 0; i \< list.Count; i++)
> {
> var element = list\[i\]; // SonarLint reports violation of rule S4158 here.
> Console.Out.WriteLine(element);
> }
> }
> }
> \`\`\`
> 
> \### Expected behavior
> 
> Since the collection is clearly not empty in the code sample, SonarLint should not report a violation of rule S4158.
> 
> \### Actual behavior
> 
> SonarLint reports a violation of rule S4158.
> 
> \### Known workarounds
> 
> None.
> 
> \### Related information
> 
> \* C#/VB.NET Plugins version: SonarLint for Visual Studio 2019, Version 4.35.0.32570
> \* Visual Studio version: Visual Studio 2019, Version 16.9.0
> \* MSBuild / dotnet version: MSBuild 16.9.0.11203 / .NET Framework 4.6.2
> \* SonarScanner for .NET version (if used)
> \* Operating System: Windows 10 x64

> <https://github.com/SonarSource/sonar-dotnet/issues/9444>
>
> Source: https://community.sonarsource.com/t/false-positive-for-s4158/117480
> 
> S…4158 is incorrectly raised when a list is filled by another function and then iterated:
> \`\`\`cs
> class CollectionFilledByAnotherFunction
> {
> List\<int\> Numbers { get; set; }
> 
> void Initialize()
> {
> Numbers = new List\<int\>();
> InitializeNumbers();
> foreach (var number in Numbers) // Noncompliant FP
> {
> System.Console.WriteLine(number);
> }
> }
> 
> void InitializeNumbers()
> {
> Numbers.Add(1);
> Numbers.Add(2);
> Numbers.Add(3);
> }
> }
> \`\`\`

---

<div class="post-metadata">

**Author:** ![MBender](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mbender/32/59283_2.png) [@MBender](https://community.sonarsource.com/u/MBender)\
**Post date:** [June 25, 2024, 8:54am UTC](https://community.sonarsource.com/t/s4158-and-actions/117822/3 "2024-06-25T08:54:06Z")

</div>

You’re right! This appears to be a duplicate.

I guess I reported this because the first time I’ve seen this issue was… some time ago, and I’ve seen SonarLint updated a few times since then with this still being a false-positive so I figured I’d report this. 😅

Anyway - feel free to delete this, since this is clearly a duplicate.

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [June 25, 2024, 8:58am UTC](https://community.sonarsource.com/t/s4158-and-actions/117822/4 "2024-06-25T08:58:42Z")

</div>

No worries. We appreciate the report anyways. 🙂 It helps us prioritize issues if we know more users are reporting them.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [July 2, 2024, 8:59am UTC](https://community.sonarsource.com/t/s4158-and-actions/117822/5 "2024-07-02T08:59:04Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
