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 S1450 FP: private field is used in several methods #9672

Open
nalka0 opened this issue Sep 30, 2024 · 1 comment
Open

Fix S1450 FP: private field is used in several methods #9672

nalka0 opened this issue Sep 30, 2024 · 1 comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.

Comments

@nalka0
Copy link
Contributor

nalka0 commented Sep 30, 2024

Description

S1450 is raised on the following snippet despite foundMultiple being used on both methods

Repro steps

private class LookForSinglePropertyAccessExpressionVisitor : ExpressionVisitor
{
    private PropertyInfo foundProperty;

    private bool foundMultiple; // S1450 FP here (but it's used on both methods)

    public PropertyInfo GetUsedProperty(Expression expression)
    {
        foundProperty = null;
        foundMultiple = false;
        Visit(expression);
        if (foundMultiple)
        {
            throw new ArgumentException($"{expression} contains more than one property access", nameof(expression));
        }

        return foundProperty ?? throw new ArgumentException($"{expression} contains no property access", nameof(expression));
    }

    protected override Expression VisitMember(MemberExpression node)
    {
        if (node.Member is PropertyInfo prop)
        {
            if (foundProperty != null)
            {
                foundMultiple = true;
            }

            foundProperty = prop;
        }

        return base.VisitMember(node);
    }
}

Expected behavior

No occurrence of S1450 in the above snippet

Actual behavior

S1450 is raised on the above snippet despite foundMultiple being used on both methods

Known workarounds

None

Related information

  • Microsoft Visual Studio Community 2022 - Version 17.11.4
  • .NET 6 and 8
  • SonarScanner for .NET version 9.0.0
  • Windows 11
@CristianAmbrosini
Copy link
Contributor

Hi @nalka0!

It's a FP, indeed.
I discovered that we already have a reproducer for this on our repo.
I'll make sure to add a link to this since it seems we don't have a ticket for it yet.

Thanks,

@CristianAmbrosini CristianAmbrosini added Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be. labels Oct 2, 2024
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

No branches or pull requests

2 participants