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 S3900 FP: when object is validated with extension method #2639

Closed
tnoiretisa opened this issue Sep 12, 2019 · 3 comments · Fixed by #5218
Closed

Fix S3900 FP: when object is validated with extension method #2639

tnoiretisa opened this issue Sep 12, 2019 · 3 comments · Fixed by #5218
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@tnoiretisa
Copy link

tnoiretisa commented Sep 12, 2019

Description

When we use method extension to check collection are not null, S3900 raise :(

Repro steps

Have an method extension like that
public static bool IsNotNullOrEmpty<T>([ValidatedNotNull]this IEnumerable<T> src)
My code

public static void Test(List<string> lst)
{
    if (lst.IsNotNullOrEmpty())
    {
        foreach (string item in lst) <-- S3900 raise on "lst"
        {
            // Do something
        }
    }
}

Expected behavior

Don't raise rules

Actual behavior

Rules raise

Related information

  • SonarLint 4.12.0.0
  • Visual Studio 2017
@andrei-epure-sonarsource andrei-epure-sonarsource added this to the Support milestone Sep 26, 2019
@Corniel
Copy link
Contributor

Corniel commented Oct 3, 2019

I Could reproduce this. I also added the test on a internal method. In that case, the ValidatedNotNullAttribute is indeed recognized.

using System;
using System.Collections.Generic;
using System.Linq;

namespace SonarAnalyzer.Experiments.CSharp
{
public static class S3900
{
    public static void Extension(List<string> list)
    {
        if (list.IsNotNullOrEmpty())
        {
            foreach (string item in list) // Compliant
            {
                // Do something
            }
        }
    }

    public static void Method(List<string> list)
    {
        if (HasAny(list))
        {
            foreach (string item in list) // Compliant
            {
                // Do something
            }
        }
    }

    private static bool HasAny<T>([ValidatedNotNull]IEnumerable<T> src)
    {
        if (src is null)
        {
            return false;
        }
        return src.Any();
    }
}
public static class S3900Exentsions
{
    public static bool IsNotNullOrEmpty<T>([ValidatedNotNull]this IEnumerable<T> src)
    {
        if (src is null)
        {
            return false;
        }
        return src.Any();
    }
}

[AttributeUsage(AttributeTargets.Parameter)]
public sealed class ValidatedNotNullAttribute : Attribute { }
}

@christophe-zurn-sonarsource
Copy link
Contributor

@tnoiretisa @Corniel Thanks for reporting this and giving additional details !

@christophe-zurn-sonarsource christophe-zurn-sonarsource removed their assignment Nov 7, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource modified the milestones: Support, CFG + Symbolic Execution Nov 7, 2019
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title S3900 - FP when collection is validated with method extension Fix S3900 FP: when collection is validated with method extension Nov 7, 2019
andrei-epure-sonarsource pushed a commit that referenced this issue Nov 8, 2019
* Add S1128 FP repro for #2747

* Add S4143 FP repro for #2674

* Add S3900 FP repro for #2639
@costin-zaharia-sonarsource costin-zaharia-sonarsource removed this from the CFG + Symbolic Execution milestone Dec 2, 2019
@costin-zaharia-sonarsource costin-zaharia-sonarsource changed the title Fix S3900 FP: when collection is validated with method extension Fix S3900 FP: when object is validated with method extension Feb 17, 2020
@costin-zaharia-sonarsource costin-zaharia-sonarsource changed the title Fix S3900 FP: when object is validated with method extension Fix S3900 FP: when object is validated with extension method Feb 17, 2020
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. and removed Type: False Positive Rule IS triggered when it shouldn't be. labels Jun 25, 2021
@andrei-epure-sonarsource
Copy link
Contributor

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. Area: CFG/SE CFG and SE related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
None yet
7 participants