Need to add custom rule for 'checking empty finally block ' in FXCop

Заблокировано Need to add custom rule for 'checking empty finally block ' in FXCop

  • 14 мая 2009 г. 6:41
     
     

    This is the code i have written for implementing the above mentioned rule.It is not complete.Please let me know how can i find the empty finally block??
    if
    (member.NodeType == NodeType.Method)

                {

                    if (method != null)

                    {

                        if (method.Body.NodeType==NodeType.Block)

                        {

                            if (method.Body.Statements.Count!=0)

                            {

                                for (int i = 0; i < method.Body.Statements.Count; i++)

                                {

                                    StatementCollection sc = method.Body.Statements;

                                    if (sc[i].NodeType == NodeType.Try)

                                    {

                                                                            

                                       

                                    }

                                }

                            }

                        }

                    }

                }

Все ответы

  • 14 мая 2009 г. 10:36
     
     
    Why do you want to write such a rule?
    Ewald - Please remember to mark the replies as answers if they help.
  • 14 мая 2009 г. 15:01
     
     Отвечено

    Here's a partial implementation:

    public override ProblemCollection Check(Member member)
    {
    	Method method = member as Method;
    	if (method != null) this.Visit(member);
    
    	return this.Problems;
    }
    
    public override void VisitFinally(FinallyNode finallyClause)
    {
    	if (finallyClause != null)
    	{
    		if (!this.ContainsActiveStatements(finallyClause.Block))
    		{
    			this.Problems.Add(new Problem(this.GetResolution(), finallyClause));
    		}
    	}
    
    	base.VisitFinally(finallyClause);
    }
    
    private bool ContainsActiveStatements(Block block)
    {
    	bool result = false;
    
    	for (int i = 0; (!result) && (i < block.Statements.Count); i++)
    	{
    		Statement statement = block.Statements[i];
    		Block childBlock = statement as Block;
    		if (childBlock != null)
    		{
    			result = this.ContainsActiveStatements(childBlock);
    		}
    		else
    		{
    			switch (statement.NodeType)
    			{
    				case NodeType.Nop:
    				case NodeType.EndFinally:
    					// Not interesting.
    					break;
    				default:
    					result = true;
    					break;
    			}
    		}
    	}
    
    	return result;
    }
    The big problem is defining what you consider to be "empty", which is pretty tough under the introspection engine.  For example, the following code doesn't actually do anything in the finally block, but it does contain an assignment statement:

    try
    {
    	Console.WriteLine("foo");
    }
    catch (Exception ex)
    {
    	Console.WriteLine(ex.ToString());
    }
    finally
    {
    	if ("a" == "a")
    	{
    	}
    }
    • Помечено в качестве ответа Roahn LuoModerator 18 мая 2009 г. 1:48
    •  
  • 19 апреля 2012 г. 13:44
     
     
    If you have nothing constructive to add, then please move on to the next thread.  How can you be so arrogant as to question why someone wants to achieve a certain behavior?  You are on the TFS Product Team?  Really?
  • 19 апреля 2012 г. 13:59
     
     
    I am sorry if you feel offended by the post. This post is almost 3 years old when I started contributing to the forums and not working for Microsoft yet. I just wanted to understand the scenario to maybe another solution.

    Please remember to mark the replies as answers if they help.