Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

{
assignedMembers.Add(memberSymbol);
assignedMembers.Add(memberSymbol.OriginalDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void Print()
// Reproducer: https://github.com/SonarSource/sonar-dotnet/issues/9106
public class Repro_9106
{
private int _foo; // Noncompliant FP
private int _foo; // Compliant

public ref int Foo => ref _foo;
}