Skip to content

Commit 2f9f503

Browse files
committed
#3334 - fix nullable warnings
1 parent bdcfd1b commit 2f9f503

26 files changed

+86
-66
lines changed

src/GitVersion.Core.Tests/VersionCalculation/Strategies/ConfigNextVersionBaseVersionStrategyTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public void ConfigNextVersionTest(string nextVersion, string expectedVersion)
4949
var branchMock = GitToolsTestingExtensions.CreateMockBranch("main", GitToolsTestingExtensions.CreateMockCommit());
5050
var branchConfiguration = context.Configuration.GetBranchConfiguration(branchMock);
5151
var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration);
52+
53+
strategy.ShouldNotBeNull();
5254
return strategy.GetBaseVersions(new(branchMock, effectiveConfiguration)).SingleOrDefault();
5355
}
5456
}

src/GitVersion.Core.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public void ShouldNotAllowIncrementOfVersion()
4040
var context = contextBuilder.ServicesProvider.GetRequiredService<Lazy<GitVersionContext>>().Value;
4141
var branchConfiguration = context.Configuration.GetBranchConfiguration(mockBranch);
4242
var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration);
43+
44+
strategy.ShouldNotBeNull();
4345
var baseVersion = strategy.GetBaseVersions(new(mockBranch, effectiveConfiguration)).Single();
4446

4547
baseVersion.ShouldIncrement.ShouldBe(false);
@@ -173,6 +175,8 @@ private static void AssertMergeMessage(string message, string? expectedVersion,
173175
var context = contextBuilder.ServicesProvider.GetRequiredService<Lazy<GitVersionContext>>().Value;
174176
var branchConfiguration = context.Configuration.GetBranchConfiguration(mockBranch);
175177
var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration);
178+
179+
strategy.ShouldNotBeNull();
176180
var baseVersion = strategy.GetBaseVersions(new(mockBranch, effectiveConfiguration)).SingleOrDefault();
177181

178182
if (expectedVersion == null)

src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public void CanTakeVersionFromNameOfReleaseBranch(string branchName, string expe
2525
var configuration = GitFlowConfigurationBuilder.New.Build();
2626
var branchConfiguration = configuration.GetBranchConfiguration(branchName);
2727
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);
28+
29+
strategy.ShouldNotBeNull();
2830
var baseVersion = strategy.GetBaseVersions(new(gitRepository.FindBranch(branchName)!, effectiveConfiguration)).Single();
2931

3032
baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion);
@@ -45,6 +47,8 @@ public void ShouldNotTakeVersionFromNameOfNonReleaseBranch(string branchName)
4547
var configuration = GitFlowConfigurationBuilder.New.Build();
4648
var branchConfiguration = configuration.GetBranchConfiguration(branchName);
4749
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);
50+
51+
strategy.ShouldNotBeNull();
4852
var baseVersions = strategy.GetBaseVersions(new(gitRepository.FindBranch(branchName)!, effectiveConfiguration));
4953

5054
baseVersions.ShouldBeEmpty();
@@ -67,6 +71,9 @@ public void CanTakeVersionFromNameOfConfiguredReleaseBranch(string branchName, s
6771
var configuration = GitFlowConfigurationBuilder.New.Build();
6872
var branchConfiguration = configuration.GetBranchConfiguration(branchName);
6973
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);
74+
75+
strategy.ShouldNotBeNull();
76+
7077
var baseVersion = strategy.GetBaseVersions(new(gitRepository.FindBranch(branchName)!, effectiveConfiguration)).Single();
7178

7279
baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion);
@@ -90,12 +97,15 @@ public void CanTakeVersionFromNameOfRemoteReleaseBranch(string branchName, strin
9097
var configuration = GitFlowConfigurationBuilder.New.Build();
9198
var branchConfiguration = configuration.GetBranchConfiguration(branchName);
9299
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);
100+
101+
strategy.ShouldNotBeNull();
102+
93103
var baseVersion = strategy.GetBaseVersions(new(gitRepository.FindBranch(branchName)!, effectiveConfiguration)).Single();
94104

95105
baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion);
96106
}
97107

