From b9dbe0b6b735483e56d7e50dab8020376eb2f812 Mon Sep 17 00:00:00 2001 From: Cristian <67206480+CristianAmbrosini@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:55:05 +0200 Subject: [PATCH] NET-371 S2583: Add reproducer for #9671 (#225) --- .../ConditionEvaluatesToConstant.CSharp12.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp12.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp12.cs index da77db99faa..de9b44d0f2b 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp12.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp12.cs @@ -1,4 +1,6 @@ using System; +using System.Linq; +using System.Collections.Generic; class Sample { @@ -49,4 +51,27 @@ void CollectionExpressions(int[] array) if (unknownLength2.Length == 0) { } // Compliant if (unknownLength2.Length < 0) { } // Noncompliant } + + // Repro for https://github.com/SonarSource/sonar-dotnet/issues/9671 + void ListFilledInLocalFunction() + { + List list = new(); + foreach (var item in Enumerable.Range(0, 5)) + { + if (item % 2 == 0) + { + LocalFunction(item); + } + + void LocalFunction(int added) + { + list.Add(added); + } + } + + if (list.Count > 0) // Noncompliant FP + { + Console.WriteLine("This code is reachable"); // Secondary FP + } + } }