Author: spouliot
Date: 2008-02-03 15:03:50 -0500 (Sun, 03 Feb 2008)
New Revision: 94718

Modified:
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/AvoidUninstantiatedInternalClassesRule.cs
   trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/DontIgnoreMethodResultRule.cs
Log:
2008-02-03  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * AvoidUninstantiatedInternalClassesRule.cs: Reworked to reduce
        false positives.
        * DontIgnoreMethodResultRule.cs: Add more exceptions to reduce
        false positives.



Modified: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/AvoidUninstantiatedInternalClassesRule.cs
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/AvoidUninstantiatedInternalClassesRule.cs
        2008-02-03 20:03:41 UTC (rev 94717)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/AvoidUninstantiatedInternalClassesRule.cs
        2008-02-03 20:03:50 UTC (rev 94718)
@@ -46,9 +46,6 @@
                                return true;
 
                        // some stuff, like arrays, is nested under 
<PrivateImplementationDetails>
-                       if (type.IsNested)
-                               return type.IsGeneratedCode () || 
type.DeclaringType.IsGeneratedCode ();
-
                        return type.IsGeneratedCode ();
                }
 
@@ -67,64 +64,88 @@
 
                        foreach (ModuleDefinition module in assembly.Modules) {
                                foreach (TypeDefinition type in module.Types) {
-                                       foreach (MethodDefinition method in 
type.Constructors) {
-                                               ProcessMethod (method, list);
-                                       }
-                                       foreach (MethodDefinition method in 
type.Methods) {
-                                               ProcessMethod (method, list);
-                                       }
+                                       ProcessType (assembly, type, list);
                                }
                        }
 
                        cache.Add (assembly, list);
                }
 
