Permalink
Browse files

Merge pull request #25 from usarskyy/master

Fixes for some of Gendarme rules
  • Loading branch information...
2 parents 5a87f37 + 62692c1 commit c1169a91576244b03d90d488a8e3aefdf2f6831f @migueldeicaza migueldeicaza committed May 24, 2012
@@ -151,7 +151,11 @@ public RuleResult CheckMethod (MethodDefinition method)
continue;
// ... to System.Console ...
- MethodReference mr = (ins.Operand as MethodReference);
+ MethodReference mr = ins.Operand as MethodReference;
+
+ if (mr == null)
+ continue;
+
if (!mr.DeclaringType.IsNamed ("System", "Console"))
continue;
@@ -118,6 +118,7 @@ public class PreferTryParseRule : Rule, IMethodRule {
static bool HasTryParseMethod (TypeDefinition type)
{
bool present = false;
+
if (!has_try_parse.TryGetValue (type, out present)) {
foreach (MethodReference method in type.Methods) {
if (MethodSignatures.TryParse.Matches (method)) {
@@ -162,7 +163,8 @@ public RuleResult CheckMethod (MethodDefinition method)
if (!MethodSignatures.Parse.Matches (mr))
continue;
- if (!HasTryParseMethod (mr.DeclaringType.Resolve ()))
+ TypeDefinition declaringType = mr.DeclaringType.Resolve();
+ if (declaringType != null && !HasTryParseMethod(declaringType))
continue;
// if inside a try (catch/finally) block then...
@@ -111,10 +111,11 @@ private static int FindTokenStart (string memberName, string token, int start)
private static bool IsUri (string memberName)
{
int index = 0;
- while ((index = FindTokenStart (memberName, "ur", index)) != -1) {
- if (memberName.Length < index + 2)
+ while ((index = FindTokenStart(memberName, "ur", index)) != -1)
+ {
+ if (memberName.Length <= index + 2)
break;
- if (url_enders.Contains (Char.ToLower (memberName [index + 2], CultureInfo.InvariantCulture)))
+ if (url_enders.Contains(Char.ToLower(memberName[index + 2], CultureInfo.InvariantCulture)))
return true;
index += 2;
}
@@ -52,6 +52,11 @@ public string Urn
get;
set;
}
+ public string CUR
+ {
+ get;
+ set;
+ }
public Uri GetUri ()
{
@@ -117,6 +122,7 @@ public void Good ()
{
AssertRuleSuccess (SimpleMethods.EmptyMethod);
AssertRuleSuccess<GoodUris> ("GetUri");
+ AssertRuleSuccess<GoodUris>("get_CUR");
AssertRuleSuccess<GoodUris> ("GetNewLink");
AssertRuleSuccess<GoodUris> ("OverloadedMethod", new Type [] { typeof (string) });
}
@@ -135,12 +135,17 @@ private bool CheckPInvoke (Instruction startInstruction)
//check if a method is called
if (ins.OpCode.FlowControl == FlowControl.Call) {
+ MethodReference mRef = ins.Operand as MethodReference;
- MethodDefinition mDef = (ins.Operand as MethodReference).Resolve ();
+ if (mRef == null) {
+ continue;
+ }
+
+ MethodDefinition mDef = mRef.Resolve();
if (mDef != null && mDef.IsPInvokeImpl) { //check if another pinvoke method is called, this counts as "GetLastError not called"
break;
}
-
+
string s = (mDef == null) ? String.Empty : mDef.DeclaringType.GetFullName ();
switch (s) {
case "System.Runtime.InteropServices.Marshal":
@@ -85,11 +85,13 @@ public RuleResult CheckType (TypeDefinition type)
return RuleResult.DoesNotApply;
int depth = 0;
- while (type.BaseType != null) {
- type = type.BaseType.Resolve ();
- if (type == null)
+ TypeDefinition temp = type;
+ while (temp.BaseType != null)
+ {
+ temp = temp.BaseType.Resolve();
+ if (temp == null)
break;
- if (countExternalDepth || Runner.Assemblies.Contains (type.Module.Assembly))
+ if (countExternalDepth || Runner.Assemblies.Contains(temp.Module.Assembly))
depth++;
}
@@ -96,7 +96,10 @@ public class AvoidDeepNamespaceHierarchyRule : Rule, IAssemblyRule {
[DefaultValue (DefaultMaxDepth)]
[Description ("The depth at which namespaces may be nested without triggering a defect.")]
public int MaxDepth {
- get { return max_depth; }
+ get
+ {
+ return max_depth;
+ }
set {
if (value < 1)
throw new ArgumentOutOfRangeException ("MaxDepth", "Minimum: 1");
@@ -149,5 +152,14 @@ public RuleResult CheckAssembly (AssemblyDefinition assembly)
}
return Runner.CurrentRuleResult;
}
+
+ public override string Solution
+ {
+ get
+ {
+ return string.Format("Try to keep the depth below {0}, with an additional one for specialization (e.g. Design, Interop, Permissions).",
+ MaxDepth);
+ }
+ }
}
}

0 comments on commit c1169a9

Please sign in to comment.