Skip to content

Add ArgumentException.ThrowIfNullOrWhiteSpace #86007

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

Merged
merged 2 commits into from
May 10, 2023
Merged
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 @@ -58,10 +58,7 @@ internal static void ThrowIfInvalidState(WebSocketState currentState, bool isDis

internal static void ValidateSubprotocol(string subProtocol)
{
if (string.IsNullOrWhiteSpace(subProtocol))
{
throw new ArgumentException(SR.net_WebSockets_InvalidEmptySubProtocol, nameof(subProtocol));
}
ArgumentException.ThrowIfNullOrWhiteSpace(subProtocol);

int indexOfInvalidChar = subProtocol.AsSpan().IndexOfAnyExcept(s_validSubprotocolChars);
if (indexOfInvalidChar >= 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
<data name="AllowedValuesAttribute_Invalid" xml:space="preserve">
<value>The {0} field does not equal any of the values specified in AllowedValuesAttribute.</value>
</data>
<data name="ArgumentIsNullOrWhitespace" xml:space="preserve">
<value>The argument '{0}' cannot be null, empty or contain only whitespace.</value>
</data>
<data name="AssociatedMetadataTypeTypeDescriptor_MetadataTypeContainsUnknownProperties" xml:space="preserve">
<value>The associated metadata type for type '{0}' contains the following unknown properties or fields: {1}. Please make sure that the names of these members match the names of the properties on the main type.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public ColumnAttribute()
/// <param name="name">The name of the column the property is mapped to.</param>
public ColumnAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name));
}

ArgumentException.ThrowIfNullOrWhiteSpace(name);
Name = name;
}

Expand Down Expand Up @@ -64,11 +60,7 @@ public string? TypeName
get => _typeName;
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(value)), nameof(value));
}

ArgumentException.ThrowIfNullOrWhiteSpace(value);
_typeName = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ public class ForeignKeyAttribute : Attribute
/// </param>
public ForeignKeyAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name));
}

ArgumentException.ThrowIfNullOrWhiteSpace(name);
Name = name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ public class InversePropertyAttribute : Attribute
/// <param name="property">The navigation property representing the other end of the same relationship.</param>
public InversePropertyAttribute(string property)
{
if (string.IsNullOrWhiteSpace(property))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(property)), nameof(property));
}

ArgumentException.ThrowIfNullOrWhiteSpace(property);
Property = property;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ public class TableAttribute : Attribute
/// <param name="name">The name of the table the class is mapped to.</param>
public TableAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name));
}

ArgumentException.ThrowIfNullOrWhiteSpace(name);
Name = name;
}

Expand All @@ -42,11 +38,7 @@ public string? Schema
get => _schema;
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(value)), nameof(value));
}

ArgumentException.ThrowIfNullOrWhiteSpace(value);
_schema = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ public static void Ctor_String(string name)

[Theory]
[InlineData(null)]
public static void Ctor_String_NullName_ThrowsArgumentException(string name)
{
AssertExtensions.Throws<ArgumentNullException>("name", null, () => new ColumnAttribute(name));
}

[Theory]
[InlineData("")]
[InlineData(" \t\r\n")]
public static void Ctor_String_NullOrWhitespaceName_ThrowsArgumentException(string name)
public static void Ctor_String_WhitespaceName_ThrowsArgumentException(string name)
{
AssertExtensions.Throws<ArgumentException>("name", null, () => new ColumnAttribute(name));
}
Expand Down Expand Up @@ -61,9 +67,16 @@ public static void TypeName_Set_ReturnsExpected(string value)

[Theory]
[InlineData(null)]
public static void TypeName_Set_NullValue_ThrowsArgumentException(string value)
{
ColumnAttribute attribute = new ColumnAttribute();
AssertExtensions.Throws<ArgumentNullException>("value", null, () => attribute.TypeName = value);
}