-               private static void ProcessMethod (MethodDefinition method, 
List<TypeReference> list)
+               private static void AddType (List<TypeReference> list, 
TypeReference type)
                {
-                       // this is needed in case we return an enum (or 
something mapped to p/invoke)
+                       // we're interested in the array element type, not the 
array itself
+                       if (type.IsArray ())
+                               type = type.GetOriginalType ();
+
+                       // only keep stuff from this assembly, which means we 
have a TypeDefinition (not a TypeReference)
+                       // and types that are not visible outside the assembly 
(since this is what we check for)
+                       TypeDefinition td = (type as TypeDefinition);
+                       if ((td != null) && !td.IsVisible ())
+                               list.AddIfNew (type);
+               }
+
+               public static void ProcessType (AssemblyDefinition assembly, 
TypeDefinition type, List<TypeReference> list)
+               {
+                       foreach (FieldDefinition field in type.Fields) {
+                               TypeReference t = field.FieldType;
+                               // don't add the type itself (e.g. enums)
+                               if (type != t)
+                                       AddType (list, t);
+                       }
+                       foreach (MethodDefinition method in type.Constructors) {
+                               ProcessMethod (method, list);
+                       }
+                       foreach (MethodDefinition method in type.Methods) {
+                               ProcessMethod (method, list);
+                       }
+               }
+
+               public static void ProcessMethod (MethodDefinition method, 
List<TypeReference> list)
+               {
+                       // this is needed in case we return an enum, a struct 
or something mapped 
+                       // to p/invoke (i.e. no ctor called). We also need to 
check for arrays.
                        TypeReference t = method.ReturnType.ReturnType;
-                       if (!list.Contains (t))
-                               list.Add (t);
+                       AddType (list, t);
 
                        // an "out" from a p/invoke must be flagged
                        foreach (ParameterDefinition parameter in 
method.Parameters) {
-                               if (parameter.IsOut) {
-                                       // we don't want the reference (&) on 
the type
-                                       t = 
parameter.ParameterType.GetOriginalType ();
-                                       if (!list.Contains (t))
-                                               list.Add (t);
-                               }
+                               // we don't want the reference (&) on the type
+                               t = parameter.ParameterType.GetOriginalType ();
+                               AddType (list, t);
                        }
 
                        if (!method.HasBody)
                                return;
 
+                       // add every type of variables we use
                        foreach (VariableDefinition variable in 
method.Body.Variables) {
                                t = variable.VariableType;
-                               if (!list.Contains (t))
-                                       list.Add (t);
+                               AddType (list, t);
                        }
 
+                       // add every type we create or refer to (e.g. loading 
fields from an enum)
                        foreach (Instruction ins in method.Body.Instructions) {
-                               if (ins.OpCode == OpCodes.Newobj) {
-                                       t = ins.Operand as TypeReference;
-                                       if (t == null) {
-                                               MethodReference m = ins.Operand 
as MethodReference;
-                                               if (m != null)
-                                                       t = m.DeclaringType;
-                                       }
+                               if (ins.Operand == null)
+                                       continue;
 
-                                       if ((t != null) && !list.Contains (t))
-                                               list.Add (t);
+                               t = ins.Operand as TypeReference;
+                               if (t == null) {
+                                       // this covers MethodReference and 
FieldReference
+                                       MemberReference m = ins.Operand as 
MemberReference;
+                                       if (m != null)
+                                               t = m.DeclaringType;
                                }
+
+                               if (t != null)
+                                       AddType (list, t);
                        }
                }
 
                public MessageCollection CheckType (TypeDefinition type, Runner 
runner)
                {
-                       // rule apply to non-public types
-                       // note: isAbstract also excludes static types (2.0)
-                       if (type.IsPublic || type.IsAbstract)
+                       // rule apply to internal (non-visible) types
+                       // note: IsAbstract also excludes static types (2.0)
+                       if (type.IsVisible () || type.IsAbstract)
                                return runner.RuleSuccess;
 
                        // some types are created by compilers and should be 
ignored

Modified: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/ChangeLog        
2008-02-03 20:03:41 UTC (rev 94717)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/ChangeLog        
2008-02-03 20:03:50 UTC (rev 94718)
@@ -1,3 +1,10 @@
+2008-02-03  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * AvoidUninstantiatedInternalClassesRule.cs: Reworked to reduce
+       false positives.
+       * DontIgnoreMethodResultRule.cs: Add more exceptions to reduce
+       false positives.
+
 2008-01-30  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * DontIgnoreMethodResultRule.cs: Don't report on compiler/tools 

Modified: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/DontIgnoreMethodResultRule.cs
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/DontIgnoreMethodResultRule.cs
    2008-02-03 20:03:41 UTC (rev 94717)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/DontIgnoreMethodResultRule.cs
    2008-02-03 20:03:50 UTC (rev 94718)
@@ -67,23 +67,26 @@
 
                // some method return stuff that isn't required in most cases
                // the rule ignores them
-               private static bool IsException (MethodReference method)
+               private static bool IsCallException (MethodReference method)
                {
-                       if (method == null)
-                               return true;
-
                        switch (method.DeclaringType.FullName) {
                        case "System.IO.Directory":
-                               // the returned DirectoryInfo is optional 
(since the directory is created)
-                               return (method.Name == "CreateDirectory");
+                       case "System.IO.DirectoryInfo":
+                               // the returned DirectoryInfo returned by 
Create* methods are optional
+                               return (method.ReturnType.ReturnType.FullName 
== "System.IO.DirectoryInfo");
                        case "System.Text.StringBuilder":
-                               // StringBuilder Append* methods return the 
StringBuilder itself so
-                               // the calls can be chained
-                               return method.Name.StartsWith ("Append");
+                               // StringBuilder Append*, Insert ... methods 
return the current 
+                               // StringBuilder so the calls can be chained
+                               return (method.ReturnType.ReturnType.FullName 
== "System.Text.StringBuilder");
                        case "System.Security.PermissionSet":
                                // PermissionSet return the permission 
(modified or unchanged) when
                                // IPermission are added or removed
                                return (method.Name == "AddPermission" || 
method.Name == "RemovePermission");
+                       case "System.Reflection.MethodBase":
+                               return (method.Name == "Invoke");
+                       case "Mono.Security.ASN1":
+                               // return the instance so we can chain 
operations
+                               return (method.Name == "Add");
                        }
 
                        // many types provide a BeginInvoke, which return value 
(deriving from IAsyncResult)
@@ -97,19 +100,33 @@
                        return false;
                }
 
+               private static bool IsNewException (MemberReference method)
+               {
+                       switch (method.ToString ()) {
+                       // supplying a callback is enough to make the Timer 
creation worthwhile
+                       case "System.Void 
System.Threading.Timer::.ctor(System.Threading.TimerCallback,System.Object,System.Int32,System.Int32)":
+                               return true;
+                       default:
+                               return false;
+                       }
+               }
+
                private static Message CheckForViolation (MethodDefinition 
method, Instruction instruction)
                {
                        if ((instruction.OpCode.Code == Code.Newobj || 
instruction.OpCode.Code == Code.Newarr)) {
-                               string s = String.Format ("Unused object of 
type {0} created", (instruction.Operand as MemberReference).ToString ());
-                               Location loc = new Location (method, 
instruction.Offset);
-                               return new Message (s, null, 
MessageType.Warning);
+                               MemberReference member = (instruction.Operand 
as MemberReference);
+                               if ((member != null) && !IsNewException 
(member)) {
+                                       string s = String.Format ("Unused 
object of type {0} created", member.ToString ());
+                                       Location loc = new Location (method, 
instruction.Offset);
+                                       return new Message (s, null, 
MessageType.Warning);
+                               }
                        }
 
                        if (instruction.OpCode.Code == Code.Call || 
instruction.OpCode.Code == Code.Callvirt) {
                                MethodReference callee = instruction.Operand as 
MethodReference;
                                if (callee != null && 
!callee.ReturnType.ReturnType.IsValueType) {
                                        // check for some common exceptions (to 
reduce false positive)
-                                       if (!IsException (callee)) {
+                                       if (!IsCallException (callee)) {
                                                Location loc = new Location 
(method, instruction.Offset);
                                                // most common case is 
something like: s.ToLower ();
                                                MessageType messageType = 
(method.DeclaringType.FullName == "System.String") ? MessageType.Error : 
MessageType.Warning;

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

Reply via email to