Roslyn Find All assignments made to a Field

Viewed 1231

I'm trying write an analyzer, I need to find all assignments made to a field using Roslyn.

private async static Task<bool> VariableDoesNotMutate(SyntaxNodeAnalysisContext context, VariableDeclaratorSyntax firstVariable)
{
    var variableSymbol = context.SemanticModel.GetDeclaredSymbol(firstVariable);
    var references = await SymbolFinder.FindReferencesAsync(variableSymbol, context.GetSolution());

    foreach (var reference in references)
    {
        //How do I check for assignment?
    }

    //need to filter by assignments
    return references.Count() > 1;
}

I heard using the symbolFinder was correct, but I'm not sure how to do this.
The symbol finder requires a solution, which I only have access to through a hack, so I'm assuming there is another way to do this.

Issues:

  1. When I try to Find all references to a variable only the Declaration is returned an I do not find any other references how can I fix this?

  2. Once I have a reference How can I determine if it's an assignment?

1 Answers
Related