Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
2008-12-29 Sebastien Pouliot <sebastien@ximian.com>
Browse files Browse the repository at this point in the history
	* ReviewSelfAssignmentRule.cs: Fix the check when a call chain
	containing an indexer is used.
	[Backport of r122215]


svn path=/branches/mono-2-2/mono-tools/; revision=122223
  • Loading branch information
Sebastien Pouliot committed Dec 30, 2008
1 parent 30be4b0 commit 2e25ef5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
6 changes: 6 additions & 0 deletions gendarme/rules/Gendarme.Rules.Correctness/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2008-12-29 Sebastien Pouliot <sebastien@ximian.com>

* ReviewSelfAssignmentRule.cs: Fix the check when a call chain
containing an indexer is used.
[Backport of r122215]

2008-12-19 Sebastien Pouliot <sebastien@ximian.com>

* CheckParametersNullityInVisibleMethodsRule.cs: Make sure (null ==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,31 @@ public class ReviewSelfAssignmentRule : Rule, IMethodRule {

static bool Compare (Instruction left, Instruction right, MethodDefinition method)
{
// is it the same field ?
FieldDefinition field = right.GetField ();
if (field != left.GetField ())
if (left == null)
return (right == null);
else if (right == null)
return false;

// is it on the same instance ?
Instruction origin_left = left.TraceBack (method);
Instruction origin_right = right.TraceBack (method);

// if this is an array access the it must be the same element
if (origin_left.IsLoadElement ())
origin_left = origin_left.Previous;
if (origin_right.IsLoadElement ())
origin_right = origin_right.Previous;
if (origin_left.IsLoadElement () && origin_right.IsLoadElement ()) {
if (!CompareOperand (origin_left.Previous, origin_right.Previous, method))
return false;
} else {
if (!CompareOperand (origin_left, origin_right, method))
return false;
}

return Compare (origin_left, origin_right, method);
}

object operand_left = origin_left.GetOperand (method);
object operand_right = origin_right.GetOperand (method);
static bool CompareOperand (Instruction left, Instruction right, MethodDefinition method)
{
object operand_left = left.GetOperand (method);
object operand_right = right.GetOperand (method);
if (operand_left == null)
return (operand_right == null);
return operand_left.Equals (operand_right);
Expand All @@ -125,10 +133,11 @@ public RuleResult CheckMethod (MethodDefinition method)
continue;

if (next.OpCode.Code == Code.Stfld) {
if (Compare (next, ins, method)) {
FieldDefinition field = ins.GetField ();
if (field != null) {
msg = String.Format ("Instance field '{0}' of type '{1}'.",
// is it the same field ?
FieldDefinition field = ins.GetField ();
if ((field != null) && (field == next.GetField ())) {
if (Compare (next, ins, method)) {
msg = String.Format ("Instance field '{0}' of type '{1}'.",
field.Name, field.FieldType.FullName);
}
}
Expand Down

0 comments on commit 2e25ef5

Please sign in to comment.