diff --git a/src/System.Linq.Dynamic.Core/Validation/Check.cs b/src/System.Linq.Dynamic.Core/Validation/Check.cs index 8ab1462b..b58e16e8 100644 --- a/src/System.Linq.Dynamic.Core/Validation/Check.cs +++ b/src/System.Linq.Dynamic.Core/Validation/Check.cs @@ -4,7 +4,7 @@ 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 @@ -12,7 +12,7 @@ namespace System.Linq.Dynamic.Core.Validation [DebuggerStepThrough] internal static class Check { - public static T Condition([NoEnumeration] T value, [NotNull] Predicate condition, [InvokerParameterName] [NotNull] string parameterName) + public static T Condition([ValidatedNotNull, NoEnumeration] T value, [ValidatedNotNull, NotNull] Predicate condition, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) { NotNull(condition, nameof(condition)); @@ -27,7 +27,7 @@ public static T Condition([NoEnumeration] T value, [NotNull] Predicate con } [ContractAnnotation("value:null => halt")] - public static T NotNull([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName) + public static T NotNull([ValidatedNotNull, NoEnumeration] T value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) { if (ReferenceEquals(value, null)) { @@ -42,8 +42,8 @@ public static T NotNull([NoEnumeration] T value, [InvokerParameterName] [NotN [ContractAnnotation("value:null => halt")] public static T NotNull( [NoEnumeration] T value, - [InvokerParameterName] [NotNull] string parameterName, - [NotNull] string propertyName) + [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName, + [ValidatedNotNull, NotNull] string propertyName) { if (ReferenceEquals(value, null)) { @@ -56,23 +56,23 @@ public static T NotNull( return value; } - [ContractAnnotation("value:null => halt")] - public static IList NotEmpty(IList value, [InvokerParameterName] [NotNull] string parameterName) - { - NotNull(value, parameterName); + //[ContractAnnotation("value:null => halt")] + //public static IList NotEmpty(IList 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)) @@ -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 HasNoNulls(IList value, [InvokerParameterName] [NotNull] string parameterName) + public static IList HasNoNulls(IList value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName) where T : class { NotNull(value, parameterName); @@ -122,16 +121,16 @@ public static IList HasNoNulls(IList 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; + //} } } diff --git a/src/System.Linq.Dynamic.Core/Validation/ValidatedNotNullAttribute.cs b/src/System.Linq.Dynamic.Core/Validation/ValidatedNotNullAttribute.cs new file mode 100644 index 00000000..ef7e34a5 --- /dev/null +++ b/src/System.Linq.Dynamic.Core/Validation/ValidatedNotNullAttribute.cs @@ -0,0 +1,9 @@ +namespace System.Linq.Dynamic.Core.Validation +{ + /// + /// To fix 'xxx' is null on at least one execution path. See also https://rules.sonarsource.com/csharp/RSPEC-3900. + /// + internal class ValidatedNotNullAttribute : Attribute + { + } +}