Skip to content

Commit

Permalink
Enable new analyzers CA1510/11/12/13 and CA1856/57/58 (dotnet#80149)
Browse files Browse the repository at this point in the history
* Enable new analyzers CA1510/11/12/13 and CA1856/57/58

CA1510: Use ArgumentNullException throw helper
CA1511: Use ArgumentException throw helper
CA1512: Use ArgumentOutOfRangeException throw helper
CA1513: Use ObjectDisposedException throw helper
CA1856: Incorrect usage of ConstantExpected attribute
CA1857: A constant is expected for the parameter
CA1858: Use 'StartsWith' instead of 'IndexOf'

* More fixes

* Address PR feedback
  • Loading branch information
stephentoub authored Jan 7, 2023
1 parent ca5e54a commit 699acfa
Show file tree
Hide file tree
Showing 139 changed files with 393 additions and 724 deletions.
24 changes: 21 additions & 3 deletions eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ dotnet_diagnostic.CA1508.severity = none
# CA1509: Invalid entry in code metrics rule specification file
dotnet_diagnostic.CA1509.severity = none

# CA1510: Use ArgumentNullException throw helper
dotnet_diagnostic.CA1510.severity = warning

# CA1511: Use ArgumentException throw helper
dotnet_diagnostic.CA1511.severity = warning

# CA1512: Use ArgumentOutOfRangeException throw helper
dotnet_diagnostic.CA1512.severity = warning

# CA1513: Use ObjectDisposedException throw helper
dotnet_diagnostic.CA1513.severity = warning

# CA1700: Do not name enum values 'Reserved'
dotnet_diagnostic.CA1700.severity = none

Expand Down Expand Up @@ -420,6 +432,15 @@ dotnet_diagnostic.CA1854.severity = warning
# CA1855: Prefer 'Clear' over 'Fill'
dotnet_diagnostic.CA1855.severity = warning

# CA1856: Incorrect usage of ConstantExpected attribute
dotnet_diagnostic.CA1856.severity = error

# CA1857: A constant is expected for the parameter
dotnet_diagnostic.CA1857.severity = warning

# CA1858: Use 'StartsWith' instead of 'IndexOf'
dotnet_diagnostic.CA1858.severity = warning

# CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = none

Expand Down Expand Up @@ -471,9 +492,6 @@ dotnet_diagnostic.CA2100.severity = none
# CA2101: Specify marshaling for P/Invoke string arguments
dotnet_diagnostic.CA2101.severity = none

# CA2109: Review visible event handlers
dotnet_diagnostic.CA2109.severity = none

# CA2119: Seal methods that satisfy private interfaces
dotnet_diagnostic.CA2119.severity = none

Expand Down
24 changes: 21 additions & 3 deletions eng/CodeAnalysis.test.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ dotnet_diagnostic.CA1508.severity = none
# CA1509: Invalid entry in code metrics rule specification file
dotnet_diagnostic.CA1509.severity = none

# CA1510: Use ArgumentNullException throw helper
dotnet_diagnostic.CA1510.severity = none

# CA1511: Use ArgumentException throw helper
dotnet_diagnostic.CA1511.severity = none

# CA1512: Use ArgumentOutOfRangeException throw helper
dotnet_diagnostic.CA1512.severity = none

# CA1513: Use ObjectDisposedException throw helper
dotnet_diagnostic.CA1513.severity = none

# CA1700: Do not name enum values 'Reserved'
dotnet_diagnostic.CA1700.severity = none

Expand Down Expand Up @@ -417,6 +429,15 @@ dotnet_diagnostic.CA1854.severity = none
# CA1855: Prefer 'Clear' over 'Fill'
dotnet_diagnostic.CA1855.severity = none

# CA1856: Incorrect usage of ConstantExpected attribute
dotnet_diagnostic.CA1856.severity = none

# CA1857: A constant is expected for the parameter
dotnet_diagnostic.CA1857.severity = none

# CA1858: Use 'StartsWith' instead of 'IndexOf'
dotnet_diagnostic.CA1858.severity = none

# CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = none

Expand Down Expand Up @@ -468,9 +489,6 @@ dotnet_diagnostic.CA2100.severity = none
# CA2101: Specify marshaling for P/Invoke string arguments
dotnet_diagnostic.CA2101.severity = none

# CA2109: Review visible event handlers
dotnet_diagnostic.CA2109.severity = none

# CA2119: Seal methods that satisfy private interfaces
dotnet_diagnostic.CA2119.severity = none

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public LowLevelList()
//
public LowLevelList(int capacity)
{
if (capacity < 0) throw new ArgumentOutOfRangeException(nameof(capacity));
ArgumentOutOfRangeException.ThrowIfNegative(capacity);

if (capacity == 0)
_items = s_emptyArray;
Expand All @@ -76,8 +76,7 @@ public LowLevelList(int capacity)
//
public LowLevelList(IEnumerable<T> collection)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
ArgumentNullException.ThrowIfNull(collection);

ICollection<T>? c = collection as ICollection<T>;
if (c != null)
Expand Down Expand Up @@ -123,10 +122,7 @@ public int Capacity
}
set
{
if (value < _size)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
ArgumentOutOfRangeException.ThrowIfLessThan(value, _size);

if (value != _items.Length)
{
Expand Down Expand Up @@ -160,19 +156,13 @@ public T this[int index]
get
{
// Following trick can reduce the range check by one
if ((uint)index >= (uint)_size)
{
throw new ArgumentOutOfRangeException();
}
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)_size, nameof(index));
return _items[index];
}

set
{
if ((uint)index >= (uint)_size)
{
throw new ArgumentOutOfRangeException();
}
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)_size, nameof(index));
_items[index] = value;
_version++;
}
Expand Down Expand Up @@ -298,8 +288,7 @@ public int IndexOf(T item)
//
public int IndexOf(T item, int index)
{
if (index > _size)
throw new ArgumentOutOfRangeException(nameof(index));
ArgumentOutOfRangeException.ThrowIfGreaterThan(index, _size);
return Array.IndexOf(_items, item, index, _size - index);
}

