Skip to content

Commit 1158132

Browse files
authored
Nullable annotations for most of NuGet.ProjectModel (#7172)
1 parent f826ad4 commit 1158132

21 files changed

Lines changed: 139 additions & 152 deletions

.github/copilot-instructions.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Instructions
22

3-
When creating pull requests, always follow the [PR template](PULL_REQUEST_TEMPLATE.md).
3+
## General Guidelines
44

5-
Use the following coding guidelines: https://github.com/NuGet/NuGet.Client/blob/dev/docs/coding-guidelines.md
5+
- When creating pull requests, always follow the [PR template](PULL_REQUEST_TEMPLATE.md).
6+
- Always format before submitting a pull request.
67

7-
Always format before submitting a pull request.
8+
## Coding Standards
89

9-
Never use reflection.
10+
- Use the following coding guidelines: https://github.com/NuGet/NuGet.Client/blob/dev/docs/coding-guidelines.md
11+
- Never use reflection.
12+
13+
## Project-Specific Rules
14+
15+
- All files in the repository are nullable by default (project-level nullable enable). No need to add `#nullable enable` directives to individual files.

src/NuGet.Core/NuGet.Common/MsBuildStringUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static bool IsTrueOrEmpty(string? value)
9191
/// </summary>
9292
/// <param name="s">A comma or semicolon delimited list of NuGet log codes.</param>
9393
/// <returns>An <see cref="IList{T}" /> containing the <see cref="NuGetLogCode" /> values that were successfully parsed from the specified string.</returns>
94-
public static ImmutableArray<NuGetLogCode> GetNuGetLogCodes(string s)
94+
public static ImmutableArray<NuGetLogCode> GetNuGetLogCodes(string? s)
9595
{
9696
// The Split() method already checks for an empty string and returns Array.Empty<string>().
9797
string[] split = MSBuildStringUtility.Split(s, ';', ',');

src/NuGet.Core/NuGet.Common/PublicAPI.Shipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ static NuGet.Common.LoggingExtensions.GetName(this NuGet.Common.NuGetLogCode cod
576576
static NuGet.Common.LoggingExtensions.TryGetName(this NuGet.Common.NuGetLogCode code, out string? codeString) -> bool
577577
static NuGet.Common.MSBuildStringUtility.Convert(string? value) -> string?
578578
static NuGet.Common.MSBuildStringUtility.GetBooleanOrNull(string? value) -> bool?
579-
static NuGet.Common.MSBuildStringUtility.GetNuGetLogCodes(string! s) -> System.Collections.Immutable.ImmutableArray<NuGet.Common.NuGetLogCode>
579+
static NuGet.Common.MSBuildStringUtility.GetNuGetLogCodes(string? s) -> System.Collections.Immutable.ImmutableArray<NuGet.Common.NuGetLogCode>
580580
static NuGet.Common.MSBuildStringUtility.IsTrue(string? value) -> bool
581581
static NuGet.Common.MSBuildStringUtility.IsTrueOrEmpty(string? value) -> bool
582582
static NuGet.Common.MSBuildStringUtility.Split(string? s) -> string![]!

src/NuGet.Core/NuGet.ProjectModel/CentralTransitiveDependencyGroup.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using NuGet.Frameworks;
@@ -32,7 +30,7 @@ public CentralTransitiveDependencyGroup(NuGetFramework framework, IEnumerable<Li
3230

3331
public IEnumerable<LibraryDependency> TransitiveDependencies { get; }
3432

35-
public bool Equals(CentralTransitiveDependencyGroup other)
33+
public bool Equals(CentralTransitiveDependencyGroup? other)
3634
{
3735
if (other == null)
3836
{
@@ -52,7 +50,7 @@ public bool Equals(CentralTransitiveDependencyGroup other)
5250
return false;
5351
}
5452

55-
public override bool Equals(object obj)
53+
public override bool Equals(object? obj)
5654
{
5755
return Equals(obj as CentralTransitiveDependencyGroup);
5856
}

src/NuGet.Core/NuGet.ProjectModel/CircularMemoryStream.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.IO;
86

@@ -15,7 +13,7 @@ internal sealed class CircularMemoryStream : MemoryStream
1513
{
1614
private readonly byte[] _buffer;
1715

18-
internal event EventHandler<ArraySegment<byte>> OnFlush;
16+
internal event EventHandler<ArraySegment<byte>>? OnFlush;
1917

2018
internal CircularMemoryStream(byte[] buffer) : base(buffer)
2119
{

src/NuGet.Core/NuGet.ProjectModel/FileFormatException.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Globalization;
86
using Newtonsoft.Json;
@@ -22,7 +20,7 @@ public FileFormatException(string message, Exception innerException)
2220
{
2321
}
2422

25-
public string Path { get; private set; }
23+
public string? Path { get; private set; }
2624
public int Line { get; private set; }
2725
public int Column { get; private set; }
2826

src/NuGet.Core/NuGet.ProjectModel/HashObjectWriter.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.IO;
@@ -134,7 +132,7 @@ public void WriteNameValue(string name, bool value)
134132
_writer.WriteValue(value);
135133
}
136134

137-
public void WriteNameValue(string name, string value)
135+
public void WriteNameValue(string name, string? value)
138136
{
139137
if (name == null)
140138
{
@@ -260,11 +258,11 @@ public void WriteArrayEnd()
260258
--_nestLevel;
261259
}
262260

263-
private void OnFlush(object sender, ArraySegment<byte> bytes)
261+
private void OnFlush(object? sender, ArraySegment<byte> bytes)
264262
{
265263
if (bytes.Count > 0)
266264
{
267-
_hashFunc.Update(bytes.Array, bytes.Offset, bytes.Count);
265+
_hashFunc.Update(bytes.Array!, bytes.Offset, bytes.Count);
268266
}
269267
}
270268

src/NuGet.Core/NuGet.ProjectModel/IExternalProjectReferenceProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
4+
using System;
65
using System.Collections.Generic;
76

87
namespace NuGet.ProjectModel
98
{
109
/// <summary>
1110
/// Provides external project reference closures.
1211
/// </summary>
12+
[Obsolete("This API is unused and will be removed in a future release.")]
1313
public interface IExternalProjectReferenceProvider
1414
{
1515
/// <summary>

src/NuGet.Core/NuGet.ProjectModel/JTokenExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
4+
using System;
65
using System.Linq;
76
using Newtonsoft.Json.Linq;
87

98
namespace NuGet.ProjectModel
109
{
10+
[Obsolete("This class is unused and will be removed in a future release.")]
1111
public static class JTokenExtensions
1212
{
13-
public static T[] ValueAsArray<T>(this JToken jToken)
13+
public static T?[] ValueAsArray<T>(this JToken jToken)
1414
{
1515
return jToken.Select(a => a.Value<T>()).ToArray();
1616
}
1717

18-
public static T[] ValueAsArray<T>(this JToken jToken, string name)
18+
public static T?[]? ValueAsArray<T>(this JToken jToken, string name)
1919
{
2020
return jToken?[name]?.ValueAsArray<T>();
2121
}
2222

23-
public static T GetValue<T>(this JToken token, string name)
23+
public static T? GetValue<T>(this JToken token, string name)
2424
{
2525
if (token == null)
2626
{

src/NuGet.Core/NuGet.ProjectModel/PackageSpecUtility.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using NuGet.Versioning;
86

@@ -13,7 +11,7 @@ public static class PackageSpecUtility
1311
/// <summary>
1412
/// Apply a snapshot value.
1513
/// </summary>
16-
public static NuGetVersion SpecifySnapshot(string version, string snapshotValue)
14+
public static NuGetVersion SpecifySnapshot(string version, string? snapshotValue)
1715
{
1816
// Snapshots should be in the form 1.0.0-*, 1.0.0-beta-*, or 1.0.0-rc.*
1917
// Snapshots may not contain metadata such as 1.0.0+5.* or be stable versions such as 1.0.*
@@ -35,7 +33,7 @@ public static NuGetVersion SpecifySnapshot(string version, string snapshotValue)
3533
/// <summary>
3634
/// True if the string is a snapshot version.
3735
/// </summary>
38-
public static bool IsSnapshotVersion(string version)
36+
public static bool IsSnapshotVersion(string? version)
3937
{
4038
if (version != null
4139
&& version.EndsWith("*", StringComparison.Ordinal)
@@ -45,8 +43,7 @@ public static bool IsSnapshotVersion(string version)
4543
|| (version.EndsWith(".*", StringComparison.Ordinal))))
4644
{
4745
// Verify the version is valid
48-
NuGetVersion parsed = null;
49-
return NuGetVersion.TryParse(version.Substring(0, version.Length - 2), out parsed);
46+
return NuGetVersion.TryParse(version.Substring(0, version.Length - 2), out _);
5047
}
5148

5249
return false;

0 commit comments

Comments
 (0)