Skip to content

Commit 0b29e5a

Browse files
authored
Merge pull request #644 from dotnet/IsNull
Apply `is null` pattern and analyzer
2 parents 0d49d20 + 9ead478 commit 0b29e5a

32 files changed

+88
-80
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ csharp_new_line_before_catch = true
7979
csharp_new_line_before_finally = true
8080
csharp_new_line_before_members_in_object_initializers = true
8181
csharp_new_line_before_members_in_anonymous_types = true
82+
83+
# CSIsNull001: Use `is null` for null checks
84+
dotnet_diagnostic.CSIsNull001.severity = warning
85+
86+
# CSIsNull002: Use `is object` for non-null checks
87+
dotnet_diagnostic.CSIsNull002.severity = warning

src/Directory.Build.props

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
<LibGit2SharpNativeVersion>2.0.312</LibGit2SharpNativeVersion>
2525
</PropertyGroup>
2626
<ItemGroup>
27-
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.8.0-5.final" PrivateAssets="all" />
27+
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.11.0" PrivateAssets="all" />
2828
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
2929
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
30+
<PackageReference Include="CSharpIsNullAnalyzer" Version="0.1.288-beta">
31+
<PrivateAssets>all</PrivateAssets>
32+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
33+
</PackageReference>
3034
</ItemGroup>
3135
<ItemGroup>
3236
<None Include="$(MSBuildThisFileDirectory)..\3rdPartyNotices.txt" Pack="true" PackagePath="" />

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public async Task GetBuildVersion_In_Git_But_Head_Lacks_VersionFile()
219219
var repo = new Repository(this.RepoPath); // do not assign Repo property to avoid commits being generated later
220220
repo.Commit("empty", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
221221
this.WriteVersionFile("3.4");
222-
Assumes.True(repo.Index[VersionFile.JsonFileName] == null);
222+
Assumes.True(repo.Index[VersionFile.JsonFileName] is null);
223223
var buildResult = await this.BuildAsync();
224224
Assert.Equal("3.4.0." + this.GetVersion().Revision, buildResult.BuildVersion);
225225
Assert.Equal("3.4.0+" + repo.Head.Tip.Id.Sha.Substring(0, VersionOptions.DefaultGitCommitIdShortFixedLength), buildResult.AssemblyInformationalVersion);

src/NerdBank.GitVersioning.Tests/ManagedGit/DeltaStreamReaderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void ReadStreamTest()
8989

9090
DeltaInstruction? current;
9191

92-
while ((current = DeltaStreamReader.Read(stream)) != null)
92+
while ((current = DeltaStreamReader.Read(stream)) is not null)
9393
{
9494
instructions.Add(current.Value);
9595
}
@@ -139,7 +139,7 @@ public void ReadStreamTest_Memory()
139139

140140
DeltaInstruction? current;
141141

142-
while ((current = DeltaStreamReader.Read(ref memory)) != null)
142+
while ((current = DeltaStreamReader.Read(ref memory)) is not null)
143143
{
144144
instructions.Add(current.Value);
145145
}

src/NerdBank.GitVersioning.Tests/ReleaseManagerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public void PrepareRelease_Master(
320320

321321
// prepare release
322322
var releaseManager = new ReleaseManager();
323-
releaseManager.PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : Version.Parse(nextVersion)), parameterVersionIncrement);
323+
releaseManager.PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion is null ? null : Version.Parse(nextVersion)), parameterVersionIncrement);
324324

325325
// check if a branch was created
326326
Assert.Contains(this.LibGit2Repository.Branches, branch => branch.FriendlyName == expectedBranchName);
@@ -394,7 +394,7 @@ public void PrepareRelease_MasterWithVersionDecrement(string initialVersion, str
394394
// running PrepareRelease should result in an error
395395
// because we're trying to add a prerelease tag to a version without prerelease tag
396396
this.AssertError(
397-
() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : Version.Parse(nextVersion))),
397+
() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion is null ? null : Version.Parse(nextVersion))),
398398
ReleasePreparationError.VersionDecrement);
399399
}
400400

