FxCop,寻找“If-Else”声明
我正在使用 FxCop 并编码如下:
public override void VisitBinaryExpression(BinaryExpression binaryExpression)
{
if (binaryExpression != null)
{
if (binaryExpression.Operand1 is MethodCall || binaryExpression.Operand2 is MethodCall)
{
Problem p = new Problem(GetResolution(null), binaryExpression);
this.Problems.Add(p);
}
}
base.VisitBinaryExpression(binaryExpression);
}
但是,加法也被视为二进制表达式。我正在尝试阻止:
if ( myFunc()){
// cool code
}
并使用这个来代替:
bool b = myFunc();
if ( b){
// cool code
}
如何在 FxCop 中找到 if-else 语句并确定其中的表达式没有任何函数调用?
I am using FxCop and coded the following:
public override void VisitBinaryExpression(BinaryExpression binaryExpression)
{
if (binaryExpression != null)
{
if (binaryExpression.Operand1 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要查找分支节点类型,然后查看条件属性。
我发现在创建规则时非常有用的工具是使用此处找到的内省器。它允许您探索代码树,随时显示注释类型和属性。创建一个包含 if 语句的程序,然后展开直到看到 ,然后查看您应该检查的属性。
我将重写访问分支方法,设置一个布尔值,然后访问“条件节点”,然后重置它。然后在您的访问二进制文件中,您只能在设置了布尔值的情况下应用您的规则(即您处于分支语句的条件下)。
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).