[Theory]
[InlineData("")]
[InlineData(" \t\r\n")]
public static void TypeName_Set_NullOrWhitespaceValue_ThrowsArgumentException(string value)
public static void TypeName_Set_WhitespaceValue_ThrowsArgumentException(string value)
{
ColumnAttribute attribute = new ColumnAttribute();
AssertExtensions.Throws<ArgumentException>("value", null, () => attribute.TypeName = value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ public static void Ctor_String(string name)

[Theory]
[InlineData(null)]
public static void Ctor_String_NullName_ThrowsArgumentException(string name)
{
AssertExtensions.Throws<ArgumentNullException>("name", null, () => new ForeignKeyAttribute(name));
}

[Theory]
[InlineData("")]
[InlineData(" \t\r\n")]
public static void Ctor_String_NullOrWhitespaceName_ThrowsArgumentException(string name)
public static void Ctor_String_WhitespaceName_ThrowsArgumentException(string name)
{
AssertExtensions.Throws<ArgumentException>("name", null, () => new ForeignKeyAttribute(name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ public static void Ctor_String(string property)

[Theory]
[InlineData(null)]
public static void Ctor_String_NullProperty_ThrowsArgumentException(string property)
{
AssertExtensions.Throws<ArgumentNullException>("property", null, () => new InversePropertyAttribute(property));
}

[Theory]
[InlineData("")]
[InlineData(" \t\r\n")]
public static void Ctor_String_NullOrWhitespaceProperty_ThrowsArgumentException(string property)
public static void Ctor_String_WhitespaceProperty_ThrowsArgumentException(string property)
{
AssertExtensions.Throws<ArgumentException>("property", null, () => new InversePropertyAttribute(property));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ public static void Ctor_String(string name)

[Theory]
[InlineData(null)]
public static void Ctor_String_NullName_ThrowsArgumentException(string name)
{
AssertExtensions.Throws<ArgumentNullException>("name", null, () => new TableAttribute(name));
}

[Theory]
[InlineData("")]
[InlineData(" \t\r\n")]
public static void Ctor_String_NullOrWhitespaceName_ThrowsArgumentException(string name)
public static void Ctor_String_WhitespaceName_ThrowsArgumentException(string name)
{
AssertExtensions.Throws<ArgumentException>("name", null, () => new TableAttribute(name));
}
Expand All @@ -35,9 +41,16 @@ public static void Schema_Set_ReturnsExpected(string value)

[Theory]
[InlineData(null)]
public static void Schema_Set_NullValue_ThrowsArgumentException(string value)
{
TableAttribute attribute = new TableAttribute("Perspicacia Tick");
AssertExtensions.Throws<ArgumentNullException>("value", null, () => attribute.Schema = value);
}

[Theory]
[InlineData("")]
[InlineData(" \t\r\n")]
public static void Schema_Set_NullOrWhitespaceValue_ThrowsArgumentException(string value)
public static void Schema_Set_WhitespaceValue_ThrowsArgumentException(string value)
{
TableAttribute attribute = new TableAttribute("Perspicacia Tick");
AssertExtensions.Throws<ArgumentException>("value", null, () => attribute.Schema = value);
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Net.Http/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@
<data name="net_http_content_read_as_stream_has_task" xml:space="preserve">
<value>The content's stream has already been retrieved via async ReadAsStreamAsync and cannot be subsequently accessed synchronously.</value>
</data>
<data name="net_http_argument_empty_string" xml:space="preserve">
<value>The value cannot be null or empty.</value>
</data>
<data name="net_http_client_request_already_sent" xml:space="preserve">
<value>The request message was already sent. Cannot send the same request message multiple times.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AuthenticationHeaderValue(string scheme)

public AuthenticationHeaderValue(string scheme, string? parameter)
{
HeaderUtilities.CheckValidToken(scheme, nameof(scheme));
HeaderUtilities.CheckValidToken(scheme);
HttpHeaders.CheckContainsNewLine(parameter);
_scheme = scheme;
_parameter = parameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ object ICloneable.Clone()

private sealed class TokenObjectCollection : ObjectCollection<string>
{
public override void Validate(string item) => HeaderUtilities.CheckValidToken(item, nameof(item));
public override void Validate(string item) => HeaderUtilities.CheckValidToken(item);

public int GetHashCode(StringComparer comparer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;

namespace System.Net.Http.Headers
Expand Down Expand Up @@ -34,7 +35,7 @@ public string DispositionType
get { return _dispositionType; }
set
{
CheckDispositionTypeFormat(value, nameof(value));
CheckDispositionTypeFormat(value);
_dispositionType = value;
}
}
Expand Down Expand Up @@ -139,7 +140,7 @@ protected ContentDispositionHeaderValue(ContentDispositionHeaderValue source)

public ContentDispositionHeaderValue(string dispositionType)
{
CheckDispositionTypeFormat(dispositionType, nameof(dispositionType));
CheckDispositionTypeFormat(dispositionType);
_dispositionType = dispositionType;
}

Expand Down Expand Up @@ -270,12 +271,9 @@ private static int GetDispositionTypeExpressionLength(string input, int startInd
return typeLength;
}

private static void CheckDispositionTypeFormat(string dispositionType, string parameterName)
private static void CheckDispositionTypeFormat(string dispositionType, [CallerArgumentExpression(nameof(dispositionType))] string? parameterName = null)
{
if (string.IsNullOrEmpty(dispositionType))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(dispositionType, parameterName);

// When adding values using strongly typed objects, no leading/trailing LWS (whitespace) are allowed.
int dispositionTypeLength = GetDispositionTypeExpressionLength(dispositionType, 0, out string? tempDispositionType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public string Unit
get { return _unit; }
set
{
HeaderUtilities.CheckValidToken(value, nameof(value));
HeaderUtilities.CheckValidToken(value);
_unit = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public EntityTagHeaderValue(string tag)

public EntityTagHeaderValue(string tag, bool isWeak)
{
if (string.IsNullOrEmpty(tag))
{
throw new ArgumentException(SR.net_http_argument_empty_string, nameof(tag));
}
ArgumentException.ThrowIfNullOrWhiteSpace(tag);

int length;
if ((HttpRuleParser.GetQuotedStringLength(tag, 0, out length) != HttpParseResult.Parsed) ||
(length != tag.Length))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;

namespace System.Net.Http.Headers
Expand Down Expand Up @@ -139,25 +140,19 @@ private static void AddHexEscaped(byte c, ref ValueStringBuilder destination)
return null;
}

internal static void CheckValidToken(string value, string parameterName)
internal static void CheckValidToken(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName);

if (HttpRuleParser.GetTokenLength(value, 0) != value.Length)
{
throw new FormatException(SR.Format(CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, value));
}
}

internal static void CheckValidComment(string value, string parameterName)
internal static void CheckValidComment(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName);

int length;
if ((HttpRuleParser.GetCommentLength(value, 0, out length) != HttpParseResult.Parsed) ||
Expand All @@ -167,12 +162,9 @@ internal static void CheckValidComment(string value, string parameterName)
}
}

internal static void CheckValidQuotedString(string value, string parameterName)
internal static void CheckValidQuotedString(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName);

int length;
if ((HttpRuleParser.GetQuotedStringLength(value, 0, out length) != HttpParseResult.Parsed) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,7 @@ private static void ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreIte

private HeaderDescriptor GetHeaderDescriptor(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(SR.net_http_argument_empty_string, nameof(name));
}
ArgumentException.ThrowIfNullOrWhiteSpace(name);

if (!HeaderDescriptor.TryGet(name, out HeaderDescriptor descriptor))
{
Expand Down
Loading