开发者

FxCop, Finding "If-Else" statements

I am using FxCop and coded the following:

public override void VisitBinaryExpression(BinaryExpression binaryExpression)
{
    if (binaryExpression != null)
    {
        if (binaryExpression.Op开发者_C百科erand1 is MethodCall || binaryExpression.Operand2 is MethodCall)
        {
            Problem p = new Problem(GetResolution(null), binaryExpression);
            this.Problems.Add(p);
        }
    }
    base.VisitBinaryExpression(binaryExpression);
}

However, addition is also considered as a Binary Expression too. I am trying to block:

if ( myFunc()){
// cool code
}

And using this one instead:

bool b = myFunc();
if ( b){
// cool code
}

How can I find the if-else statements in FxCop and determine that the expression inside does not have any function calls?


You need to be looking for the Branch node type, then looking at the condition property.

A tool I found invaluable when working on creating rules is using The introspector found here. It allows you to explore the code tree, displaying the note types and properties as you go. Create a program with an if statement in it, then expand until you see a , then take a look at the properties you should be checking for.

I would override the visit branch method, set a bool, then visit the 'condition nodes', then reset it afterwards. Then in your visitbinary you can only apply your rule if the bool is set (i.e. you're in the condition of a branch statement).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