98-
private static IVersionStrategy GetVersionStrategy(string workingDirectory, IGitRepository repository, string branch, GitVersionConfiguration? configuration = null)
108+
private static IVersionStrategy? GetVersionStrategy(string workingDirectory, IGitRepository repository, string branch, GitVersionConfiguration? configuration = null)
99109
{
100110
var sp = BuildServiceProvider(workingDirectory, repository, branch, configuration);
101111
return sp.GetServiceForType<IVersionStrategy, VersionInBranchNameVersionStrategy>();

src/GitVersion.Core/Core/GitVersionModule.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Extensions;
12
using Microsoft.Extensions.DependencyInjection;
23

34
namespace GitVersion;
@@ -6,8 +7,10 @@ public abstract class GitVersionModule : IGitVersionModule
67
{
78
public abstract void RegisterTypes(IServiceCollection services);
89

9-
protected static IEnumerable<Type> FindAllDerivedTypes<T>(Assembly assembly)
10+
protected static IEnumerable<Type> FindAllDerivedTypes<T>(Assembly? assembly)
1011
{
12+
assembly.NotNull();
13+
1114
var derivedType = typeof(T);
1215
return assembly.GetTypes().Where(t => t != derivedType && derivedType.IsAssignableFrom(t));
1316
}

src/GitVersion.Core/Core/MergeCommitFinder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal class MergeCommitFinder
88
{
99
private readonly IEnumerable<IBranch> excludedBranches;
1010
private readonly ILog log;
11-
private readonly Dictionary<IBranch?, List<BranchCommit>> mergeBaseCommitsCache = new();
11+
private readonly Dictionary<IBranch, List<BranchCommit>> mergeBaseCommitsCache = new();
1212
private readonly RepositoryStore repositoryStore;
1313
private readonly GitVersionConfiguration configuration;
1414

@@ -24,10 +24,10 @@ public IEnumerable<BranchCommit> FindMergeCommitsFor(IBranch branch)
2424
{
2525
branch = branch.NotNull();
2626

27-
if (this.mergeBaseCommitsCache.ContainsKey(branch))
27+
if (this.mergeBaseCommitsCache.TryGetValue(branch, out var mergeCommitsFor))
2828
{
29-
this.log.Debug($"Cache hit for getting merge commits for branch {branch?.Name.Canonical}.");
30-
return this.mergeBaseCommitsCache[branch];
29+
this.log.Debug($"Cache hit for getting merge commits for branch {branch.Name.Canonical}.");
30+
return mergeCommitsFor;
3131
}
3232

3333
var branchMergeBases = FindMergeBases(branch)

src/GitVersion.Core/Extensions/CommonExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public static string NotNullOrEmpty([NotNull] this string? value, [CallerArgumen
1515
throw new ArgumentException("The parameter is null or empty.", name);
1616
}
1717

18-
#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
19-
return value!;
20-
#pragma warning restore CS8777 // Parameter must have a non-null value when exiting.
18+
return value;
2119
}
2220

2321
public static string NotNullOrWhitespace([NotNull] this string? value, [CallerArgumentExpression("value")] string name = "")
@@ -27,8 +25,6 @@ public static string NotNullOrWhitespace([NotNull] this string? value, [CallerAr
2725
throw new ArgumentException("The parameter is null or empty or contains only white space.", name);
2826
}
2927

30-
#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
31-
return value!;
32-
#pragma warning restore CS8777 // Parameter must have a non-null value when exiting.
28+
return value;
3329
}
3430
}

src/GitVersion.Core/Extensions/DictionaryExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict,
66
{
77
if (dict is null) throw new ArgumentNullException(nameof(dict));
88
if (getValue is null) throw new ArgumentNullException(nameof(getValue));
9-
if (!dict.TryGetValue(key, out TValue value))
9+
10+
if (!dict.TryGetValue(key, out var value))
1011
{
1112
value = getValue();
1213
dict.Add(key, value);

src/GitVersion.Core/Extensions/ObjectExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public static void Deconstruct<TKey, TValue>(
1616
value = kvp.Value;
1717
}
1818

19-
public static IEnumerable<KeyValuePair<string, string>> GetProperties(this object obj)
19+
public static Dictionary<string, string> GetProperties(this object obj)
2020
{
2121
var type = typeof(string);
2222
return obj.GetType().GetProperties()
2323
.Where(p => p.PropertyType == type && !p.GetIndexParameters().Any() && !p.GetCustomAttributes(typeof(ReflectionIgnoreAttribute), false).Any())
24-
.Select(p => new KeyValuePair<string, string>(p.Name, (string)p.GetValue(obj, null)));
24+
.ToDictionary(p => p.Name, p => Convert.ToString(p.GetValue(obj, null))!);
2525
}
2626
}

src/GitVersion.Core/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ public static IServiceCollection AddModule(this IServiceCollection serviceCollec
1111
}
1212

1313
public static TService GetServiceForType<TService, TType>(this IServiceProvider serviceProvider) =>
14-
serviceProvider.GetServices<TService>().SingleOrDefault(t => t?.GetType() == typeof(TType));
14+
serviceProvider.GetServices<TService>().Single(t => t?.GetType() == typeof(TType));
1515
}

src/GitVersion.Core/Git/ReferenceName.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public static ReferenceName Parse(string canonicalName)
5656
public bool IsPullRequest { get; }
5757

5858
public bool Equals(ReferenceName? other) => equalityHelper.Equals(this, other);
59-
public int CompareTo(ReferenceName other) => comparerHelper.Compare(this, other);
60-
public override bool Equals(object obj) => Equals((obj as ReferenceName));
59+
public int CompareTo(ReferenceName? other) => comparerHelper.Compare(this, other);
60+
public override bool Equals(object? obj) => Equals((obj as ReferenceName));
6161
public override int GetHashCode() => equalityHelper.GetHashCode(this);
6262
public override string ToString() => Friendly;
6363

0 commit comments

Comments
 (0)