Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ArgumentException.ThrowIfNullOrEmpty #50666

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ private protected ProtectedBrowserStorage(string storeName, IJSRuntime jsRuntime
throw new PlatformNotSupportedException($"{GetType()} cannot be used when running in a browser.");
}

if (string.IsNullOrEmpty(storeName))
{
throw new ArgumentException("The value cannot be null or empty", nameof(storeName));
}
ArgumentException.ThrowIfNullOrEmpty(storeName);

_storeName = storeName;
_jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime));
Expand Down Expand Up @@ -71,15 +68,9 @@ public ValueTask SetAsync(string key, object value)
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
public ValueTask SetAsync(string purpose, string key, object value)
{
if (string.IsNullOrEmpty(purpose))
{
throw new ArgumentException("Cannot be null or empty", nameof(purpose));
}
ArgumentException.ThrowIfNullOrEmpty(purpose);

if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Cannot be null or empty", nameof(key));
}
ArgumentException.ThrowIfNullOrEmpty(key);

return SetProtectedJsonAsync(key, Protect(purpose, value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ internal void Add([DynamicallyAccessedMembers(LinkerFlags.Component)] Type compo
{
Add(componentType, identifier);

if (string.IsNullOrEmpty(javaScriptInitializer))
{
throw new ArgumentException($"'{nameof(javaScriptInitializer)}' cannot be null or empty.", nameof(javaScriptInitializer));
}
ArgumentException.ThrowIfNullOrEmpty(javaScriptInitializer);

// Since it has a JS initializer, prepare the metadata we'll supply to JS code
if (!JSComponentIdentifiersByInitializer.TryGetValue(javaScriptInitializer, out var identifiersForInitializer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ public DeploymentParameters(
RuntimeFlavor runtimeFlavor,
RuntimeArchitecture runtimeArchitecture)
{
if (string.IsNullOrEmpty(applicationPath))
{
throw new ArgumentException("Value cannot be null.", nameof(applicationPath));
}
ArgumentException.ThrowIfNullOrEmpty(applicationPath);

if (!Directory.Exists(applicationPath))
{
Expand Down
20 changes: 4 additions & 16 deletions src/Http/Http.Results/src/TypedResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,7 @@ public static PhysicalFileHttpResult PhysicalFile(
EntityTagHeaderValue? entityTag = null,
bool enableRangeProcessing = false)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("Argument cannot be null or empty", nameof(path));
}
ArgumentException.ThrowIfNullOrEmpty(path);

return new(path, contentType)
{
Expand Down Expand Up @@ -547,10 +544,7 @@ public static VirtualFileHttpResult VirtualFile(
EntityTagHeaderValue? entityTag = null,
bool enableRangeProcessing = false)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("Argument cannot be null or empty", nameof(path));
}
ArgumentException.ThrowIfNullOrEmpty(path);

return new(path, contentType)
{
Expand All @@ -576,10 +570,7 @@ public static VirtualFileHttpResult VirtualFile(
/// <returns>The created <see cref="RedirectHttpResult"/> for the response.</returns>
public static RedirectHttpResult Redirect([StringSyntax(StringSyntaxAttribute.Uri)] string url, bool permanent = false, bool preserveMethod = false)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException("Argument cannot be null or empty", nameof(url));
}
ArgumentException.ThrowIfNullOrEmpty(url);

return new(url, permanent, preserveMethod);
}
Expand All @@ -599,10 +590,7 @@ public static RedirectHttpResult Redirect([StringSyntax(StringSyntaxAttribute.Ur
/// <returns>The created <see cref="RedirectHttpResult"/> for the response.</returns>
public static RedirectHttpResult LocalRedirect([StringSyntax(StringSyntaxAttribute.Uri, UriKind.Relative)] string localUrl, bool permanent = false, bool preserveMethod = false)
{
if (string.IsNullOrEmpty(localUrl))
{
throw new ArgumentException("Argument cannot be null or empty", nameof(localUrl));
}
ArgumentException.ThrowIfNullOrEmpty(localUrl);

return new(localUrl, acceptLocalUrlOnly: true, permanent, preserveMethod);
}
Expand Down
45 changes: 9 additions & 36 deletions src/Http/Routing/src/Patterns/RoutePatternFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,10 +727,7 @@ private static RoutePatternPathSegment SegmentCore(RoutePatternPart[] parts)
/// <returns>The <see cref="RoutePatternLiteralPart"/>.</returns>
public static RoutePatternLiteralPart LiteralPart(string content)
{
if (string.IsNullOrEmpty(content))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(content));
}
ArgumentException.ThrowIfNullOrEmpty(content);

if (content.Contains('?'))
{
Expand All @@ -753,10 +750,7 @@ private static RoutePatternLiteralPart LiteralPartCore(string content)
/// <returns>The <see cref="RoutePatternSeparatorPart"/>.</returns>
public static RoutePatternSeparatorPart SeparatorPart(string content)
{
if (string.IsNullOrEmpty(content))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(content));
}
ArgumentException.ThrowIfNullOrEmpty(content);

return SeparatorPartCore(content);
}
Expand All @@ -774,10 +768,7 @@ private static RoutePatternSeparatorPart SeparatorPartCore(string content)
/// <returns>The <see cref="RoutePatternParameterPart"/>.</returns>
public static RoutePatternParameterPart ParameterPart(string parameterName)
{
if (string.IsNullOrEmpty(parameterName))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}
ArgumentException.ThrowIfNullOrEmpty(parameterName);

if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
Expand All @@ -800,10 +791,7 @@ public static RoutePatternParameterPart ParameterPart(string parameterName)
/// <returns>The <see cref="RoutePatternParameterPart"/>.</returns>
public static RoutePatternParameterPart ParameterPart(string parameterName, object @default)
{
if (string.IsNullOrEmpty(parameterName))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}
ArgumentException.ThrowIfNullOrEmpty(parameterName);

if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
Expand All @@ -830,10 +818,7 @@ public static RoutePatternParameterPart ParameterPart(
object? @default,
RoutePatternParameterKind parameterKind)
{
if (string.IsNullOrEmpty(parameterName))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}
ArgumentException.ThrowIfNullOrEmpty(parameterName);

if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
Expand Down Expand Up @@ -867,10 +852,7 @@ public static RoutePatternParameterPart ParameterPart(
RoutePatternParameterKind parameterKind,
IEnumerable<RoutePatternParameterPolicyReference> parameterPolicies)
{
if (string.IsNullOrEmpty(parameterName))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}
ArgumentException.ThrowIfNullOrEmpty(parameterName);

if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
Expand Down Expand Up @@ -906,10 +888,7 @@ public static RoutePatternParameterPart ParameterPart(
RoutePatternParameterKind parameterKind,
params RoutePatternParameterPolicyReference[] parameterPolicies)
{
if (string.IsNullOrEmpty(parameterName))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}
ArgumentException.ThrowIfNullOrEmpty(parameterName);

if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
Expand Down Expand Up @@ -1008,10 +987,7 @@ public static RoutePatternParameterPolicyReference Constraint(IRouteConstraint c
#endif
public static RoutePatternParameterPolicyReference Constraint(string constraint)
{
if (string.IsNullOrEmpty(constraint))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(constraint));
}
ArgumentException.ThrowIfNullOrEmpty(constraint);

return ParameterPolicyCore(constraint);
}
Expand Down Expand Up @@ -1041,10 +1017,7 @@ public static RoutePatternParameterPolicyReference ParameterPolicy(IParameterPol
#endif
public static RoutePatternParameterPolicyReference ParameterPolicy(string parameterPolicy)
{
if (string.IsNullOrEmpty(parameterPolicy))
{
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterPolicy));
}
ArgumentException.ThrowIfNullOrEmpty(parameterPolicy);

return ParameterPolicyCore(parameterPolicy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public JSInvokableAttribute()
/// <param name="identifier">An identifier for the method, which must be unique within the scope of the assembly.</param>
public JSInvokableAttribute(string identifier)
{
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("Cannot be null or empty", nameof(identifier));
}
ArgumentException.ThrowIfNullOrEmpty(identifier);

Identifier = identifier;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/Rewrite/src/ApacheModRewrite/FlagParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ internal static class FlagParser

public static Flags Parse(string flagString)
{
if (string.IsNullOrEmpty(flagString))
{
throw new ArgumentException("Argument cannot be null or empty string.", nameof(flagString));
}
ArgumentException.ThrowIfNullOrEmpty(flagString);

// Check that flags are contained within []
// Guaranteed to have a length of at least 1 here, so this will never throw for indexing.
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/Session/src/DistributedSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public DistributedSession(
{
ArgumentNullException.ThrowIfNull(cache);

if (string.IsNullOrEmpty(sessionKey))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(sessionKey));
}
ArgumentException.ThrowIfNullOrEmpty(sessionKey);

ArgumentNullException.ThrowIfNull(tryEstablishSession);
ArgumentNullException.ThrowIfNull(loggerFactory);
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/Session/src/DistributedSessionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ public DistributedSessionStore(IDistributedCache cache, ILoggerFactory loggerFac
/// <inheritdoc />
public ISession Create(string sessionKey, TimeSpan idleTimeout, TimeSpan ioTimeout, Func<bool> tryEstablishSession, bool isNewSessionKey)
{
if (string.IsNullOrEmpty(sessionKey))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(sessionKey));
}
ArgumentException.ThrowIfNullOrEmpty(sessionKey);

ArgumentNullException.ThrowIfNull(tryEstablishSession);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public class AngularCliBuilder : ISpaPrerendererBuilder
/// <param name="npmScript">The name of the script in your package.json file that builds the server-side bundle for your Angular application.</param>
public AngularCliBuilder(string npmScript)
{
if (string.IsNullOrEmpty(npmScript))
{
throw new ArgumentException("Cannot be null or empty.", nameof(npmScript));
}
ArgumentException.ThrowIfNullOrEmpty(npmScript);

_scriptName = npmScript;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ public static void Attach(
var pkgManagerCommand = spaBuilder.Options.PackageManagerCommand;
var sourcePath = spaBuilder.Options.SourcePath;
var devServerPort = spaBuilder.Options.DevServerPort;
if (string.IsNullOrEmpty(sourcePath))
{
throw new ArgumentException("Property 'SourcePath' cannot be null or empty", nameof(spaBuilder));
}
ArgumentException.ThrowIfNullOrEmpty(sourcePath);

if (string.IsNullOrEmpty(scriptName))
{
throw new ArgumentException("Cannot be null or empty", nameof(scriptName));
}
ArgumentException.ThrowIfNullOrEmpty(scriptName);

// Start Angular CLI and attach to middleware pipeline
var appBuilder = spaBuilder.ApplicationBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,9 @@ internal sealed class NodeScriptRunner : IDisposable

public NodeScriptRunner(string workingDirectory, string scriptName, string? arguments, IDictionary<string, string>? envVars, string pkgManagerCommand, DiagnosticSource diagnosticSource, CancellationToken applicationStoppingToken)
{
if (string.IsNullOrEmpty(workingDirectory))
{
throw new ArgumentException("Cannot be null or empty.", nameof(workingDirectory));
}

if (string.IsNullOrEmpty(scriptName))
{
throw new ArgumentException("Cannot be null or empty.", nameof(scriptName));
}

if (string.IsNullOrEmpty(pkgManagerCommand))
{
throw new ArgumentException("Cannot be null or empty.", nameof(pkgManagerCommand));
}
ArgumentException.ThrowIfNullOrEmpty(workingDirectory);
ArgumentException.ThrowIfNullOrEmpty(scriptName);
ArgumentException.ThrowIfNullOrEmpty(pkgManagerCommand);

var exeToRun = pkgManagerCommand;
var completeArguments = $"run {scriptName} -- {arguments ?? string.Empty}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@ public static void Attach(
var pkgManagerCommand = spaBuilder.Options.PackageManagerCommand;
var sourcePath = spaBuilder.Options.SourcePath;
var devServerPort = spaBuilder.Options.DevServerPort;
if (string.IsNullOrEmpty(sourcePath))
{
throw new ArgumentException("Property 'SourcePath' cannot be null or empty", nameof(spaBuilder));
}

if (string.IsNullOrEmpty(scriptName))
{
throw new ArgumentException("Cannot be null or empty", nameof(scriptName));
}
ArgumentException.ThrowIfNullOrEmpty(sourcePath);
ArgumentException.ThrowIfNullOrEmpty(scriptName);

// Start create-react-app and attach to middleware pipeline
var appBuilder = spaBuilder.ApplicationBuilder;
Expand Down
10 changes: 2 additions & 8 deletions src/Middleware/Spa/SpaServices.Extensions/src/SpaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ public PathString DefaultPage
get => _defaultPage;
set
{
if (string.IsNullOrEmpty(value.Value))
{
throw new ArgumentException($"The value for {nameof(DefaultPage)} cannot be null or empty.");
}
ArgumentException.ThrowIfNullOrEmpty(value, nameof(DefaultPage));

_defaultPage = value;
}
Expand Down Expand Up @@ -86,10 +83,7 @@ public string PackageManagerCommand
get => _packageManagerCommand;
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException($"The value for {nameof(PackageManagerCommand)} cannot be null or empty.");
}
ArgumentException.ThrowIfNullOrEmpty(value, nameof(PackageManagerCommand));

_packageManagerCommand = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using Microsoft.AspNetCore.Mvc.Abstractions;

Check failure on line 5 in src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs#L5

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs(5,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 5 in src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs#L5

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs(5,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 5 in src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs#L5

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs(5,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 5 in src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs#L5

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs(5,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 5 in src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs#L5

src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelMetadataIdentity.cs(5,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;

Expand Down Expand Up @@ -53,10 +53,7 @@
ArgumentNullException.ThrowIfNull(modelType);
ArgumentNullException.ThrowIfNull(containerType);

if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(name));
}
ArgumentException.ThrowIfNullOrEmpty(name);

return new ModelMetadataIdentity(modelType, name, containerType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public HttpMethodActionConstraint(IEnumerable<string> httpMethods)

foreach (var method in httpMethods)
{
if (string.IsNullOrEmpty(method))
{
throw new ArgumentException("httpMethod cannot be null or empty");
}
ArgumentException.ThrowIfNullOrEmpty(method);

methods.Add(method);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Mvc/Mvc.Core/src/ApiConventionMethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public ApiConventionMethodAttribute(Type conventionType, string methodName)
ConventionType = conventionType ?? throw new ArgumentNullException(nameof(conventionType));
ApiConventionTypeAttribute.EnsureValid(conventionType);

if (string.IsNullOrEmpty(methodName))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(methodName));
}
ArgumentException.ThrowIfNullOrEmpty(methodName);

Method = GetConventionMethod(conventionType, methodName);
}
Expand Down
Loading
Loading