-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing some nullable ref type support on public APIs (#6757)
* Started marking a few types as non-nullable * Replace guard with more modern one * A few more nullables * More nullables * Adjust guard method * Approval test to show public types without nullable annotations --------- Co-authored-by: David Boike <david.boike@gmail.com>
- Loading branch information
1 parent
b8212c1
commit 11c8799
Showing
195 changed files
with
1,130 additions
and
695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#if NET6_0_OR_GREATER | ||
namespace NServiceBus.Core.Tests.API | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using NServiceBus.Core.Tests.API.Infra; | ||
using NUnit.Framework; | ||
using Particular.Approvals; | ||
|
||
|
||
|
||
[TestFixture] | ||
public class NullableAnnotations | ||
{ | ||
// Only available with net6.0+, implementation for net472 not worth it | ||
NullabilityInfoContext nullContext = new NullabilityInfoContext(); | ||
|
||
[Test] | ||
public void ApproveNullableTypes() | ||
{ | ||
var b = new StringBuilder() | ||
.AppendLine("The following types do not have annotations for nullable reference types.") | ||
.AppendLine("Changes that make this list longer should not be approved.") | ||
.AppendLine("-----"); | ||
|
||
var context = new NullabilityInfoContext(); | ||
|
||
foreach (var type in NServiceBusAssembly.Types.Where(t => t.IsPublic).OrderBy(t => t.FullName)) | ||
{ | ||
if (HasNonAnnotatedMember(type)) | ||
{ | ||
b.AppendLine(type.FullName); | ||
} | ||
} | ||
|
||
Console.WriteLine(b.ToString()); | ||
Approver.Verify(b.ToString()); | ||
|
||
} | ||
|
||
bool HasNonAnnotatedMember(Type type) | ||
{ | ||
var allInfo = AllMemberNullabilityInfoFor(type); | ||
var noNullInfoFor = allInfo.Where(info => info.WriteState is NullabilityState.Unknown || info.ReadState == NullabilityState.Unknown); | ||
return noNullInfoFor.Any(); | ||
} | ||
|
||
IEnumerable<NullabilityInfo> AllMemberNullabilityInfoFor(Type type) | ||
{ | ||
foreach (var member in type.GetMembers()) | ||
{ | ||
if (member is PropertyInfo prop) | ||
{ | ||
yield return nullContext.Create(prop); | ||
} | ||
else if (member is FieldInfo field) | ||
{ | ||
yield return nullContext.Create(field); | ||
} | ||
else if (member is EventInfo evt) | ||
{ | ||
yield return nullContext.Create(evt); | ||
} | ||
else if (member is MethodBase method) | ||
{ | ||
var parameters = method.GetParameters(); | ||
foreach (var parameter in parameters) | ||
{ | ||
yield return nullContext.Create(parameter); | ||
} | ||
} | ||
else if (member.MemberType == MemberTypes.NestedType) | ||
{ | ||
continue; | ||
} | ||
else | ||
{ | ||
throw new Exception($"Unhandled MemberType: {member.MemberType}"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.