Commit
…e code.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,25 +42,34 @@ namespace Gendarme.Rules.Performance { | |
/// <example> | ||
/// Bad example: | ||
/// <code> | ||
/// public override string ToString() | ||
/// public override string ToString () | ||
/// { | ||
/// return base.ToString(); | ||
/// return base.ToString (); | ||
/// } | ||
/// </code> | ||
/// </example> | ||
/// <example> | ||
/// Good example: | ||
/// Good example (1): | ||
/// <code> | ||
/// [FileIOPermission (SecurityAction.Demand, @"c:\dir\file")] | ||
/// public override string ToString() | ||
/// public override string ToString () | ||
/// { | ||
/// return base.ToString(); | ||
/// return base.ToString (); | ||
/// } | ||
/// </code> | ||
/// </example> | ||
/// <example> | ||
/// Good example (2): | ||
/// <code> | ||
/// /*public override string ToString () | ||
/// { | ||
/// return base.ToString (); | ||
/// }*/ | ||
/// </code> | ||
/// </example> | ||
|
||
[Problem ("This override of a base class method is unnecessary.")] | ||
[Solution ("Remove the override method.")] | ||
[Solution ("Remove the override method or extend the functionality of the method.")] | ||
public class AvoidUnnecessaryOverridesRule : Rule, ITypeRule { | ||
|
||
public RuleResult CheckType(TypeDefinition type) | ||
|
@@ -88,9 +97,8 @@ public RuleResult CheckType(TypeDefinition type) | |
while (i < instrs.Count && (instrs[i].Is (Code.Nop) || instrs[i].IsLoadArgument ())) | ||
i++; | ||
|
||
// If the next instruction is not a call we are good. (Relatively certain | ||
// calls to the base class cannot be virtual.) | ||
if (!instrs[i].Is (Code.Call)) | ||
// If the next instruction is not a call we are good. | ||
if (!instrs[i].Is (Code.Call) && !instrs[i].Is(Code.Call)) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
continue; | ||
// Check to make sure the call is to the base class, and the same method name... | ||
MethodReference mr = instrs[i].Operand as MethodReference; | ||
|
@@ -102,6 +110,26 @@ public RuleResult CheckType(TypeDefinition type) | |
continue; | ||
if (mr.Name != method.Name) | ||
continue; | ||
// Account for calling overrides. | ||
if (mr.HasParameters != method.HasParameters) | ||
continue; | ||
// Check the parameter count and type. | ||
if (mr.Parameters.Count != method.Parameters.Count) | ||
This comment has been minimized.
Sorry, something went wrong.
spouliot
|
||
continue; | ||
// Shallow check for equality. | ||
bool sameParam = true; | ||
for (int o = 0; o < mr.Parameters.Count; o++) { | ||
var p1 = mr.Parameters[o]; | ||
var p2 = method.Parameters[o]; | ||
sameParam = true; | ||
sameParam &= p1.HasDefault == p2.HasDefault; | ||
sameParam &= p1.HasFieldMarshal == p2.HasFieldMarshal; | ||
sameParam &= p1.IsOptional == p2.IsOptional; | ||
sameParam &= p1.IsOut == p2.IsOut; | ||
sameParam &= p1.ParameterType == p2.ParameterType; | ||
if (!sameParam) break; | ||
This comment has been minimized.
Sorry, something went wrong.
spouliot
|
||
} | ||
if (!sameParam) continue; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
i++; | ||
// If the return type is void, all we should have is nop and return. | ||
|
2 comments
on commit e6105e8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a bug (in either Firefox and github) does not let me scroll the source past line 135. I'll try to refresh the page and continue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, the patch goes no further :-) but the horizontal scrollbar was hiding this.
I bet you meant Callvirt in the second case ;-)
missing space before '(' too