Author: spouliot
Date: 2008-02-06 22:05:50 -0500 (Wed, 06 Feb 2008)
New Revision: 95107

Added:
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/AvoidUnsealedUninheritedInternalClassesRule.cs
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/UseIsOperatorRule.cs
Modified:
   trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Gendarme.Rules.Performance.mdp
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Gendarme.Rules.Performance.xml.in
   trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Makefile.am
Log:
2008-02-06  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * AvoidUnsealedUninheritedInternalClassesRule.cs: New. Rule to check
        for non-sealed and non-visible types [Scott Peterson]
        * UseIsOperatorRule.cs: New. Rule to check when using the "is" 
        operator would be better than a cast+null check [Seo Sanghyeon]
        * Gendarme.Rules.Performance.xml.in: Add rule description.
        * Gendarme.Rules.Performance.mdp: Add rule to MonoDevelop project 
        file.
        * Makefile.am: Add rule and tests to the build.



Added: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/AvoidUnsealedUninheritedInternalClassesRule.cs
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/AvoidUnsealedUninheritedInternalClassesRule.cs
   2008-02-07 03:03:20 UTC (rev 95106)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/AvoidUnsealedUninheritedInternalClassesRule.cs
   2008-02-07 03:05:50 UTC (rev 95107)
@@ -0,0 +1,58 @@
+// 
+// Gendarme.Rules.Performance.AvoidUnsealedUninheritedInternalClassesRule
+//
+// Authors:
+//     Scott Peterson <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Scott Peterson
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to 
deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+
+using Mono.Cecil;
+
+using Gendarme.Framework;
+using Gendarme.Framework.Rocks;
+
+namespace Gendarme.Rules.Performance {
+
+       public class AvoidUnsealedUninheritedInternalClassesRule : ITypeRule {
+
+               public MessageCollection CheckType (TypeDefinition type, Runner 
runner)
+               {
+                       if (type.IsAbstract || type.IsSealed || type.IsVisible 
() || type.IsGeneratedCode ())
+                               return runner.RuleSuccess;
+
+                       foreach (TypeDefinition type_definition in 
type.Module.Types) {
+                               // skip ourself
+                               if (type_definition.FullName == type.FullName)
+                                       continue;
+                               if (type_definition.Inherits (type.FullName))
+                                       return runner.RuleSuccess;
+                       }
+
+                       Message msg = new Message (
+                               "Due to performance issues, types which are not 
visible outside of the " +
+                               "assembly and which have no inherited types 
within the assembly should be sealed.",
+                               new Location (type), MessageType.Error);
+                       return new MessageCollection (msg);
+               }
+       }
+}

Modified: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/ChangeLog        
2008-02-07 03:03:20 UTC (rev 95106)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/ChangeLog        
2008-02-07 03:05:50 UTC (rev 95107)
@@ -1,3 +1,14 @@
+2008-02-06  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * AvoidUnsealedUninheritedInternalClassesRule.cs: New. Rule to check
+       for non-sealed and non-visible types [Scott Peterson]
+       * UseIsOperatorRule.cs: New. Rule to check when using the "is" 
+       operator would be better than a cast+null check [Seo Sanghyeon]
+       * Gendarme.Rules.Performance.xml.in: Add rule description.
+       * Gendarme.Rules.Performance.mdp: Add rule to MonoDevelop project 
+       file.
+       * Makefile.am: Add rule and tests to the build.
+
 2008-02-03  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * AvoidUninstantiatedInternalClassesRule.cs: Reworked to reduce

Modified: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Gendarme.Rules.Performance.mdp
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Gendarme.Rules.Performance.mdp
   2008-02-07 03:03:20 UTC (rev 95106)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Gendarme.Rules.Performance.mdp
   2008-02-07 03:05:50 UTC (rev 95107)
@@ -25,6 +25,9 @@
     <File name="AvoidToStringOnStringsRule.cs" subtype="Code" 
buildaction="Compile" />
     <File name="DontIgnoreMethodResultRule.cs" subtype="Code" 
buildaction="Compile" />
     <File name="AvoidUnsealedConcreteAttributesRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="AvoidUnsealedUninheritedInternalClassesRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="AvoidReturningArraysOnPropertiesRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="UseIsOperatorRule.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Assembly" localcopy="True" 
refto="../../../lib/Mono.Cecil.dll" />

Modified: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Gendarme.Rules.Performance.xml.in
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Gendarme.Rules.Performance.xml.in
        2008-02-07 03:03:20 UTC (rev 95106)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Gendarme.Rules.Performance.xml.in
        2008-02-07 03:05:50 UTC (rev 95107)
@@ -23,6 +23,12 @@
                <problem>The internal type '{0}' is not instantiated by code 
within the assembly.</problem>
                <solution>Remove the type or add the code that uses it.  If the 
type contains only static methods then either add the static modifier to the 
type or add the private construtor to the type to prevent the compiler from 
emitting a default public instance constructor.</solution>
        </rule>
+       <rule   Name="AvoidUnsealedUninheritedInternalClassesRule" 
+               
Type="Gendarme.Rules.Performance.AvoidUnsealedUninheritedInternalClassesRule, 
Gendarme.Rules.Performance, [EMAIL PROTECTED]@, Culture=neutral, 
PublicKeyToken=null"
+               
Uri="http://www.mono-project.com/Gendarme.Rules.Performance#AvoidUnsealedUninheritedInternalClassesRule";
 >
+               <problem>The type '{0}' should be sealed since it is not 
inherited from nor is it visibile outside the assembly.</problem>
+               <solution>You should seal this type, unless you plan to inherit 
from this type in the near-future.</solution>
+       </rule>
        <rule   Name="AvoidUnsealedConcreteAttributesRule" 
                
