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 S3459 FP: Backing field with ref property #9383

Merged
merged 4 commits into from
Jun 4, 2024

Conversation

martin-strecker-sonarsource
Copy link
Contributor

Fixes #9106

@martin-strecker-sonarsource martin-strecker-sonarsource marked this pull request as ready for review June 4, 2024 09:05
Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round 1 (many more to come!)

@@ -184,7 +184,8 @@ private static ISet<ISymbol> GetAssignedMemberSymbols(IList<MemberUsage> memberU

if (PreOrPostfixOpSyntaxKinds.Contains(parentNode.Kind())
|| (parentNode is AssignmentExpressionSyntax assignment && assignment.Left == node)
|| (parentNode is ArgumentSyntax argument && (!argument.RefOrOutKeyword.IsKind(SyntaxKind.None) || TupleExpressionSyntaxWrapper.IsInstance(argument.Parent))))
|| (parentNode is ArgumentSyntax argument && (!argument.RefOrOutKeyword.IsKind(SyntaxKind.None) || TupleExpressionSyntaxWrapper.IsInstance(argument.Parent)))
|| RefExpressionSyntaxWrapper.IsInstance(parentNode))
Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too simple and introduces an FN (run with C# 9):

    private int _foo; // FN
    public int Foo => _foo;
    void Method()
    {
        _ = ref _foo;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a FN. If you get a reference to the field, you have write access to it and that write can happen anywhere. This is like calling SomeMethod(ref field) where you do not know what happens inside SomeMethod. We would need to do symbolic execution on the assigned variable to properly detect all scenarios.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you have access to the method where the ref was used. I accept that this is out of the scope of this ticket, but at least add a reproducer to document the limitation.

Copy link

sonarcloud bot commented Jun 4, 2024

Quality Gate Passed Quality Gate passed for 'Sonar .NET Java Plugin'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvements!

Copy link

sonarcloud bot commented Jun 4, 2024

@martin-strecker-sonarsource martin-strecker-sonarsource merged commit 778270c into master Jun 4, 2024
28 checks passed
@martin-strecker-sonarsource martin-strecker-sonarsource deleted the Martin/S3459_RefProp branch June 4, 2024 14:15
@CristianAmbrosini
Copy link
Contributor

Peach validation
Lost: 12 issues (TN)
Added: /

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S3459 FP: Backing field with ref property
3 participants