src/NerdBank.GitVersioning.Tests/RepoTestBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ protected void AddCommits(int count = 1)
143143

144144
protected Commit? WriteVersionTxtFile(string version = "1.2", string prerelease = "", string? relativeDirectory = null)
145145
{
146-
if (relativeDirectory == null)
146+
if (relativeDirectory is null)
147147
{
148148
relativeDirectory = string.Empty;
149149
}
@@ -163,7 +163,7 @@ protected void AddCommits(int count = 1)
163163
{
164164
Requires.NotNull(versionData, nameof(versionData));
165165

166-
if (relativeDirectory == null)
166+
if (relativeDirectory is null)
167167
{
168168
relativeDirectory = string.Empty;
169169
}
@@ -197,7 +197,7 @@ protected void AddCommits(int count = 1)
197197
if (Path.GetExtension(relativeFilePath) == ".json")
198198
{
199199
string txtFilePath = relativeFilePath.Substring(0, relativeFilePath.Length - 4) + "txt";
200-
if (!File.Exists(Path.Combine(this.RepoPath, txtFilePath)) && this.LibGit2Repository.Index[txtFilePath] != null)
200+
if (!File.Exists(Path.Combine(this.RepoPath, txtFilePath)) && this.LibGit2Repository.Index[txtFilePath] is not null)
201201
{
202202
this.LibGit2Repository.Index.Remove(txtFilePath);
203203
}

src/NerdBank.GitVersioning.Tests/TestUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal static void ExtractEmbeddedResource(string resourcePath, string extract
5454

5555
using (var stream = GetEmbeddedResource(resourcePath))
5656
{
57-
Requires.Argument(stream != null, nameof(resourcePath), "Resource not found.");
57+
Requires.Argument(stream is not null, nameof(resourcePath), "Resource not found.");
5858
using (var extractedFile = File.OpenWrite(extractedFilePath))
5959
{
6060
stream.CopyTo(extractedFile);

src/NerdBank.GitVersioning.Tests/VersionFileTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void SetVersion_WritesSimplestFile(string version, string assemblyVersion
140140
var versionOptions = new VersionOptions
141141
{
142142
Version = SemanticVersion.Parse(version),
143-
AssemblyVersion = assemblyVersion != null || precision != null ? new VersionOptions.AssemblyVersionOptions(assemblyVersion != null ? new Version(assemblyVersion) : null, precision) : null,
143+
AssemblyVersion = assemblyVersion is not null || precision is not null ? new VersionOptions.AssemblyVersionOptions(assemblyVersion is not null ? new Version(assemblyVersion) : null, precision) : null,
144144
VersionHeightOffset = versionHeightOffset,
145145
Inherit = inherit,
146146
};

src/NerdBank.GitVersioning/AssemblyVersionOptionsConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
5151
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
5252
{
5353
var data = value as VersionOptions.AssemblyVersionOptions;
54-
if (data != null)
54+
if (data is not null)
5555
{
5656
if (data.PrecisionOrDefault == VersionOptions.DefaultVersionPrecision && !this.includeDefaults)
5757
{

src/NerdBank.GitVersioning/CloudBuildServices/GitLab.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
internal class GitLab : ICloudBuild
1515
{
1616
public string BuildingBranch =>
17-
Environment.GetEnvironmentVariable("CI_COMMIT_TAG") == null ?
17+
Environment.GetEnvironmentVariable("CI_COMMIT_TAG") is null ?
1818
$"refs/heads/{Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME")}" : null;
1919

2020
public string BuildingRef => this.BuildingBranch ?? this.BuildingTag;
2121

2222
public string BuildingTag =>
23-
Environment.GetEnvironmentVariable("CI_COMMIT_TAG") != null ?
23+
Environment.GetEnvironmentVariable("CI_COMMIT_TAG") is not null ?
2424
$"refs/tags/{Environment.GetEnvironmentVariable("CI_COMMIT_TAG")}" : null;
2525

2626
public string GitCommitId => Environment.GetEnvironmentVariable("CI_COMMIT_SHA");

0 commit comments

Comments
 (0)