Skip to content

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

Open
@nalka0

Description

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

Metadata

Assignees

No one assigned

    Labels

    Area: C#C# rules related issues.Type: False PositiveRule IS triggered when it shouldn't be.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions