Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #135 from Barsonax/feature/nullablereferencecleanup
Browse files Browse the repository at this point in the history
Feature/nullablereferencecleanup
  • Loading branch information
Barsonax committed Mar 13, 2020
2 parents e6df2a2 + 8d31a80 commit 44dfa61
Show file tree
Hide file tree
Showing 39 changed files with 276 additions and 71 deletions.
7 changes: 7 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>

<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Nullable.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;

using System.Diagnostics.CodeAnalysis;
using Duality.Resources;
using Singularity.Duality.Resources;

Expand Down Expand Up @@ -30,7 +30,7 @@ internal GameScope(ILogger logger, ISceneScopeFactory sceneScopeFactory, ISceneE
}
else if (TryCreateModule(moduleRef, out IModule? module))
{
modules.Add(module!);
modules.Add(module);
}
}
}
Expand All @@ -48,14 +48,14 @@ internal GameScope(ILogger logger, ISceneScopeFactory sceneScopeFactory, ISceneE
}
}

private bool TryCreateModule(ModuleRef moduleRef, out IModule? module)
private bool TryCreateModule(ModuleRef moduleRef, [NotNullWhen(true)] out IModule? module)
{
Type type = moduleRef.Type;
if (type == null)
{
_logger.WriteWarning($"{nameof(Singularity)}: Could not resolve the type {moduleRef}");
}
else
else
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<AssemblyName>Singularity.Duality.core</AssemblyName>
<PackageTags>Duality Plugin ioc di inversion control dependency injection container</PackageTags>
<Description>Plugin that adds a ioc container to duality</Description>
Expand Down
149 changes: 149 additions & 0 deletions src/Nullable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#pragma warning disable MA0048 // File name must match type name
#define INTERNAL_NULLABLE_ATTRIBUTES
#if NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NET45 || NET451 || NET452 || NET6 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48

// https://github.com/dotnet/corefx/blob/48363ac826ccf66fbe31a5dcb1dc2aab9a7dd768/src/Common/src/CoreLib/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Diagnostics.CodeAnalysis
{
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class AllowNullAttribute : Attribute
{ }

/// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class DisallowNullAttribute : Attribute
{ }

/// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class MaybeNullAttribute : Attribute
{ }

/// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class NotNullAttribute : Attribute
{ }

/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class MaybeNullWhenAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified return value condition.</summary>
/// <param name="returnValue">
/// The return value condition. If the method returns this value, the associated parameter may be null.
/// </param>
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;

/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }
}

/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class NotNullWhenAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified return value condition.</summary>
/// <param name="returnValue">
/// The return value condition. If the method returns this value, the associated parameter will not be null.
/// </param>
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;

/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }
}

/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class NotNullIfNotNullAttribute : Attribute
{
/// <summary>Initializes the attribute with the associated parameter name.</summary>
/// <param name="parameterName">
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
/// </param>
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;

/// <summary>Gets the associated parameter name.</summary>
public string ParameterName { get; }
}

/// <summary>Applied to a method that will never return under any circumstance.</summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class DoesNotReturnAttribute : Attribute
{ }

/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
[ExcludeFromCodeCoverage]
#if INTERNAL_NULLABLE_ATTRIBUTES
internal
#else
public
#endif
sealed class DoesNotReturnIfAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified parameter value.</summary>
/// <param name="parameterValue">
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
/// the associated parameter matches this value.
/// </param>
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;

/// <summary>Gets the condition parameter value.</summary>
public bool ParameterValue { get; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Description>The AspNetCore hosting integration library for the singularity ioc container</Description>
<PackageTags>ioc di inversion control dependency injection container microsoftdi microsoftdependencyinjection hosting</PackageTags>
<RootNamespace>Singularity</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Description>The AspNetCore MVC integration library for the singularity ioc container</Description>
<PackageTags>ioc di inversion control dependency injection container microsoftdi microsoftdependencyinjection mvc aspnetcore</PackageTags>
<RootNamespace>Singularity</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion src/Singularity.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Singularity.Benchmark
{
class Program
class Program
{
static void Main()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Description>The microsoft dependency injection integration library for the singularity ioc container</Description>
<PackageTags>ioc di inversion control dependency injection container microsoftdi microsoftdependencyinjection</PackageTags>
<RootNamespace>Singularity</RootNamespace>
Expand Down
4 changes: 2 additions & 2 deletions src/Singularity/Collections/ActionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public void Invoke()
SinglyLinkedListNode<T>? root = _root;
while (root != null)
{
Action(root.Value!);
root = root.Next!;
Action(root.Value);
root = root.Next;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Singularity/Collections/ImmutableHashTable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Singularity.Collections
Expand All @@ -17,6 +18,7 @@ internal sealed class ImmutableHashTable<TKey, TValue> : IEnumerable<TValue>
return new ImmutableHashTable<TKey, TValue>(this, new HashedKeyValue<TKey, TValue>(key, value));
}

[return: MaybeNull]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal TValue GetOrDefault(TKey key)
{
Expand All @@ -31,7 +33,7 @@ internal TValue GetOrDefault(TKey key)
current = current.Next;
}

return default!;
return default;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
12 changes: 8 additions & 4 deletions src/Singularity/Collections/SinglyLinkedListKeyNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Singularity.Collections
Expand Down Expand Up @@ -67,14 +68,16 @@ public sealed class Enumerator : IEnumerator<TValue>
/// <summary>
/// The value of the current node.
/// </summary>
[AllowNull]
[MaybeNull]
public TValue Current { get; private set; }

object IEnumerator.Current => Current!;
object? IEnumerator.Current => Current;

internal Enumerator(SinglyLinkedListKeyNode<TKey, TValue> list)
{
_node = list;
Current = default!;
Current = default;
}

/// <inheritdoc />
Expand Down Expand Up @@ -108,16 +111,17 @@ internal static class SinglyLinkedListKeyNodeExtensions
return new SinglyLinkedListKeyNode<TKey, TValue>(previous, in key, in value);
}

[return: MaybeNull]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TValue GetOrDefault<TKey, TValue>(this SinglyLinkedListKeyNode<TKey, TValue>? list, TKey key)
{
while (list != null)
{
if (ReferenceEquals(list.Key, key)) return list.Value;
list = list.Next!;
list = list.Next;
}

return default!;
return default;
}
}
}
9 changes: 6 additions & 3 deletions src/Singularity/Collections/SinglyLinkedListNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Singularity.Collections
Expand Down Expand Up @@ -74,14 +75,16 @@ public sealed class Enumerator : IEnumerator<T>
/// <summary>
/// The value of the current node.
/// </summary>
[AllowNull]
[MaybeNull]
public T Current { get; private set; }

object IEnumerator.Current => Current!;
object? IEnumerator.Current => Current;

internal Enumerator(SinglyLinkedListNode<T> list)
{
_node = list;
Current = default!;
Current = default;
}

/// <inheritdoc />
Expand Down Expand Up @@ -143,4 +146,4 @@ public static SinglyLinkedListNode<T> Add<T>(this SinglyLinkedListNode<T>? previ
return previous;
}
}
}
}
Loading

0 comments on commit 44dfa61

Please sign in to comment.