You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the rule S1168 raises an issue when returning null from within a lambda declared inside of a method returning a collection.
Repro steps
this code will demonstrate the false positive:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp14
{
static class Program
{
class ABC
{
}
static void Main()
{
var test = Test();
Console.WriteLine(test);
}
static IEnumerable<ABC> Test()
{
var list = new List<ABC>();
return list.Select(o =>
{
if (o != null)
{
return o;
}
return null;
});
}
}
}
Expected behavior
S1168 shouldn't be raised inside the scope of the lambda, as the return of the lambda is different than the one of the encapsulating method.
There could be legitimate case where the return of a lambda is a collection, where this rule could apply.
Both positive and negative results in lambda should be tested.
Actual behavior
S1168 is raised inside the lambda considering the result of the encapsulating method instead of the result of the lambda.
christophe-zurn-sonarsource
changed the title
False positive on S1168 - returning null in LINQ lambda
Fix S1168 FP: when returning null in LINQ lambda
Dec 6, 2019
Description
Regression of #761
Currently the rule S1168 raises an issue when returning null from within a lambda declared inside of a method returning a collection.
Repro steps
this code will demonstrate the false positive:
Expected behavior
S1168 shouldn't be raised inside the scope of the lambda, as the return of the lambda is different than the one of the encapsulating method.
There could be legitimate case where the return of a lambda is a collection, where this rule could apply.
Both positive and negative results in lambda should be tested.
Actual behavior
S1168 is raised inside the lambda considering the result of the encapsulating method instead of the result of the lambda.
Known workarounds
ignore S1168 around the lambda expression.
Related information
The text was updated successfully, but these errors were encountered: