static string CheckDoubleAssignement (MethodDefinition method, Instruction ins, Instruction next) { // for a static field the pattern is // DUP, STSFLD, STSFLD if (ins.OpCode.Code == Code.Stsfld) { if (next.OpCode.Code != Code.Stsfld) return String.Empty; // check that we're assigning the same field twice FieldDefinition fd1 = ins.GetField (); FieldDefinition fd2 = next.GetField (); if (fd1.MetadataToken.RID != fd2.MetadataToken.RID) return String.Empty; return String.Format ("Static field '{0}'.", fd1.Name); } else if (ins.IsStoreLocal ()) { // for a local variable the pattern is // DUP, STLOC, STLOC VariableDefinition vd2 = next.GetVariable (method); // check that we're assigning the same variable twice if (vd2 != null) { VariableDefinition vd1 = ins.GetVariable (method); if (vd1.Index != vd2.Index) return String.Empty; return String.Format ("Local variable '{0}'.", vd1.Name); } else if (next.OpCode.Code == Code.Stfld) { // instance fields are a bit more complex... return CheckDoubleAssignementOnInstanceFields (method, ins, next); } } return String.Empty; }
void CheckFields (Instruction ins, Instruction next, MethodDefinition method, bool isStatic) { FieldDefinition field = ins.GetField (); if ((field != null) && (field == next.GetField ())) { // instance fields need extra comparison using method if (isStatic || Compare (next, ins, method)) { string msg = String.Format (CultureInfo.InvariantCulture, "{0} field '{1}' of type '{2}'.", isStatic ? "Static" : "Instance", field.Name, field.FieldType.GetFullName ()); Runner.Report (method, ins, Severity.Medium, Confidence.Normal, msg); } } }