I do not think you can do this at the SyntaxToken level. At first I thought the semantic model would help you here, but in both cases the symbols refer to the same thing, so you cannot use this to differentiate.
However, what you can do is simply examine SimpleAssignmentExpression , check if both operands are identifiers, and check their equivalence through the same SyntaxFactory.AreEquivalent() that Marcus mentioned. I got to this (see this point for a complete LINQPad query):
Let's say you write this method:
private static bool IsAssignmentBad(AssignmentExpressionSyntax assignmentNode) { if (!assignmentNode.IsKind(SyntaxKind.SimpleAssignmentExpression)) { return false; } var lhs = assignmentNode.Left; if (!lhs.IsKind(SyntaxKind.IdentifierName)) { return false; } var rhs = assignmentNode.Right; if (!rhs.IsKind(SyntaxKind.IdentifierName)) { return false; } return SyntaxFactory.AreEquivalent(lhs, rhs); }
Then starting with this gives what you want, I think:
var tree = CSharpSyntaxTree.ParseText( @"public class Thing { public int Item { get; } public Thing(int item) { Item = Item; // note incorrect assignment: rhs should be item, the passed-in arg, hence analyzer should warn } public Thing(Thing other) { Item = other.Item; // correct assignment, should NOT trigger analyzer } }"); var root = tree.GetRoot(); var incorrectAssignment = root.DescendantNodes().OfType<AssignmentExpressionSyntax>().First(); var correctAssignment = root.DescendantNodes().OfType<AssignmentExpressionSyntax>().Last(); var b1 = IsAssignmentBad(correctAssignment);
source share