Skip to content

Commit

Permalink
Nullable annotations for ErrorUtilities (#10272)
Browse files Browse the repository at this point in the history
This will help with using these from new code that's nullable-
aware. The biggest helpful bit is the `[NotNull]` ones that let the
compiler know to assume that parameter is non-null in the rest
of the method, which should eliminate the need for some `!`s
after calling `VerifyThrowArgumentNull(whatever)`.
  • Loading branch information
rainersigwald authored Jun 20, 2024
1 parent 3c12d57 commit 48003ed
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 103 deletions.
20 changes: 12 additions & 8 deletions src/Framework/ErrorUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;

#nullable disable
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Build.Framework
{
Expand All @@ -22,11 +21,11 @@ internal class FrameworkErrorUtilities
/// </summary>
/// <param name="condition"></param>
/// <param name="unformattedMessage"></param>
internal static void VerifyThrow(bool condition, string unformattedMessage)
internal static void VerifyThrow([DoesNotReturnIf(false)] bool condition, string unformattedMessage)
{
if (!condition)
{
ThrowInternalError(unformattedMessage, null, null);
ThrowInternalError(unformattedMessage, innerException: null, args: null);
}
}

Expand All @@ -37,9 +36,9 @@ internal static void VerifyThrow(bool condition, string unformattedMessage)
/// </summary>
/// <param name="parameter">The value of the argument.</param>
/// <param name="parameterName">Parameter that should not be null.</param>
internal static void VerifyThrowInternalNull(object parameter, string parameterName)
internal static void VerifyThrowInternalNull([NotNull] object? parameter, string parameterName)
{
if (parameter == null)
if (parameter is null)
{
ThrowInternalError("{0} unexpectedly null", innerException: null, args: parameterName);
}
Expand All @@ -49,9 +48,14 @@ internal static void VerifyThrowInternalNull(object parameter, string parameterN
/// Throws InternalErrorException.
/// This is only for situations that would mean that there is a bug in MSBuild itself.
/// </summary>
internal static void ThrowInternalError(string message, Exception innerException, params object[] args)
[DoesNotReturn]
internal static void ThrowInternalError(string message, Exception? innerException, params object?[]? args)
{
throw new InternalErrorException(string.Format(message, args), innerException);
throw new InternalErrorException(
args is null ?
message :
string.Format(message, args),
innerException);
}
}
}
1 change: 1 addition & 0 deletions src/MSBuildTaskHost/MSBuildTaskHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Link>IExtendedBuildEventArgs.cs</Link>
</Compile>
<Compile Include="..\Framework\AssemblyUtilities.cs" />
<Compile Include="..\Framework\NullableAttributes.cs" />
<Compile Include="..\Framework\ResponseFileUsedEventArgs.cs" />
<Compile Include="..\Shared\BufferedReadStream.cs" />
<Compile Include="..\Shared\CollectionHelpers.cs" />
Expand Down
Loading

0 comments on commit 48003ed

Please sign in to comment.