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

Add ValidatedNotNullAttribute (for SonarQube) #452

Merged
merged 2 commits into from
Apr 10, 2020
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
4 changes: 1 addition & 3 deletions src/WireMock.Net/Util/BodyParser.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
Expand Down
50 changes: 25 additions & 25 deletions src/WireMock.Net/Validation/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace WireMock.Validation
internal static class Check
{
[ContractAnnotation("value:null => halt")]
public static T Condition<T>([NoEnumeration] T value, [NotNull] Predicate<T> condition, [InvokerParameterName] [NotNull] string parameterName)
public static T Condition<T>([NoEnumeration] T value, [ValidatedNotNull, NotNull] Predicate<T> condition, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
{
NotNull(condition, nameof(condition));
NotNull(value, nameof(value));
Expand All @@ -32,7 +32,7 @@ public static T Condition<T>([NoEnumeration] T value, [NotNull] Predicate<T> con
}

[ContractAnnotation("value:null => halt")]
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName)
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
{
if (ReferenceEquals(value, null))
{
Expand All @@ -45,7 +45,7 @@ public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotN
}

[ContractAnnotation("value:null => halt")]
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName, [NotNull] string propertyName)
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName, [ValidatedNotNull, NotNull] string propertyName)
{
if (ReferenceEquals(value, null))
{
Expand All @@ -59,7 +59,7 @@ public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotN
}

[ContractAnnotation("value:null => halt")]
public static IList<T> NotNullOrEmpty<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName)
public static IList<T> NotNullOrEmpty<T>(IList<T> value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
{
NotNull(value, parameterName);

Expand All @@ -74,7 +74,7 @@ public static IList<T> NotNullOrEmpty<T>(IList<T> value, [InvokerParameterName]
}

[ContractAnnotation("value:null => halt")]
public static string NotNullOrEmpty(string value, [InvokerParameterName] [NotNull] string parameterName)
public static string NotNullOrEmpty(string value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
{
Exception e = null;
if (ReferenceEquals(value, null))
Expand All @@ -96,20 +96,20 @@ public static string NotNullOrEmpty(string value, [InvokerParameterName] [NotNul
return value;
}

public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName)
{
if (!ReferenceEquals(value, null)
&& (value.Length == 0))
{
NotNullOrEmpty(parameterName, nameof(parameterName));
//public static string NullButNotEmpty(string value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
//{
// if (!ReferenceEquals(value, null)
// && (value.Length == 0))
// {
// NotNullOrEmpty(parameterName, nameof(parameterName));

throw new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName));
}
// throw new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName));
// }

return value;
}
// return value;
//}

public static IList<T> HasNoNulls<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName)
public static IList<T> HasNoNulls<T>(IList<T> value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
where T : class
{
NotNull(value, parameterName);
Expand All @@ -124,16 +124,16 @@ public static IList<T> HasNoNulls<T>(IList<T> value, [InvokerParameterName] [Not
return value;
}

public static Type ValidEntityType(Type value, [InvokerParameterName] [NotNull] string parameterName)
{
if (!value.GetTypeInfo().IsClass)
{
NotNullOrEmpty(parameterName, nameof(parameterName));
//public static Type ValidEntityType(Type value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
//{
// if (!value.GetTypeInfo().IsClass)
// {
// NotNullOrEmpty(parameterName, nameof(parameterName));

throw new ArgumentException(CoreStrings.InvalidEntityType(value, parameterName));
}
// throw new ArgumentException(CoreStrings.InvalidEntityType(value, parameterName));
// }

return value;
}
// return value;
//}
}
}
11 changes: 11 additions & 0 deletions src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace WireMock.Validation
{
/// <summary>
/// To fix 'xxx' is null on at least one execution path. See also https://rules.sonarsource.com/csharp/RSPEC-3900.
/// </summary>
internal class ValidatedNotNullAttribute : Attribute
{
}
}