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

    Domanda

  • 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)

                                    {

                                                                            

                                       

                                    }

                                }

                            }

                        }

                    }

                }

    giovedì 14 maggio 2009 06:41

Risposte

  • 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")
    	{
    	}
    }
    • Contrassegnato come risposta Roahn Luo lunedì 18 maggio 2009 01:48
    giovedì 14 maggio 2009 15:01

Tutte le risposte

  • Why do you want to write such a rule?
    Ewald - Please remember to mark the replies as answers if they help.
    giovedì 14 maggio 2009 10:36
  • 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")
    	{
    	}
    }
    • Contrassegnato come risposta Roahn Luo lunedì 18 maggio 2009 01:48
    giovedì 14 maggio 2009 15:01
  • 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?
    giovedì 19 aprile 2012 13:44
  • 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.

    giovedì 19 aprile 2012 13:59