SonarLint for Visual Studio 2017 incorrectly asks to use more general type than IList

10 views
Skip to first unread message

tonnit...@gmail.com

unread,
Aug 24, 2017, 5:48:28 AM8/24/17
to SonarLint
Hi there,

I'm using SonarLint in Visual Studio 2017 with C# code. I have a piece of code that looks like the following:

public static void DoSomething(IList<int> numbers)
{
   
if (numbers.Count != 0)
   
{
       
var myNumber = numbers[0];
       
Console.WriteLine(myNumber);
   
}
}


SonarLint for Visual Studio 2017 reports that we should use a more generic type (like ICollection<int>) for numbers. This would be true if we wouldn't access it using a index.

When removing the if statement, like below, SonarLint does not complain anymore, so it seems like it does not see that we access the collection using the index when it's wrapped in an if statement.

public static void DoSomething(IList<int> numbers)
{
   
var myNumber = numbers[0];
   
Console.WriteLine(myNumber);
}

Am I missing something here, or did I discover a bug?

Regards,

Tonni Tielens

Amaury Leve

unread,
Aug 24, 2017, 5:59:12 AM8/24/17
to tonnit...@gmail.com, SonarLint
Hi Tonni,

Thanks for reporting this issue. It is already tracked here: https://github.com/SonarSource/sonar-csharp/issues/640

Cheers,
Amaury

--
You received this message because you are subscribed to the Google Groups "SonarLint" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarlint+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarlint/f89d318d-a2db-4023-9255-bc9a9ea6955c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Amaury Levé | SonarSource

Software Developer - .Net Team

http://sonarsource.com


Are you using SonarLint in your IDE? 
Reply all
Reply to author
Forward
0 new messages