Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S1168 FP: when returning null in LINQ lambda #2461

Closed
Thieum opened this issue Jun 19, 2019 · 1 comment · Fixed by #2821
Closed

Fix S1168 FP: when returning null in LINQ lambda #2461

Thieum opened this issue Jun 19, 2019 · 1 comment · Fixed by #2821
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@Thieum
Copy link

Thieum commented Jun 19, 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:

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.

Known workarounds

ignore S1168 around the lambda expression.

Related information

  • SonarLint for Visual Studio 4.10.0.9867 / Sonar C# 7.14
  • Visual Studio Enterprise 2019 16.1.3
@andrei-epure-sonarsource andrei-epure-sonarsource added this to the Support milestone Jul 30, 2019
costin-zaharia-sonarsource added a commit that referenced this issue Oct 14, 2019
@costin-zaharia-sonarsource costin-zaharia-sonarsource added Area: C# C# rules related issues. Area: Rules Type: False Positive Rule IS triggered when it shouldn't be. and removed Status: Needs Review labels Oct 14, 2019
@antoinebj
Copy link

Also encountered this

@christophe-zurn-sonarsource christophe-zurn-sonarsource modified the milestones: Support, 8.1 Nov 21, 2019
@christophe-zurn-sonarsource 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants