Skip to content

Commit

Permalink
Add ValidatedNotNullAttribute (for SonarQube) (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Apr 10, 2020
1 parent f46c61e commit f6adade
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
73 changes: 36 additions & 37 deletions src/System.Linq.Dynamic.Core/Validation/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
using System.Collections.Generic;
using System.Diagnostics;
using JetBrains.Annotations;
using System.Reflection;
// using System.Reflection;

// Copied from https://github.com/aspnet/EntityFramework/blob/dev/src/Shared/Check.cs
namespace System.Linq.Dynamic.Core.Validation
{
[DebuggerStepThrough]
internal static class Check
{
public static T Condition<T>([NoEnumeration] T value, [NotNull] Predicate<T> condition, [InvokerParameterName] [NotNull] string parameterName)
public static T Condition<T>([ValidatedNotNull, NoEnumeration] T value, [ValidatedNotNull, NotNull] Predicate<T> condition, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
{
NotNull(condition, nameof(condition));

Expand All @@ -27,7 +27,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>([ValidatedNotNull, NoEnumeration] T value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
{
if (ReferenceEquals(value, null))
{
Expand All @@ -42,8 +42,8 @@ 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)
[InvokerParameterName, ValidatedNotNull, NotNull] string parameterName,
[ValidatedNotNull, NotNull] string propertyName)
{
if (ReferenceEquals(value, null))
{
Expand All @@ -56,23 +56,23 @@ public static T NotNull<T>(
return value;
}

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

if (value.Count == 0)
{
NotEmpty(parameterName, nameof(parameterName));
// if (value.Count == 0)
// {
// NotEmpty(parameterName, nameof(parameterName));

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

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

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

public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName)
{
if (!ReferenceEquals(value, null)
&& (value.Length == 0))
{
NotEmpty(parameterName, nameof(parameterName));
//public static string NullButNotEmpty(string value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
//{
// if (!ReferenceEquals(value, null) && value.Length == 0)
// {
// NotEmpty(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 @@ -122,16 +121,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)
{
NotEmpty(parameterName, nameof(parameterName));
//public static Type ValidEntityType(Type value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
//{
// if (!value.GetTypeInfo().IsClass)
// {
// NotEmpty(parameterName, nameof(parameterName));

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

return value;
}
// return value;
//}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace System.Linq.Dynamic.Core.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
{
}
}

0 comments on commit f6adade

Please sign in to comment.