Type="Gendarme.Rules.Performance.AvoidUnsealedConcreteAttributesRule, 
Gendarme.Rules.Performance, [EMAIL PROTECTED]@, Culture=neutral, 
PublicKeyToken=null"
                
Uri="http://www.mono-project.com/Gendarme.Rules.Performance#AvoidUnsealedConcreteAttributesRule";
 >
@@ -53,6 +59,12 @@
                <problem>The type '{0}' has a destructor and implements 
IDisposable. However it doesn't call System.GC.SuppressFinalize inside it's 
Dispose method.</problem>
                <solution>Add a call to GC.SuppressFinalize inside your Dispose 
method.</solution>
        </rule>
+       <rule   Name="UseIsOperatorRule" 
+               Type="Gendarme.Rules.Performance.UseIsOperatorRule, 
Gendarme.Rules.Performance, [EMAIL PROTECTED]@, Culture=neutral, 
PublicKeyToken=null"
+               
Uri="http://www.mono-project.com/Gendarme.Rules.Performance#UseStringEmptyRule"; 
>
+               <problem>The method '{0}' should use the "is" operator and 
avoid the cast and compare to null.</problem>
+               <solution>Replace the cast and compare to null with the simpler 
"is" operator.</solution>
+       </rule>
        <rule   Name="UseStringEmptyRule" 
                Type="Gendarme.Rules.Performance.UseStringEmptyRule, 
Gendarme.Rules.Performance, [EMAIL PROTECTED]@, Culture=neutral, 
PublicKeyToken=null"
                
Uri="http://www.mono-project.com/Gendarme.Rules.Performance#UseStringEmptyRule"; 
>

Modified: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Makefile.am
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Makefile.am      
2008-02-07 03:03:20 UTC (rev 95106)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Makefile.am      
2008-02-07 03:05:50 UTC (rev 95107)
@@ -14,7 +14,7 @@
        UseStringEmptyRule.cs AvoidUnusedParametersRule.cs 
AvoidUninstantiatedInternalClassesRule.cs \
        AvoidUncalledPrivateCodeRule.cs AvoidToStringOnStringsRule.cs 
DontIgnoreMethodResultRule.cs \
        UsingStringLengthInsteadOfCheckingEmptyStringRule.cs 
AvoidReturningArraysOnPropertiesRule.cs \
-       AvoidUnsealedConcreteAttributesRule.cs
+       AvoidUnsealedConcreteAttributesRule.cs UseIsOperatorRule.cs 
AvoidUnsealedUninheritedInternalClassesRule.cs
 
 performance_rules_build_sources = $(addprefix $(srcdir)/, 
$(performance_rules_sources))
 performance_rules_build_sources += $(performance_rules_generated_sources)
@@ -26,7 +26,8 @@
 performance_test_sources = EmptyDestructorTest.cs 
IDisposableWithDestructorWithoutSuppressFinalizeTest.cs \
        UseStringEmptyTest.cs AvoidUnusedParametersTest.cs 
UsingStringLengthInsteadOfCheckingEmptyStringTest.cs \
        AvoidUncalledPrivateCodeTest.cs AvoidToStringOnStringsTest.cs 
AvoidUninstantiatedInternalClassesTest.cs \
-       DontIgnoreMethodResultTest.cs AvoidReturningArraysOnPropertiesTest.cs 
AvoidUnsealedConcreteAttributesTest.cs
+       DontIgnoreMethodResultTest.cs AvoidReturningArraysOnPropertiesTest.cs 
AvoidUnsealedConcreteAttributesTest.cs \
+       UseIsOperatorTest.cs AvoidUnsealedUninheritedInternalClassesTest.cs
 
 performance_test_build_sources = $(addprefix $(srcdir)/Test/, 
$(performance_test_sources))
 

Added: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/UseIsOperatorRule.cs
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/UseIsOperatorRule.cs 
    2008-02-07 03:03:20 UTC (rev 95106)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/UseIsOperatorRule.cs 
    2008-02-07 03:05:50 UTC (rev 95107)
@@ -0,0 +1,68 @@
+//
+// Gendarme.Rules.Performance.UseIsOperatorRule class
+//
+// Authors:
+//     Seo Sanghyeon  <[EMAIL PROTECTED]>
+//
+// Copyright (c) 2008 Seo Sanghyeon
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Performance {
+
+       public class UseIsOperatorRule : IMethodRule {
+
+               private const string Text = "Using the 'is' operator would 
produce better (less) IL and would be easier to understand.";
+
+               public MessageCollection CheckMethod (MethodDefinition method, 
Runner runner)
+               {
+                       if (!method.HasBody)
+                               return runner.RuleSuccess;
+
+                       MessageCollection messages = null;
+                       InstructionCollection instructions = 
method.Body.Instructions;
+                       int n = instructions.Count - 2;
+                       for (int i = 0; i < n; i++) {
+                               Code code0 = instructions [i].OpCode.Code;
+                               if (code0 != Code.Isinst)
+                                       continue;
+                               Code code1 = instructions [i + 1].OpCode.Code;
+                               if (code1 != Code.Ldnull)
+                                       continue;
+                               Code code2 = instructions [i + 2].OpCode.Code;
+                               if (code2 != Code.Ceq)
+                                       continue;
+
+                               if (messages == null)
+                                       messages = new MessageCollection ();
+                               Location loc = new Location (method, i);
+                               Message msg = new Message (Text, loc, 
MessageType.Warning);
+                               messages.Add (msg);
+                       }
+
+                       return messages;
+               }
+       }
+}

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

Reply via email to