Skip to content

Commit

Permalink
Add MemberNotNullWhen to SemanticVersion.HasMetadata (#5465)
Browse files Browse the repository at this point in the history
  • Loading branch information
amis92 authored Oct 23, 2023
1 parent 041c6da commit b02fdbb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/NuGet.Core/NuGet.Versioning/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace NuGet.Versioning
Expand Down Expand Up @@ -231,6 +232,7 @@ public virtual bool IsPrerelease
/// <summary>
/// True if metadata exists for the version.
/// </summary>
[MemberNotNullWhen(true, nameof(Metadata))]
public virtual bool HasMetadata
{
get { return !string.IsNullOrEmpty(Metadata); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void SemanticVersionStrictEqualityPreRelease(string versionString)
[InlineData("1.2.3-A.00.B")]
public void TryParseStrictReturnsFalseIfVersionIsNotStrictSemVer(string version)
{
// Act
// Act
SemanticVersion? semanticVersion;
var result = SemanticVersion.TryParse(version, out semanticVersion);

Expand All @@ -109,6 +109,20 @@ public void ToString_ClassExtendingSemanticVersion_ReturnsDefaultFormat()
Assert.Equal("1.2.3", result);
}

[Fact]
public void Metadata_NoNullableWarning_After_HasMetadata_checked()
{
// Arrange
SemanticVersion target = new(1, 2, 3);

// Act
// should not result in a compiler warning CS8602: Dereference of a possibly null reference.
string result = target.HasMetadata ? target.Metadata.Substring(1) : "no-metadata";

// Assert
Assert.Equal("no-metadata", result);
}

private class ExtendedSemanticVersion : SemanticVersion
{
public ExtendedSemanticVersion(int major, int minor, int patch)
Expand Down

0 comments on commit b02fdbb

Please sign in to comment.