Skip to content

Commit 699acfa

Browse files
authored
Enable new analyzers CA1510/11/12/13 and CA1856/57/58 (#80149)
* 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
1 parent ca5e54a commit 699acfa

139 files changed

Lines changed: 393 additions & 724 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

eng/CodeAnalysis.src.globalconfig

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,18 @@ dotnet_diagnostic.CA1508.severity = none
238238
# CA1509: Invalid entry in code metrics rule specification file
239239
dotnet_diagnostic.CA1509.severity = none
240240

241+
# CA1510: Use ArgumentNullException throw helper
242+
dotnet_diagnostic.CA1510.severity = warning
243+
244+
# CA1511: Use ArgumentException throw helper
245+
dotnet_diagnostic.CA1511.severity = warning
246+
247+
# CA1512: Use ArgumentOutOfRangeException throw helper
248+
dotnet_diagnostic.CA1512.severity = warning
249+
250+
# CA1513: Use ObjectDisposedException throw helper
251+
dotnet_diagnostic.CA1513.severity = warning
252+
241253
# CA1700: Do not name enum values 'Reserved'
242254
dotnet_diagnostic.CA1700.severity = none
243255

@@ -420,6 +432,15 @@ dotnet_diagnostic.CA1854.severity = warning
420432
# CA1855: Prefer 'Clear' over 'Fill'
421433
dotnet_diagnostic.CA1855.severity = warning
422434

435+
# CA1856: Incorrect usage of ConstantExpected attribute
436+
dotnet_diagnostic.CA1856.severity = error
437+
438+
# CA1857: A constant is expected for the parameter
439+
dotnet_diagnostic.CA1857.severity = warning
440+
441+
# CA1858: Use 'StartsWith' instead of 'IndexOf'
442+
dotnet_diagnostic.CA1858.severity = warning
443+
423444
# CA2000: Dispose objects before losing scope
424445
dotnet_diagnostic.CA2000.severity = none
425446

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

474-
# CA2109: Review visible event handlers
475-
dotnet_diagnostic.CA2109.severity = none
476-
477495
# CA2119: Seal methods that satisfy private interfaces
478496
dotnet_diagnostic.CA2119.severity = none
479497

eng/CodeAnalysis.test.globalconfig

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ dotnet_diagnostic.CA1508.severity = none
237237
# CA1509: Invalid entry in code metrics rule specification file
238238
dotnet_diagnostic.CA1509.severity = none
239239

240+
# CA1510: Use ArgumentNullException throw helper
241+
dotnet_diagnostic.CA1510.severity = none
242+
243+
# CA1511: Use ArgumentException throw helper
244+
dotnet_diagnostic.CA1511.severity = none
245+
246+
# CA1512: Use ArgumentOutOfRangeException throw helper
247+
dotnet_diagnostic.CA1512.severity = none
248+
249+
# CA1513: Use ObjectDisposedException throw helper
250+
dotnet_diagnostic.CA1513.severity = none
251+
240252
# CA1700: Do not name enum values 'Reserved'
241253
dotnet_diagnostic.CA1700.severity = none
242254

@@ -417,6 +429,15 @@ dotnet_diagnostic.CA1854.severity = none
417429
# CA1855: Prefer 'Clear' over 'Fill'
418430
dotnet_diagnostic.CA1855.severity = none
419431

432+
# CA1856: Incorrect usage of ConstantExpected attribute
433+
dotnet_diagnostic.CA1856.severity = none
434+
435+
# CA1857: A constant is expected for the parameter
436+
dotnet_diagnostic.CA1857.severity = none
437+
438+
# CA1858: Use 'StartsWith' instead of 'IndexOf'
439+
dotnet_diagnostic.CA1858.severity = none
440+
420441
# CA2000: Dispose objects before losing scope
421442
dotnet_diagnostic.CA2000.severity = none
422443

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

471-
# CA2109: Review visible event handlers
472-
dotnet_diagnostic.CA2109.severity = none
473-
474492
# CA2119: Seal methods that satisfy private interfaces
475493
dotnet_diagnostic.CA2119.severity = none
476494

src/coreclr/nativeaot/Common/src/System/Collections/Generic/LowLevelList.cs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public LowLevelList()
6262
//
6363
public LowLevelList(int capacity)
6464
{
65-
if (capacity < 0) throw new ArgumentOutOfRangeException(nameof(capacity));
65+
ArgumentOutOfRangeException.ThrowIfNegative(capacity);
6666

6767
if (capacity == 0)
6868
_items = s_emptyArray;
@@ -76,8 +76,7 @@ public LowLevelList(int capacity)
7676
//
7777
public LowLevelList(IEnumerable<T> collection)
7878
{
79-
if (collection == null)
80-
throw new ArgumentNullException(nameof(collection));
79+
ArgumentNullException.ThrowIfNull(collection);
8180

8281
ICollection<T>? c = collection as ICollection<T>;
8382
if (c != null)
@@ -123,10 +122,7 @@ public int Capacity
123122
}
124123
set
125124
{
126-
if (value < _size)
127-
{
128-
throw new ArgumentOutOfRangeException(nameof(value));
129-
}
125+
ArgumentOutOfRangeException.ThrowIfLessThan(value, _size);
130126

131127
if (value != _items.Length)
132128
{
@@ -160,19 +156,13 @@ public T this[int index]
160156
get
161157
{
162158
// Following trick can reduce the range check by one
163-
if ((uint)index >= (uint)_size)
164-
{
165-
throw new ArgumentOutOfRangeException();
166-
}
159+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)_size, nameof(index));
167160
return _items[index];
168161
}
169162

170163
set
171164
{
172-
if ((uint)index >= (uint)_size)
173-
{
174-
throw new ArgumentOutOfRangeException();
175-
}
165+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)_size, nameof(index));
176166
_items[index] = value;
177167
_version++;
178168
}
@@ -298,8 +288,7 @@ public int IndexOf(T item)
298288
//
299289
public int IndexOf(T item, int index)
300290
{
301-
if (index > _size)
302-
throw new ArgumentOutOfRangeException(nameof(index));
291+
ArgumentOutOfRangeException.ThrowIfGreaterThan(index, _size);
303292
return Array.IndexOf(_items, item, index, _size - index);
304293
}
305294

