Skip to content

Commit 10bacca

Browse files
authored
refactor: simplify usage of "Nullable" (#708)
This pull request refactors how nullable reference type attributes are handled across the codebase. The main focus is on removing the internal polyfill for the `NotNullWhenAttribute` and instead relying on the external `Nullable` NuGet package where needed. This simplifies the code and reduces duplication. Additionally, related method signatures are updated to remove the use of the `[NotNullWhen]` attribute in internal APIs.
1 parent 78babd7 commit 10bacca

12 files changed

Lines changed: 19 additions & 170 deletions

File tree

Source/Directory.Build.props

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@
4747
</PropertyGroup>
4848

4949
<ItemGroup>
50-
<PackageReference Include="Nullable">
51-
<PrivateAssets>all</PrivateAssets>
52-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
53-
</PackageReference>
5450
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="all"/>
5551
</ItemGroup>
5652

Source/Mockolate.Analyzers/MockabilityAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private static bool NeedsRefStructPipeline(ITypeSymbol type)
293293
}
294294

295295
private static bool TryGetRefStructIssue(IMethodSymbol method, string? pipelineUnsupportedReason,
296-
[NotNullWhen(true)] out string? issue)
296+
out string? issue)
297297
{
298298
bool hasRefStructParam = false;
299299
foreach (IParameterSymbol p in method.Parameters)
@@ -334,7 +334,7 @@ private static bool TryGetRefStructIssue(IMethodSymbol method, string? pipelineU
334334
}
335335

336336
private static bool TryGetRefStructIssueForIndexer(IPropertySymbol indexer, string? pipelineUnsupportedReason,
337-
[NotNullWhen(true)] out string? issue)
337+
out string? issue)
338338
{
339339
if (!indexer.Parameters.Any(p => NeedsRefStructPipeline(p.Type)))
340340
{
@@ -422,7 +422,7 @@ private static void AnalyzeImplementing(SyntaxNodeAnalysisContext context,
422422
private static bool IsInMockolateNamespace(ISymbol symbol)
423423
=> symbol.ContainingNamespace is { Name: "Mockolate", ContainingNamespace.IsGlobalNamespace: true, };
424424

425-
private static bool IsMockable(ITypeSymbol typeSymbol, [NotNullWhen(false)] out string? reason)
425+
private static bool IsMockable(ITypeSymbol typeSymbol, out string? reason)
426426
{
427427
if (typeSymbol.TypeKind == TypeKind.Struct)
428428
{

Source/Mockolate.Analyzers/Mockolate.Analyzers.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,4 @@
4141
</Compile>
4242
</ItemGroup>
4343

44-
<ItemGroup>
45-
<PackageReference Remove="Nullable"/>
46-
</ItemGroup>
47-
4844
</Project>

Source/Mockolate.Analyzers/Polyfills/NotNullWhenAttribute.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

Source/Mockolate.SourceGenerators/Entities/Class.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private string GetTypeName(ITypeSymbol type)
232232

233233
if (TryExtractSpecialName(namedType, out string? name))
234234
{
235-
return name;
235+
return name!;
236236
}
237237
}
238238

@@ -265,14 +265,14 @@ string GetPrefix(ITypeSymbol s)
265265

266266
if (TryExtractSpecialName(namedType, out string? name))
267267
{
268-
return name;
268+
return name!;
269269
}
270270
}
271271

272272
return GetPrefix(type) + type.Name;
273273
}
274274

275-
private static bool TryExtractSpecialName(INamedTypeSymbol namedType, [NotNullWhen(true)] out string? specialName)
275+
private static bool TryExtractSpecialName(INamedTypeSymbol namedType, out string? specialName)
276276
{
277277
(specialName, bool hasSpecialType) = namedType.SpecialType switch
278278
{

Source/Mockolate.SourceGenerators/MockGeneratorHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ internal static IEnumerable<MockClass> ExtractMockOrMockFactoryCreateSyntaxOrDef
145145

146146
if (IsMockable(rootType))
147147
{
148-
yield return new MockClass([rootType, ..collectedTypes,], sourceAssembly);
148+
yield return new MockClass([rootType!, ..collectedTypes,], sourceAssembly);
149149
}
150150
}
151151
}
@@ -180,7 +180,7 @@ private static IEnumerable<MockClass> DiscoverMockableTypes(IEnumerable<ITypeSym
180180
}
181181
}
182182

183-
private static bool IsMockable([NotNullWhen(true)] ITypeSymbol? typeSymbol)
183+
private static bool IsMockable(ITypeSymbol? typeSymbol)
184184
=> typeSymbol is
185185
{
186186
DeclaredAccessibility: not Accessibility.Private and not Accessibility.ProtectedAndInternal and not Accessibility.ProtectedAndFriend,

Source/Mockolate.SourceGenerators/Mockolate.SourceGenerators.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,4 @@
2424
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
2525
</ItemGroup>
2626

27-
<ItemGroup>
28-
<PackageReference Remove="Nullable"/>
29-
</ItemGroup>
30-
3127
</Project>

Source/Mockolate.SourceGenerators/Polyfills/NotNullWhenAttribute.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

Source/Mockolate/Mockolate.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>
16+
<PackageReference Include="Nullable">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
1620
</ItemGroup>
1721

1822
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">

Tests/Directory.Build.props

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
<PrivateAssets>all</PrivateAssets>
3434
</PackageReference>
3535
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
36-
<PackageReference Include="Nullable">
37-
<PrivateAssets>all</PrivateAssets>
38-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
39-
</PackageReference>
4036
<PackageReference Include="coverlet.collector">
4137
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4238
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)