Skip to content

Commit 746c201

Browse files
authored
Merge pull request #86 from Lorilatschki/master
fix: consider types referenced by non compiler generated fields inside ctor base calls
2 parents 1dff2f0 + f37eaf6 commit 746c201

5 files changed

Lines changed: 52 additions & 6 deletions

File tree

src/NetArchTest.Rules/Dependencies/TypeDefinitionCheckingContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ private void CheckMethodBodyInstructions(MethodDefinition methodToCheck)
220220
}
221221
}
222222
break;
223+
case FieldReference fieldReference:
224+
if (!fieldReference.Resolve().CustomAttributes.IsCompilerGenerated())
225+
{
226+
CheckTypeReference(fieldReference.DeclaringType);
227+
}
228+
break;
223229
case MethodReference methodReference:
224230
CheckTypeReference( methodReference.DeclaringType);
225231
break;

src/NetArchTest.Rules/Extensions/TypeDefinitionExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using Mono.Cecil;
77
using System.Runtime.CompilerServices;
8+
using Mono.Collections.Generic;
89

910
/// <summary>
1011
/// Extensions for the <see cref="TypeDefinition"/> class.
@@ -107,7 +108,12 @@ public static bool HasNullableMembers(this TypeDefinition typeDefinition)
107108

108109
public static bool IsCompilerGenerated(this TypeDefinition typeDefinition)
109110
{
110-
return typeDefinition.CustomAttributes.Any(x => x?.AttributeType?.FullName == typeof(CompilerGeneratedAttribute).FullName);
111+
return typeDefinition.CustomAttributes.IsCompilerGenerated();
112+
}
113+
114+
public static bool IsCompilerGenerated(this Collection<CustomAttribute> customAttributes)
115+
{
116+
return customAttributes.Any(x => x?.AttributeType?.FullName == typeof(CompilerGeneratedAttribute).FullName);
111117
}
112118

113119
/// <summary>

src/NetArchTest.Rules/NetArchTest.Rules.xml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/NetArchTest.Rules.UnitTests/DependencySearch/DependencyTypeTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ public void DependencySearch_VariableTupleNested_Found()
396396
public void DependencySearch_VariableTuple_NotFound()
397397
{
398398
Utils.RunDependencyTest(typeof(VariableTuple), typeof(Tuple<int, double>), false, true);
399-
}
399+
}
400+
401+
[Fact(DisplayName = "Finds a dependency StaticType in BaseCtorCall.")]
402+
public void DependencySearch_BaseCtorCall_Found()
403+
{
404+
Utils.RunDependencyTest(typeof(BaseCtorCall), typeof(StaticType), true, true);
405+
}
400406
}
401407
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace NetArchTest.TestStructure.Dependencies.Examples
2+
{
3+
public struct Id
4+
{
5+
}
6+
7+
public static class StaticType
8+
{
9+
public static readonly Id SomeId;
10+
}
11+
12+
public abstract class BaseCtorCallBase
13+
{
14+
#pragma warning disable 219
15+
protected BaseCtorCallBase(params Id[] ids)
16+
{
17+
}
18+
#pragma warning restore 219
19+
}
20+
21+
public class BaseCtorCall : BaseCtorCallBase
22+
{
23+
public BaseCtorCall() :
24+
base(StaticType.SomeId)
25+
{
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)