@@ -310,10 +299,8 @@ public int IndexOf(T item, int index)
310299
public void Insert(int index, T item)
311300
{
312301
// Note that insertions at the end are legal.
313-
if ((uint)index > (uint)_size)
314-
{
315-
throw new ArgumentOutOfRangeException(nameof(index));
316-
}
302+
ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)index, (uint)_size, nameof(index));
303+
317304
if (_size == _items.Length) EnsureCapacity(_size + 1);
318305
if (index < _size)
319306
{
@@ -331,15 +318,8 @@ public void Insert(int index, T item)
331318
//
332319
public void InsertRange(int index, IEnumerable<T> collection)
333320
{
334-
if (collection == null)
335-
{
336-
throw new ArgumentNullException(nameof(collection));
337-
}
338-
339-
if ((uint)index > (uint)_size)
340-
{
341-
throw new ArgumentOutOfRangeException(nameof(index));
342-
}
321+
ArgumentNullException.ThrowIfNull(collection);
322+
ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)index, (uint)_size, nameof(index));
343323

344324
ICollection<T>? c = collection as ICollection<T>;
345325
if (c != null)
@@ -402,10 +382,7 @@ public bool Remove(T item)
402382
// The complexity is O(n).
403383
public int RemoveAll(Predicate<T> match)
404384
{
405-
if (match == null)
406-
{
407-
throw new ArgumentNullException(nameof(match));
408-
}
385+
ArgumentNullException.ThrowIfNull(match);
409386

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

@@ -438,10 +415,7 @@ public int RemoveAll(Predicate<T> match)
438415
//
439416
public void RemoveAt(int index)
440417
{
441-
if ((uint)index >= (uint)_size)
442-
{
443-
throw new ArgumentOutOfRangeException(nameof(index));
444-
}
418+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)_size, nameof(index));
445419
_size--;
446420
if (index < _size)
447421
{

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ internal ExecutionDomain(ReflectionDomainSetup executionDomainSetup, ExecutionEn
4141
//
4242
public Type GetType(string typeName, Func<AssemblyName, Assembly> assemblyResolver, Func<Assembly, string, bool, Type> typeResolver, bool throwOnError, bool ignoreCase, IList<string> defaultAssemblyNames)
4343
{
44-
if (typeName == null)
45-
throw new ArgumentNullException(nameof(typeName));
44+
ArgumentNullException.ThrowIfNull(typeName);
4645

4746
if (typeName.Length == 0)
4847
{

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Extensions/NonPortable/CustomAttributeInheritanceRules.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public static IEnumerable<CustomAttributeData> GetMatchingCustomAttributes(this
7272
return EventCustomAttributeSearcher.Default.GetMatchingCustomAttributes(eventInfo, optionalAttributeTypeFilter, inherit, skipTypeValidation: skipTypeValidation);
7373
}
7474

75-
if (element == null)
76-
throw new ArgumentNullException();
75+
ArgumentNullException.ThrowIfNull(element);
7776

7877
throw new NotSupportedException(); // Shouldn't get here.
7978
}

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Extensions/NonPortable/CustomAttributeSearcher.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ public IEnumerable<CustomAttributeData> GetMatchingCustomAttributes(E element, T
3333
{
3434
// Do all parameter validation here before we enter the iterator function (so that exceptions from validations
3535
// show up immediately rather than on the first MoveNext()).
36-
if (element == null)
37-
throw new ArgumentNullException(nameof(element));
36+
ArgumentNullException.ThrowIfNull(element);
3837

3938
bool typeFilterKnownToBeSealed = false;
4039
if (!skipTypeValidation)
4140
{
42-
if (optionalAttributeTypeFilter == null)
43-
throw new ArgumentNullException("type");
41+
ArgumentNullException.ThrowIfNull(optionalAttributeTypeFilter, "type");
4442
if (!(optionalAttributeTypeFilter == typeof(Attribute) ||
4543
optionalAttributeTypeFilter.IsSubclassOf(typeof(Attribute))))
4644
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);

src/coreclr/nativeaot/System.Private.CoreLib/src/System/ActivatorImplementation.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public static object CreateInstance(
1919
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
2020
Type type, bool nonPublic)
2121
{
22-
if (type == null)
23-
throw new ArgumentNullException(nameof(type));
22+
ArgumentNullException.ThrowIfNull(type);
2423

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

5250
// If they didn't specify a lookup, then we will provide the default lookup.
5351
const BindingFlags LookupMask = (BindingFlags)0x000000FF;

src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ public static partial class GC
6565
{
6666
public static int GetGeneration(object obj)
6767
{
68-
if (obj == null)
69-
{
70-
throw new ArgumentNullException(nameof(obj));
71-
}
68+
ArgumentNullException.ThrowIfNull(obj);
7269

7370
return RuntimeImports.RhGetGeneration(obj);
7471
}
@@ -389,18 +386,14 @@ public static void WaitForPendingFinalizers()
389386

390387
public static void SuppressFinalize(object obj)
391388
{
392-
if (obj == null)
393-
{
394-
throw new ArgumentNullException(nameof(obj));
395-
}
389+
ArgumentNullException.ThrowIfNull(obj);
396390

397391
RuntimeImports.RhSuppressFinalize(obj);
398392
}
399393

400394
public static void ReRegisterForFinalize(object obj)
401395
{
402-
if (obj == null)
403-
throw new ArgumentNullException(nameof(obj));
396+
ArgumentNullException.ThrowIfNull(obj);
404397

405398
RuntimeImports.RhReRegisterForFinalize(obj);
406399
}

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public static Assembly GetCallingAssembly()
2929

3030
public static Assembly Load(string assemblyString)
3131
{
32-
if (assemblyString == null)
33-
throw new ArgumentNullException(nameof(assemblyString));
32+
ArgumentNullException.ThrowIfNull(assemblyString);
3433

3534
AssemblyName name = new AssemblyName(assemblyString);
3635
return Load(name);
@@ -39,8 +38,7 @@ public static Assembly Load(string assemblyString)
3938
[Obsolete("Assembly.LoadWithPartialName has been deprecated. Use Assembly.Load() instead.")]
4039
public static Assembly LoadWithPartialName(string partialName)
4140
{
42-
if (partialName == null)
43-
throw new ArgumentNullException(nameof(partialName));
41+
ArgumentNullException.ThrowIfNull(partialName);
4442

4543
if ((partialName.Length == 0) || (partialName[0] == '\0'))
4644
throw new ArgumentException(SR.Format_StringZeroLength, nameof(partialName));

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ public static class AssemblyExtensions
1818
[CLSCompliant(false)] // out byte* blob
1919
public static unsafe bool TryGetRawMetadata(this Assembly assembly, out byte* blob, out int length)
2020
{
21-
if (assembly == null)
22-
{
23-
throw new ArgumentNullException(nameof(assembly));
24-
}
21+
ArgumentNullException.ThrowIfNull(assembly);
2522

2623
blob = null;
2724
length = 0;

0 commit comments

Comments
 (0)