Since the update, I now see more bugs when reviewing the c# code.
Is there any documentation that explains why this change?
Example: S2259 # Null pointers should not be dereferenced
In that rule, this code did not jump before, something changed.
if (array.Any())
{
TreeItem itemMain = array.FirstOrDefault();
itemMain.Selected = OneHelper.SetSelectedFolder(itemMain);
}
itemMain this variable is the one marked by that rule
With each new release, some rules get smarter. It seems that there have been several iterations on S2259 since the 9.9 release, so it’s not surprising to me that more issues are found.
Do you disagree with some of them? (I’ve never done C#, so it’s not obvious to me whether your code snippet is a FP or not, altho I’m leaning that way…?)
I confirm that the issue was raised incorrectly, we don’t take into account .Any().
I created a ticket to track this issue.
Know that it will be part of the improvement of S4158 in this ticket, especially about:
Learn from well-known (extension-) methods like list.Any()
Checking if the array is not empty with .Any() using .FirstOrDefault() is not necessary since you already know that .FirstOrDefault() will never return null (or default), as you pointed out.
Because of the .FirstOrDefault(), we assume that itemMain can be null; thus, we raise S2259.
Then it is relevant to update your snippet to this (assuming array is of type Array):
if (array.Any())
{
TreeItem itemMain = array[0]; // Using .First() would raise S6608
itemMain.Selected = SetSelectedFolder(itemMain);
}
Beyond the proposed case and how we should solve it, the unwanted effect in production was not good when updating the version.
Originally we had fewer bugs and after the update they increased a lot, and we believe that beyond First or FirstOrDefault that code will never fail, the issue is semantic.