Author: spouliot
Date: 2008-01-03 10:42:02 -0500 (Thu, 03 Jan 2008)
New Revision: 92182

Modified:
   
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUncalledPrivateCodeRule.cs
   
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUninstantiatedInternalClassesRule.cs
   trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
Log:
2008-01-03  Sebastien Pouliot  <[EMAIL PROTECTED]> 

        * AvoidUncalledPrivateCodeRule.cs: Updated/simplified to use rocks.
        * AvoidUninstantiatedInternalClassesRule.cs: Fix rule to handle
        enums correctly.



Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUncalledPrivateCodeRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUncalledPrivateCodeRule.cs
       2008-01-03 15:40:05 UTC (rev 92181)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUncalledPrivateCodeRule.cs
       2008-01-03 15:42:02 UTC (rev 92182)
@@ -6,7 +6,7 @@
 //     Sebastien Pouliot  <[EMAIL PROTECTED]>
 //
 // Copyright (c) <2007> Nidhi Rawal
-// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2007-2008 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to 
deal
@@ -31,6 +31,7 @@
 using Mono.Cecil;
 using Mono.Cecil.Cil;
 using Gendarme.Framework;
+using Gendarme.Framework.Rocks;
 
 namespace Gendarme.Rules.Performance {
 
@@ -39,18 +40,8 @@
                // we should move these methods into an helper class (e.g. 
MethodHelper)
                // and reuse them in other rules
 
-               public bool IsStaticConstructor (MethodDefinition md)
+               public static bool IsSerializationConstructor (MethodDefinition 
md)
                {
-                       return (md.IsStatic && md.IsConstructor);
-               }
-
-               public bool IsEntryPoint (MethodDefinition md)
-               {
-                       return (md == 
md.DeclaringType.Module.Assembly.EntryPoint);
-               }
-
-               public bool IsSerializationConstructor (MethodDefinition md)
-               {
                        if (!md.IsConstructor || (md.Parameters.Count != 2))
                                return false;
 
@@ -60,41 +51,8 @@
                        return (md.Parameters[1].ParameterType.Name == 
"System.Runtime.Serialization.StreamingContext");
                }
                
-               // static [void|int] Main ()
-               // static [void|int] Main (string[] args)
-               public bool IsMain (MethodDefinition md)
+               public static bool IsExplicitImplementationOfInterface 
(MethodDefinition method)
                {
-                       // Main must be static
-                       if (!md.IsStatic)
-                               return false;
-
-                       if (md.Name != "Main")
-                               return false;
-
-                       // Main must return void or int
-                       switch (md.ReturnType.ReturnType.Name) {
-                       case "Void":
-                       case "Int":
-                               // ok, continue checks
-                               break;
-                       default:
-                               return false;
-                       }
-
-                       switch (md.Parameters.Count) {
-                       case 0 :
-                               // Main (void)
-                               return true;
-                       case 1:
-                               // Main (string[] args)
-                               return (md.Parameters[0].ParameterType.Name == 
"String[]");
-                       default:
-                               return false;
-                       }
-               }
-
-               public bool IsExplicitImplementationOfInterface 
(MethodDefinition method)
-               {
                        // quick out if the name doesn't include a .
                        if (!method.Name.Contains ("."))
                                return false;
@@ -107,33 +65,25 @@
                        return false;
                }
                
-               public bool IsGeneratedByCompiler (MethodDefinition method)
-               {
-                       foreach (CustomAttribute custom in 
method.CustomAttributes) {
-                               if (custom.Constructor.DeclaringType.FullName 
== "System.Runtime.CompilerServices.CompilerGeneratedAttribute")
-                                       return true;
-                       }
-                       return false;
-               }
-
                //
 
                public MessageCollection CheckMethod (MethodDefinition method, 
Runner runner)
                {
                        // #1 - rule doesn't apply to static ctor
-                       if (IsStaticConstructor (method))
+                       if (method.IsStatic && method.IsConstructor)
                                return runner.RuleSuccess;
 
                        // #2 - rule doesn't apply if the method is the 
assembly entry point
-                       if (IsEntryPoint (method))
+                       if (method.IsEntryPoint ())
                                return runner.RuleSuccess;
 
-                       if (IsMain (method))
+                       // #3 - rule doesn't apply to Main
+                       if (method.IsMain ())
                                return runner.RuleSuccess;
 
                        // #4 - rule doesn't apply if the method is generated by
                        // the compiler.
-                       if (IsGeneratedByCompiler (method))
+                       if (method.IsCompilerGenerated ())
                                return runner.RuleSuccess;
 
                        // ok, the rule applies
@@ -170,7 +120,7 @@
                        return runner.RuleSuccess;
                }
 
-               private bool CheckAssemblyForMethodUsage (AssemblyDefinition 
ad, MethodDefinition md)
+               private static bool CheckAssemblyForMethodUsage 
(AssemblyDefinition ad, MethodDefinition md)
                {
                        // scan each module
                        foreach (ModuleDefinition module in ad.Modules) {
@@ -183,7 +133,7 @@
                        return false;
                }
 
-               private bool CheckTypeForMethodUsage (TypeDefinition td, 
MethodDefinition md)
+               private static bool CheckTypeForMethodUsage (TypeDefinition td, 
MethodDefinition md)
                {
                        // check every constructor for the type
                        foreach (MethodDefinition ctor in td.Constructors) {
@@ -204,7 +154,7 @@
                        return false;
                }
 
-               private bool CheckMethodUsage (MethodDefinition method, 
MethodDefinition md)
+               private static bool CheckMethodUsage (MethodDefinition method, 
MethodDefinition md)
                {
                        if (!method.HasBody)
                                return false;

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUninstantiatedInternalClassesRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUninstantiatedInternalClassesRule.cs
     2008-01-03 15:40:05 UTC (rev 92181)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUninstantiatedInternalClassesRule.cs
     2008-01-03 15:42:02 UTC (rev 92182)
@@ -104,9 +104,14 @@
                        if (!method.HasBody)
                                return;
 
+                       // this is needed in case we return an enum
+                       TypeReference t = method.ReturnType.ReturnType;
+                       if (!list.Contains (t))
+                               list.Add (t);
+
                        foreach (Instruction ins in method.Body.Instructions) {
                                if (ins.OpCode == OpCodes.Newobj) {
-                                       TypeReference t = ins.Operand as 
TypeReference;
+                                       t = ins.Operand as TypeReference;
                                        if (t == null) {
                                                MethodReference m = ins.Operand 
as MethodReference;
                                                if (m != null)

Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/ChangeLog     
2008-01-03 15:40:05 UTC (rev 92181)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/ChangeLog     
2008-01-03 15:42:02 UTC (rev 92182)
@@ -1,3 +1,9 @@
+2008-01-03  Sebastien Pouliot  <[EMAIL PROTECTED]> 
+
+       * AvoidUncalledPrivateCodeRule.cs: Updated/simplified to use rocks.
+       * AvoidUninstantiatedInternalClassesRule.cs: Fix rule to handle
+       enums correctly.
+
 2008-01-03  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * AvoidReturningArraysOnPropertiesRule.cs: New. Rule to warn for 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to