Skip to content

Commit

Permalink
Reproduce S2583 FP: indexer does not throw exception (#9585)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource authored Aug 2, 2024
1 parent 7387741 commit d5449cb
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Timers;
using System.Collections.Specialized;

namespace Tests.Diagnostics
{
Expand Down Expand Up @@ -4310,3 +4311,20 @@ void Test(List<Repro9425> aList)
}
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/9580
public class Repro_9580
{
public void IndexerReturnsNullInsteadOfThrowingException(NameValueCollection collection)
{
var element = collection["key"];
if (element != null)
{
Console.WriteLine(element.ToString());
}
if (collection.Count == 0) // Noncompliant - FP: the indexer with string argument returns null if the key is not found rather than throwing an exception,
{ // so at this point we can't know for sure that the collection is not empty.
Console.WriteLine("Empty!"); // Secondary - FP
}
}
}

0 comments on commit d5449cb

Please sign in to comment.