diff options
author | Stepan Sindelar <me@stevesindelar.cz> | 2012-04-29 20:26:08 +0400 |
---|---|---|
committer | Stepan Sindelar <me@stevesindelar.cz> | 2012-04-29 20:26:08 +0400 |
commit | 43d871f1f2fd485bdcf9dd30dea94501e3b9f942 (patch) | |
tree | 52b3feaeb8ac1af89992a8783838d5b359ab7389 /gendarme | |
parent | 3484c51a89ee8c9b82643c19e6076607ac1a7bd5 (diff) |
Some further performance improvements.
Diffstat (limited to 'gendarme')
-rw-r--r-- | gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedPrivateFieldsRule.cs | 31 | ||||
-rw-r--r-- | gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedPrivateFieldsTest.cs | 21 |
2 files changed, 39 insertions, 13 deletions
diff --git a/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedPrivateFieldsRule.cs b/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedPrivateFieldsRule.cs index 1508d336..c7d74a5c 100644 --- a/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedPrivateFieldsRule.cs +++ b/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedPrivateFieldsRule.cs @@ -105,19 +105,21 @@ namespace Gendarme.Rules.Performance { // copy all fields into an hashset fields.Clear (); foreach (FieldDefinition field in type.Fields) { - if (!field.IsPrivate || field.IsLiteral) + if (!field.IsPrivate || field.IsLiteral || field.IsGeneratedCode ()) continue; fields.Add (field); } // scan all methods, including constructors, to find if the field is used - CheckFieldsUsageInType (type); + if (fields.Count > 0) { + CheckFieldsUsageInType (type); + } // scan nested types becuase they also have access to private fields of their parent - if (type.HasNestedTypes) { + if (type.HasNestedTypes && fields.Count > 0) { foreach (TypeDefinition nested in type.NestedTypes) { - CheckFieldsUsageInType(nested); + CheckFieldsUsageInType (nested); } } @@ -134,18 +136,21 @@ namespace Gendarme.Rules.Performance { if (!method.HasBody) continue; - // don't check the method if it does not access any field - if (!OpCodeEngine.GetBitmask (method).Intersect (LoadStoreFields)) - continue; + // don't check the method if it does not access any field + if (!OpCodeEngine.GetBitmask (method).Intersect (LoadStoreFields)) + continue; - foreach (Instruction ins in method.Body.Instructions) { - FieldDefinition fd = ins.GetField (); - if (fd == null) - continue; + foreach (Instruction ins in method.Body.Instructions) { + FieldDefinition fd = ins.GetField (); + if (fd == null) + continue; - fields.Remove (fd); - } + fields.Remove (fd); } + + if (fields.Count == 0) + break; + } } #if false diff --git a/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedPrivateFieldsTest.cs b/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedPrivateFieldsTest.cs index c8c6bd01..dbb9f153 100644 --- a/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedPrivateFieldsTest.cs +++ b/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedPrivateFieldsTest.cs @@ -182,5 +182,26 @@ namespace Test.Rules.Performance { { AssertRuleSuccess<FieldsUsedInNested> (); } + + class CompilerGenerated { + public string Name { get; set; } + } + + [Test] + public void ClassWithCompilerGeneratedFields () + { + AssertRuleSuccess<CompilerGenerated> (); + } + + class CompilerGeneratedAndUnused { + private int number; + public string Name { get; set; } + } + + [Test] + public void ClassWithCompilerGeneratedFieldsAndUnusedPrivate () + { + AssertRuleFailure<CompilerGeneratedAndUnused> (1); + } } } |