Skip to content

Commit

Permalink
fix more nullables (#33966)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra committed Feb 8, 2023
1 parent e14c1dd commit 95de9f7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static string GetIngestionEndpoint(this AzureCoreConnectionString conne
else if (connectionString.TryGetNonRequiredValue(Constants.EndpointSuffixKey, out string? endpointSuffix))
{
var location = connectionString.GetNonRequired(Constants.LocationKey);
if (!TryBuildUri(prefix: Constants.IngestionPrefix, suffix: endpointSuffix!, location: location, uri: out uri))
if (!TryBuildUri(prefix: Constants.IngestionPrefix, suffix: endpointSuffix, location: location, uri: out uri))
{
throw new ArgumentException($"The value for {Constants.EndpointSuffixKey} is invalid. '{endpointSuffix}'");
}
Expand Down Expand Up @@ -126,7 +126,7 @@ internal static bool TryBuildUri(string prefix, string suffix, out Uri uri, stri
/// <summary>
/// This method wraps <see cref="AzureCoreConnectionString.GetNonRequired(string)"/> in a null check.
/// </summary>
internal static bool TryGetNonRequiredValue(this AzureCoreConnectionString connectionString, string key, out string? value)
internal static bool TryGetNonRequiredValue(this AzureCoreConnectionString connectionString, string key, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out string? value)
{
value = connectionString.GetNonRequired(key);
return value != null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// The following code was borrowed from https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
// These classes are available in .NET versions after NetStandard2.0
// For more information see: https://learn.microsoft.com/dotnet/csharp/language-reference/attributes/nullable-analysis

#if NETSTANDARD2_0

namespace System.Diagnostics.CodeAnalysis
{
#pragma warning disable SA1649 // File name should match first type name

/// <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)]
internal 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; }
}

#pragma warning restore SA1649 // File name should match first type name
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal static class StorageHelper
if (TryCreateTelemetryDirectory(path: environmentVars["LOCALAPPDATA"]?.ToString(), createdDirectoryPath: out dirPath)
|| TryCreateTelemetryDirectory(path: environmentVars["TEMP"]?.ToString(), createdDirectoryPath: out dirPath))
{
s_defaultStorageDirectory = dirPath!;
s_defaultStorageDirectory = dirPath;
return s_defaultStorageDirectory;
}
}
Expand All @@ -38,7 +38,7 @@ internal static class StorageHelper
|| TryCreateTelemetryDirectory(path: "/var/tmp/", createdDirectoryPath: out dirPath)
|| TryCreateTelemetryDirectory(path: "/tmp/", createdDirectoryPath: out dirPath))
{
s_defaultStorageDirectory = dirPath!;
s_defaultStorageDirectory = dirPath;
return s_defaultStorageDirectory;
}
}
Expand All @@ -53,7 +53,7 @@ internal static class StorageHelper
/// <param name="path">Base directory.</param>
/// <param name="createdDirectoryPath">Full directory.</param>
/// <returns><see langword= "true"/> if directory is created.</returns>
private static bool TryCreateTelemetryDirectory(string? path, out string? createdDirectoryPath)
private static bool TryCreateTelemetryDirectory(string? path, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out string? createdDirectoryPath)
{
createdDirectoryPath = null;
if (path == null)
Expand Down

0 comments on commit 95de9f7

Please sign in to comment.