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