diff options
author | Sebastien Pouliot <sebastien@ximian.com> | 2008-02-08 06:44:51 +0300 |
---|---|---|
committer | Sebastien Pouliot <sebastien@ximian.com> | 2008-02-08 06:44:51 +0300 |
commit | a51559e43e9352071e07e5e653cdffd9b6027c3e (patch) | |
tree | 190d085e581b1470bc1aceeed4f9945c77428714 /gendarme/rules | |
parent | 0747797b672d2c1cc615e26a0e1a314e7b8cd285 (diff) |
2008-02-07 Sebastien Pouliot <sebastien@ximian.com>
* EnumsShouldUseInt32Rule.cs: New. Rule to warn about enumations that
are not based on Int32.
* Gendarme.Rules.Design.mdp: Update project file.
* Gendarme.Rules.Design.xml.in: Add new rule description.
* Makefile.am: Add new rule/test to the build.
svn path=/trunk/mono-tools/; revision=95220
Diffstat (limited to 'gendarme/rules')
5 files changed, 107 insertions, 3 deletions
diff --git a/gendarme/rules/Gendarme.Rules.Design/ChangeLog b/gendarme/rules/Gendarme.Rules.Design/ChangeLog index 39a53a38..cd62ca49 100644 --- a/gendarme/rules/Gendarme.Rules.Design/ChangeLog +++ b/gendarme/rules/Gendarme.Rules.Design/ChangeLog @@ -1,3 +1,11 @@ +2008-02-07 Sebastien Pouliot <sebastien@ximian.com> + + * EnumsShouldUseInt32Rule.cs: New. Rule to warn about enumations that + are not based on Int32. + * Gendarme.Rules.Design.mdp: Update project file. + * Gendarme.Rules.Design.xml.in: Add new rule description. + * Makefile.am: Add new rule/test to the build. + 2008-02-07 Nestor Salceda <nestor.salceda@gmail.com> * DisposableTypesShouldHaveFinalizerRule.cs, diff --git a/gendarme/rules/Gendarme.Rules.Design/EnumsShouldUseInt32Rule.cs b/gendarme/rules/Gendarme.Rules.Design/EnumsShouldUseInt32Rule.cs new file mode 100644 index 00000000..f5a2f5c0 --- /dev/null +++ b/gendarme/rules/Gendarme.Rules.Design/EnumsShouldUseInt32Rule.cs @@ -0,0 +1,83 @@ +// +// Gendarme.Rules.Design.EnumsShouldUseInt32Rule +// +// Authors: +// Sebastien Pouliot <sebastien@ximian.com> +// +// Copyright (C) 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 +// 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.Design { + + public class EnumsShouldUseInt32Rule : ITypeRule { + + public MessageCollection CheckType (TypeDefinition type, Runner runner) + { + // rule apply only on enums + if (!type.IsEnum) + return runner.RuleSuccess; + + // rule applies! + + string value_type = null; + + foreach (FieldDefinition field in type.Fields) { + // we looking for the special value__ + if (field.IsStatic) + continue; + + value_type = field.FieldType.FullName; + break; + } + + MessageType criticality = MessageType.Error; + switch (value_type) { + case "System.Int32": + return runner.RuleSuccess; + // some are bad choice (when possible) but usable by all CLS compliant languages + case "System.Byte": + case "System.Int16": + case "System.Int64": + criticality = MessageType.Warning; + break; + // while others are not usable in non-CLS compliant languages + case "System.SByte": + case "System.UInt16": + case "System.UInt32": + case "System.UInt64": + criticality = MessageType.Error; + break; + default: + throw new NotSupportedException (value_type + " unexpected as a Enum value type"); + } + + string text = String.Format ("Enums should use System.Int32 instead of '{0}'.", value_type); + Message msg = new Message (text, new Location (type), criticality); + return new MessageCollection (msg); + } + } +} diff --git a/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.mdp b/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.mdp index 383883de..5954c80b 100644 --- a/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.mdp +++ b/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.mdp @@ -29,12 +29,18 @@ <File name="ConsiderConvertingMethodToPropertyRule.cs" subtype="Code" buildaction="Compile" /> <File name="MissingAttributeUsageOnCustomAttributeRule.cs" subtype="Code" buildaction="Compile" /> <File name="DisposableFieldsShouldBeDisposedRule.cs" subtype="Code" buildaction="Compile" /> - <File name="DisposableHelper.cs" subtype="Code" buildaction="Compile" /> <File name="DisposableTypesShouldHaveFinalizerRule.cs" subtype="Code" buildaction="Compile" /> <File name="TypesWithDisposableFieldsShouldBeDisposableRule.cs" subtype="Code" buildaction="Compile" /> <File name="TypesWithNativeFieldsShouldBeDisposableRule.cs" subtype="Code" buildaction="Compile" /> <File name="FinalizersShouldBeProtectedRule.cs" subtype="Code" buildaction="Compile" /> <File name="FinalizersShouldCallBaseClassFinalizerRule.cs" subtype="Code" buildaction="Compile" /> + <File name="AttributeArgumentsShouldHaveAccessorsRule.cs" subtype="Code" buildaction="Compile" /> + <File name="EnsureSymmetryForOverloadedOperatorsRule.cs" subtype="Code" buildaction="Compile" /> + <File name="EnumsShouldUseInt32Rule.cs" subtype="Code" buildaction="Compile" /> + <File name="ImplementEqualsAndGetHashCodeInPairRule.cs" subtype="Code" buildaction="Compile" /> + <File name="OperatorEqualsShouldBeOverloadedRule.cs" subtype="Code" buildaction="Compile" /> + <File name="OverrideEqualsMethodRule.cs" subtype="Code" buildaction="Compile" /> + <File name="ProvideAlternativeNamesForOperatorOverloadsRule.cs" subtype="Code" buildaction="Compile" /> </Contents> <References> <ProjectReference type="Project" localcopy="True" refto="framework" /> diff --git a/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.xml.in b/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.xml.in index 271d2647..c8c1c82d 100644 --- a/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.xml.in +++ b/gendarme/rules/Gendarme.Rules.Design/Gendarme.Rules.Design.xml.in @@ -65,6 +65,12 @@ <problem>The enum '{0}' should define a member with a value of 0.</problem> <solution>Add a new member in the enum with a value of 0.</solution> </rule> + <rule Name="EnumsShouldUseInt32Rule" + Type="Gendarme.Rules.Design.EnumsShouldUseInt32Rule, Gendarme.Rules.Design, Version=@GENDARME_VERSION@, Culture=neutral, PublicKeyToken=null" + Uri="http://www.mono-project.com/Gendarme.Rules.Design#EnumsShouldUseInt32Rule" > + <problem>Unless it is required for interoperability the enum '{0}' should use Int32 as it's underling storage type.</problem> + <solution>Remove the extra type from the enumeration declaration (Int32 will be used as default).</solution> + </rule> <rule Name="FinalizersShouldBeProtectedRule" Type="Gendarme.Rules.Design.FinalizersShouldBeProtectedRule, Gendarme.Rules.Design, Version=@GENDARME_VERSION@, Culture=neutral, PublicKeyToken=null" Uri="http://www.mono-project.com/Gendarme.Rules.Design#FinalizersShouldBeProtectedRule" > diff --git a/gendarme/rules/Gendarme.Rules.Design/Makefile.am b/gendarme/rules/Gendarme.Rules.Design/Makefile.am index 8cf32dbb..dbc1211e 100644 --- a/gendarme/rules/Gendarme.Rules.Design/Makefile.am +++ b/gendarme/rules/Gendarme.Rules.Design/Makefile.am @@ -19,7 +19,8 @@ design_rules_sources = DontDeclareProtectedFieldsInSealedClassRule.cs UsingClone TypesWithDisposableFieldsShouldBeDisposableRule.cs DisposableTypesShouldHaveFinalizerRule.cs \ DisposableFieldsShouldBeDisposedRule.cs FinalizersShouldBeProtectedRule.cs FinalizersShouldCallBaseClassFinalizerRule.cs \ EnsureSymmetryForOverloadedOperatorsRule.cs OperatorEqualsShouldBeOverloadedRule.cs \ - OverrideEqualsMethodRule.cs ProvideAlternativeNamesForOperatorOverloadsRule.cs ImplementEqualsAndGetHashCodeInPairRule.cs + OverrideEqualsMethodRule.cs ProvideAlternativeNamesForOperatorOverloadsRule.cs ImplementEqualsAndGetHashCodeInPairRule.cs \ + EnumsShouldUseInt32Rule.cs design_rules_build_sources = $(addprefix $(srcdir)/, $(design_rules_sources)) design_rules_build_sources += $(design_rules_generated_sources) @@ -37,7 +38,7 @@ design_test_sources = DontDeclareProtectedFieldsInSealedClassTest.cs UsingCloneW TypesWithDisposableFieldsShouldBeDisposableTest.cs DisposableTypesShouldHaveFinalizerTest.cs \ DisposableFieldsShouldBeDisposedTest.cs FinalizersShouldBeProtectedTest.cs FinalizersShouldCallBaseClassFinalizerTest.cs \ EnsureSymmetryForOverloadedOperatorsTest.cs OperatorEqualsShouldBeOverloadedTest.cs OverrideEqualsMethodTest.cs \ - ProvideAlternativeNamesForOperatorOverloadsTest.cs ImplementEqualsAndGetHashCodeInPairTest.cs + ProvideAlternativeNamesForOperatorOverloadsTest.cs ImplementEqualsAndGetHashCodeInPairTest.cs EnumsShouldUseInt32Test.cs design_test_build_sources = $(addprefix $(srcdir)/Test/, $(design_test_sources)) |