Fix S4158 FP: collection filled by another function
CristianAmbrosini opened this issue · 1 comments
CristianAmbrosini commented
Source: https://community.sonarsource.com/t/false-positive-for-s4158/117480
S4158 is incorrectly raised when a list is filled by another function and then iterated:
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);
}
}
Tim-Pohlmann commented
The cause of the problem is that the invocation processor is only resetting field states. Properties are missing from the logic: