确定两个SyntaxTokens是否相同

时间:2016-09-28 07:51:44

标签: c# roslyn roslyn-code-analysis

以下面的代码为例:

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
    }
}

我正在写一个Roslyn分析器来检测和报告这些可能错误的自我分配案例,以下相关部分:

public override void Initialize(AnalysisContext context)
{
    context.RegisterSyntaxNodeAction(MistakenSelfAssignment, SyntaxKind.SimpleAssignmentExpression);
}

private static void MistakenSelfAssignment(SyntaxNodeAnalysisContext context)
{
    var assignment = context.Node as AssignmentExpressionSyntax;
    if (assignment == null)
    {
        return;
    }

    var leftToken = GetIdentifierToken(assignment.Left);
    var rightToken = GetIdentifierToken(assignment.Right);

    if (leftToken != null && leftToken.IsEquivalentTo(rightToken)) // this never works
    {
        var diagnostic = Diagnostic.Create(Rule, assignment.GetLocation());
        context.ReportDiagnostic(diagnostic);
    }
}

private static SyntaxToken GetIdentifierToken(ExpressionSyntax syntax)
{
    var identifierName = syntax as IdentifierNameSyntax;
    if (identifierName != null)
    {
        return identifierName.Identifier;
    }

    var identifierAccess = syntax as MemberAccessExpressionSyntax;
    if (identifierAccess != null)
    {
        return identifierAccess.Name.Identifier;
    }

    return default(SyntaxToken);
}

但是我无法弄清楚如何确定赋值的LHS和RHS是否是同一个令牌 - SyntaxToken.IsEquivalentTo似乎是我想要的方法,但它总是返回false,{{1 }和SyntaxToken.Equals

确定令牌是否指向自身的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

我认为你不能在SyntaxToken级别上这样做。起初,我认为语义模型会对你有帮助,但在这两种情况下,符号都指的是相同的东西,所以你不能用它来区分。

但是,您可以做的只是调查SimpleAssignmentExpression,检查两个操作数是否都是标识符,并通过Marcus提到的相同SyntaxFactory.AreEquivalent()检查它们的等效性。我得到了这个(请参阅this gist获取完整的LINQPad查询):

让我们说你写这个方法:

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);
}

然后用它运行它给出了你想要的东西,我想:

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); // doesn't consider the assignment bad
var b2 = IsAssignmentBad(incorrectAssignment); // this one does