Expand All @@ -310,10 +299,8 @@ public int IndexOf(T item, int index)
public void Insert(int index, T item)
{
// Note that insertions at the end are legal.
if ((uint)index > (uint)_size)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)index, (uint)_size, nameof(index));

if (_size == _items.Length) EnsureCapacity(_size + 1);
if (index < _size)
{
Expand All @@ -331,15 +318,8 @@ public void Insert(int index, T item)
//
public void InsertRange(int index, IEnumerable<T> collection)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}

if ((uint)index > (uint)_size)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
ArgumentNullException.ThrowIfNull(collection);
ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)index, (uint)_size, nameof(index));

ICollection<T>? c = collection as ICollection<T>;
if (c != null)
Expand Down Expand Up @@ -402,10 +382,7 @@ public bool Remove(T item)
// The complexity is O(n).
public int RemoveAll(Predicate<T> match)
{
if (match == null)
{
throw new ArgumentNullException(nameof(match));
}
ArgumentNullException.ThrowIfNull(match);

int freeIndex = 0; // the first free slot in items array

Expand Down Expand Up @@ -438,10 +415,7 @@ public int RemoveAll(Predicate<T> match)
//
public void RemoveAt(int index)
{
if ((uint)index >= (uint)_size)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)_size, nameof(index));
_size--;
if (index < _size)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ internal ExecutionDomain(ReflectionDomainSetup executionDomainSetup, ExecutionEn
//
public Type GetType(string typeName, Func<AssemblyName, Assembly> assemblyResolver, Func<Assembly, string, bool, Type> typeResolver, bool throwOnError, bool ignoreCase, IList<string> defaultAssemblyNames)
{
if (typeName == null)
throw new ArgumentNullException(nameof(typeName));
ArgumentNullException.ThrowIfNull(typeName);

if (typeName.Length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public static IEnumerable<CustomAttributeData> GetMatchingCustomAttributes(this
return EventCustomAttributeSearcher.Default.GetMatchingCustomAttributes(eventInfo, optionalAttributeTypeFilter, inherit, skipTypeValidation: skipTypeValidation);
}

if (element == null)
throw new ArgumentNullException();
ArgumentNullException.ThrowIfNull(element);

throw new NotSupportedException(); // Shouldn't get here.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ public IEnumerable<CustomAttributeData> GetMatchingCustomAttributes(E element, T
{
// Do all parameter validation here before we enter the iterator function (so that exceptions from validations
// show up immediately rather than on the first MoveNext()).
if (element == null)
throw new ArgumentNullException(nameof(element));
ArgumentNullException.ThrowIfNull(element);

bool typeFilterKnownToBeSealed = false;
if (!skipTypeValidation)
{
if (optionalAttributeTypeFilter == null)
throw new ArgumentNullException("type");
ArgumentNullException.ThrowIfNull(optionalAttributeTypeFilter, "type");
if (!(optionalAttributeTypeFilter == typeof(Attribute) ||
optionalAttributeTypeFilter.IsSubclassOf(typeof(Attribute))))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static object CreateInstance(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type type, bool nonPublic)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
ArgumentNullException.ThrowIfNull(type);

type = type.UnderlyingSystemType;
CreateInstanceCheckType(type);
Expand All @@ -46,8 +45,7 @@ public static object CreateInstance(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type type, BindingFlags bindingAttr, Binder binder, object?[]? args, CultureInfo? culture, object?[]? activationAttributes)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
ArgumentNullException.ThrowIfNull(type);

// If they didn't specify a lookup, then we will provide the default lookup.
const BindingFlags LookupMask = (BindingFlags)0x000000FF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public static partial class GC
{
public static int GetGeneration(object obj)
{
if (obj == null)
{
throw new ArgumentNullException(nameof(obj));
}
ArgumentNullException.ThrowIfNull(obj);

return RuntimeImports.RhGetGeneration(obj);
}
Expand Down Expand Up @@ -389,18 +386,14 @@ public static void WaitForPendingFinalizers()

public static void SuppressFinalize(object obj)
{
if (obj == null)
{
throw new ArgumentNullException(nameof(obj));
}
ArgumentNullException.ThrowIfNull(obj);

RuntimeImports.RhSuppressFinalize(obj);
}

public static void ReRegisterForFinalize(object obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
ArgumentNullException.ThrowIfNull(obj);

RuntimeImports.RhReRegisterForFinalize(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public static Assembly GetCallingAssembly()

public static Assembly Load(string assemblyString)
{
if (assemblyString == null)
throw new ArgumentNullException(nameof(assemblyString));
ArgumentNullException.ThrowIfNull(assemblyString);

AssemblyName name = new AssemblyName(assemblyString);
return Load(name);
Expand All @@ -39,8 +38,7 @@ public static Assembly Load(string assemblyString)
[Obsolete("Assembly.LoadWithPartialName has been deprecated. Use Assembly.Load() instead.")]
public static Assembly LoadWithPartialName(string partialName)
{
if (partialName == null)
throw new ArgumentNullException(nameof(partialName));
ArgumentNullException.ThrowIfNull(partialName);

if ((partialName.Length == 0) || (partialName[0] == '\0'))
throw new ArgumentException(SR.Format_StringZeroLength, nameof(partialName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public static class AssemblyExtensions
[CLSCompliant(false)] // out byte* blob
public static unsafe bool TryGetRawMetadata(this Assembly assembly, out byte* blob, out int length)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}
ArgumentNullException.ThrowIfNull(assembly);

blob = null;
length = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public sealed override Module GetModule(string name)
[RequiresUnreferencedCode("Types might be removed")]
public sealed override Type GetType(string name, bool throwOnError, bool ignoreCase)
{
if (name == null)
throw new ArgumentNullException();
if (name.Length == 0)
throw new ArgumentException();
ArgumentException.ThrowIfNullOrEmpty(name);

TypeName typeName = TypeParser.ParseAssemblyQualifiedTypeName(name, throwOnError: throwOnError);
if (typeName == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public sealed override IEnumerable<CustomAttributeData> CustomAttributes

public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

if (!(other is NativeFormatRuntimeEventInfo otherEvent))
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public sealed override string ToString()

public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

if (!(other is NativeFormatRuntimeFieldInfo otherField))
return false;
Expand Down
Loading

0 comments on commit 699acfa

Please sign in to comment.