Skip to content
Closed
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
2 changes: 1 addition & 1 deletion eng/CodeAnalysis.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<Rule Id="CA2200" Action="Warning" /> <!-- Rethrow to preserve stack details. -->
<Rule Id="CA2201" Action="None" /> <!-- Do not raise reserved exception types -->
<Rule Id="CA2207" Action="Warning" /> <!-- Initialize value type static fields inline -->
<Rule Id="CA2208" Action="None" /> <!-- Instantiate argument exceptions correctly -->
<Rule Id="CA2208" Action="Info" /> <!-- Instantiate argument exceptions correctly -->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have thought we wanted Warnings here (with everything fixed or suppressed by this PR)

@buyaa-n buyaa-n Apr 20, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have discussed to upgrade severity level from hidden to info, so I thought I should make it same here, but yes maybe we want to make it warning to get full benefit, we might need lots of suppress though

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked with @bartonjs we will make it warning after fixing/suppressing all warnings. For now, I will leave this PR on hold until applying his suggestions on improving analyzer:

  • maybe we only want the analyzer to report for public/protected/protected ( do not warn for internal private methods).
  • We could include the generic method parameters (and possibly the generic type parameters for constructors) as legal targets.

Then we might have much less warning need to fix/suppress, and i might update this PR with all repo fix (coreclr+libs+mono)

<Rule Id="CA2211" Action="None" /> <!-- Non-constant fields should not be visible -->
<Rule Id="CA2213" Action="None" /> <!-- Disposable fields should be disposed -->
<Rule Id="CA2214" Action="None" /> <!-- Do not call overridable methods in constructors -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static object GetClassFactoryForType(ComActivationContext cxt)

if (!Path.IsPathRooted(cxt.AssemblyPath))
{
throw new ArgumentException();
throw new ArgumentException(null, nameof(cxt));
Comment thread
AaronRobinsonMSFT marked this conversation as resolved.
}

Type classType = FindClassType(cxt.ClassId, cxt.AssemblyPath, cxt.AssemblyName, cxt.TypeName);
Expand Down Expand Up @@ -156,7 +156,7 @@ public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bo

if (!Path.IsPathRooted(cxt.AssemblyPath))
{
throw new ArgumentException();
throw new ArgumentException(null, nameof(cxt));
}

Type classType = FindClassType(cxt.ClassId, cxt.AssemblyPath, cxt.AssemblyName, cxt.TypeName);
Expand Down Expand Up @@ -288,7 +288,7 @@ public static unsafe int RegisterClassForTypeInternal(ComActivationContextIntern
if (cxtInt.InterfaceId != Guid.Empty
|| cxtInt.ClassFactoryDest != IntPtr.Zero)
{
throw new ArgumentException();
throw new ArgumentException(null, nameof(pCxtInt));
}

try
Expand Down Expand Up @@ -328,7 +328,7 @@ public static unsafe int UnregisterClassForTypeInternal(ComActivationContextInte
if (cxtInt.InterfaceId != Guid.Empty
|| cxtInt.ClassFactoryDest != IntPtr.Zero)
{
throw new ArgumentException();
throw new ArgumentException(null, nameof(pCxtInt));
}

try
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/src/System.Private.CoreLib/src/System/GC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static void AddMemoryPressure(long bytesAllocated)

if ((4 == IntPtr.Size) && (bytesAllocated > int.MaxValue))
{
throw new ArgumentOutOfRangeException("pressure",
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
SR.ArgumentOutOfRange_MustBeNonNegInt32);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public IntPtr GetOrCreateComInterfaceForObject(object instance, CreateComInterfa
{
IntPtr ptr;
if (!TryGetOrCreateComInterfaceForObjectInternal(this, instance, flags, out ptr))
throw new ArgumentException();
throw new ArgumentException(null, nameof(instance));

return ptr;
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public object GetOrCreateObjectForComInstance(IntPtr externalComObject, CreateOb
{
object? obj;
if (!TryGetOrCreateObjectForComInstanceInternal(this, externalComObject, flags, null, out obj))
throw new ArgumentNullException();
throw new ArgumentNullException(nameof(externalComObject));

return obj!;
}
Expand Down Expand Up @@ -230,7 +230,7 @@ public object GetOrRegisterObjectForComInstance(IntPtr externalComObject, Create

object? obj;
if (!TryGetOrCreateObjectForComInstanceInternal(this, externalComObject, flags, wrapper, out obj))
throw new ArgumentNullException();
throw new ArgumentNullException(nameof(externalComObject));

return obj!;
}
Expand Down Expand Up @@ -319,4 +319,4 @@ internal static int CallICustomQueryInterface(object customQueryInterfaceMaybe,
return (int)customQueryInterface.GetInterface(ref iid, out ppObject);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ private static void ThrowIfTypeNeverValidGenericArgument(RuntimeType type)
internal static void SanityCheckGenericArguments(RuntimeType[] genericArguments, RuntimeType[] genericParamters)
{
if (genericArguments == null)
throw new ArgumentNullException();
throw new ArgumentNullException(nameof(genericArguments));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

genericArguments was never supplied by the user. As such, maybe this code ought to not throw at all (just let it throw NRE). But, not suggesting you change it.


for (int i = 0; i < genericArguments.Length; i++)
{
Expand Down Expand Up @@ -2752,7 +2752,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)
protected override PropertyInfo? GetPropertyImpl(
string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers)
{
if (name == null) throw new ArgumentNullException();
if (name == null) throw new ArgumentNullException(nameof(name));

ListBuilder<PropertyInfo> candidates = GetPropertyCandidates(name, bindingAttr, types, false);

Expand Down Expand Up @@ -2788,7 +2788,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)

public override EventInfo? GetEvent(string name, BindingFlags bindingAttr)
{
if (name is null) throw new ArgumentNullException();
if (name is null) throw new ArgumentNullException(nameof(name));

FilterHelper(bindingAttr, ref name, out _, out MemberListType listType);

Expand All @@ -2814,7 +2814,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)

public override FieldInfo? GetField(string name, BindingFlags bindingAttr)
{
if (name is null) throw new ArgumentNullException();
if (name is null) throw new ArgumentNullException(nameof(name));

FilterHelper(bindingAttr, ref name, out _, out MemberListType listType);

Expand Down Expand Up @@ -2851,7 +2851,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)

public override Type? GetInterface(string fullname, bool ignoreCase)
{
if (fullname is null) throw new ArgumentNullException();
if (fullname is null) throw new ArgumentNullException(nameof(fullname));

BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic;

Expand Down Expand Up @@ -2885,7 +2885,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)

public override Type? GetNestedType(string fullname, BindingFlags bindingAttr)
{
if (fullname is null) throw new ArgumentNullException();
if (fullname is null) throw new ArgumentNullException(nameof(fullname));

bindingAttr &= ~BindingFlags.Static;
string name, ns;
Expand Down Expand Up @@ -2913,7 +2913,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)

public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
{
if (name is null) throw new ArgumentNullException();
if (name is null) throw new ArgumentNullException(nameof(name));

ListBuilder<MethodInfo> methods = default;
ListBuilder<ConstructorInfo> constructors = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ bool compressStack
}
else
{
Comment thread
buyaa-n marked this conversation as resolved.
throw new ArgumentNullException(nameof(WaitOrTimerCallback));
throw new ArgumentNullException(nameof(callBack));
}
return registeredWaitHandle;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void End(IAsyncResult asyncResult)
return;
}

throw new ArgumentNullException();
throw new ArgumentNullException(nameof(asyncResult));
}

/// <summary>Processes an IAsyncResult returned by Begin.</summary>
Expand All @@ -55,7 +55,7 @@ public static TResult End<TResult>(IAsyncResult asyncResult)
return task.GetAwaiter().GetResult();
}

throw new ArgumentNullException();
throw new ArgumentNullException(nameof(asyncResult));
}

/// <summary>Provides a simple IAsyncResult that wraps a Task.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ internal DiagnosticCounter(string name, EventSource eventSource)
{
if (name == null)
{
throw new ArgumentNullException(nameof(Name));
throw new ArgumentNullException(nameof(name));
}

if (eventSource == null)
{
throw new ArgumentNullException(nameof(EventSource));
throw new ArgumentNullException(nameof(eventSource));
}

Name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private void Write<T>(EventSource eventSource, string? eventName, ref EventSourc
if (this.state != State.Started)
throw new InvalidOperationException(); // Write after stop.
if (eventName == null)
throw new ArgumentNullException();
throw new ArgumentNullException(nameof(eventName));

eventSource.Write(eventName, ref options, ref this.activityId, ref s_empty, ref data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ private static int AddValueToMetaData(List<byte> metaData, string value)
{
if (!(i + 1 < value.Length))
{
throw new ArgumentException(SR.EventSource_EvenHexDigits, "traits");
throw new ArgumentException(SR.EventSource_EvenHexDigits, nameof(value));
}
metaData.Add((byte)(HexDigit(value[i]) * 16 + HexDigit(value[i + 1])));
i++;
Expand All @@ -826,7 +826,7 @@ private static int AddValueToMetaData(List<byte> metaData, string value)
}
else
{
throw new ArgumentException(SR.Format(SR.EventSource_IllegalValue, value), "traits");
throw new ArgumentException(SR.Format(SR.EventSource_IllegalValue, value), nameof(value));
}

return metaData.Count - startPos;
Expand All @@ -850,7 +850,7 @@ private static int HexDigit(char c)
return c - 'A' + 10;
}

throw new ArgumentException(SR.Format(SR.EventSource_BadHexDigit, c), "traits");
throw new ArgumentException(SR.Format(SR.EventSource_BadHexDigit, c), nameof(c));
}

private NameInfo? UpdateDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public virtual unsafe void Write(string value)
{
if (charStart < 0 || charCount < 0 || charStart > value.Length - charCount)
{
throw new ArgumentOutOfRangeException(nameof(charCount));
throw new ArgumentOutOfRangeException(nameof(value));
}
fixed (char* pChars = value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public unsafe byte* PositionPointer
throw new IOException(SR.IO_SeekBeforeBegin);
long newPosition = (long)value - (long)_mem;
if (newPosition < 0)
throw new ArgumentOutOfRangeException("offset", SR.ArgumentOutOfRange_UnmanagedMemStreamLength);
throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_UnmanagedMemStreamLength);

Interlocked.Exchange(ref _position, newPosition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static string ComputeDisplayName(string? name, Version? version, string?
if (pkt != null)
{
if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
throw new ArgumentException();
throw new ArgumentException(null, nameof(pkt));

sb.Append(", PublicKeyToken=");
if (pkt.Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal static unsafe int GetAnsiStringByteCount(ReadOnlySpan<char> chars)
byteLength = Interop.Kernel32.WideCharToMultiByte(
Interop.Kernel32.CP_ACP, Interop.Kernel32.WC_NO_BEST_FIT_CHARS, pChars, chars.Length, null, 0, IntPtr.Zero, IntPtr.Zero);
if (byteLength <= 0)
throw new ArgumentException();
throw new ArgumentException(null, nameof(chars));
}
}

Expand All @@ -121,7 +121,7 @@ internal static unsafe void GetAnsiStringBytes(ReadOnlySpan<char> chars, Span<by
byteLength = Interop.Kernel32.WideCharToMultiByte(
Interop.Kernel32.CP_ACP, Interop.Kernel32.WC_NO_BEST_FIT_CHARS, pChars, chars.Length, pBytes, bytes.Length, IntPtr.Zero, IntPtr.Zero);
if (byteLength <= 0)
throw new ArgumentException();
throw new ArgumentException(null, nameof(bytes));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ internal EncoderFallbackException(
}
if (!char.IsLowSurrogate(charUnknownLow))
{
throw new ArgumentOutOfRangeException(nameof(CharUnknownLow),
throw new ArgumentOutOfRangeException(nameof(charUnknownLow),
SR.Format(SR.ArgumentOutOfRange_Range, 0xDC00, 0xDFFF));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ private StringBuilder AppendCore(StringBuilder value, int startIndex, int count)

if ((uint)newLength > (uint)m_MaxCapacity)
{
throw new ArgumentOutOfRangeException(nameof(Capacity), SR.ArgumentOutOfRange_Capacity);
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_Capacity);
}

while (count > 0)
Expand Down Expand Up @@ -1144,7 +1144,9 @@ public StringBuilder Remove(int startIndex, int length)
return this;
}

#pragma warning disable CA1830 // Prefer strongly-typed Append and Insert method overloads on StringBuilder.
public StringBuilder Append(bool value) => Append(value.ToString());
#pragma warning restore CA1830 // Prefer strongly-typed Append and Insert method overloads on StringBuilder.

public StringBuilder Append(char value)
{
Expand Down Expand Up @@ -2401,7 +2403,7 @@ private void ExpandByABlock(int minBlockCharCount)

if ((minBlockCharCount + Length) > m_MaxCapacity || minBlockCharCount + Length < minBlockCharCount)
{
throw new ArgumentOutOfRangeException("requiredLength", SR.ArgumentOutOfRange_SmallCapacity);
throw new ArgumentOutOfRangeException(nameof(minBlockCharCount), SR.ArgumentOutOfRange_SmallCapacity);
}

// - We always need to make the new chunk at least as big as was requested (`minBlockCharCount`).
Expand Down Expand Up @@ -2490,7 +2492,7 @@ private void MakeRoom(int index, int count, out StringBuilder chunk, out int ind

if (count + Length > m_MaxCapacity || count + Length < count)
{
throw new ArgumentOutOfRangeException("requiredLength", SR.ArgumentOutOfRange_SmallCapacity);
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_SmallCapacity);
}

chunk = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public bool TrySetApartmentState(ApartmentState state)
break;

default:
throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_Enum, nameof(state));
throw new ArgumentOutOfRangeException(nameof(state), SR.ArgumentOutOfRange_Enum);
}

return TrySetApartmentStateUnchecked(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void GetRuntimeEvent()
});


Assert.Throws<ArgumentNullException>(null, () =>
Assert.Throws<ArgumentNullException>("name", () =>
{
typeof(RuntimeReflectionExtensionsTests).GetRuntimeEvent(null);
});
Expand Down Expand Up @@ -250,7 +250,7 @@ public void GetRuntimeField()
});


Assert.Throws<ArgumentNullException>(null, () =>
Assert.Throws<ArgumentNullException>("name", () =>
{
typeof(RuntimeReflectionExtensionsTests).GetRuntimeField(null);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Ctor_NullType_ThrowsNullReferenceException()
[Fact]
public void Ctor_NullEventName_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>(null, () => new ComAwareEventInfo(typeof(NonComObject), null));
AssertExtensions.Throws<ArgumentNullException>("name", () => new ComAwareEventInfo(typeof(NonComObject), null));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public void OffsetOf_NullType_ThrowsArgumentNullException()
[Fact]
public void OffsetOf_NullFieldName_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>(null, () => Marshal.OffsetOf(new object().GetType(), null));
AssertExtensions.Throws<ArgumentNullException>(null, () => Marshal.OffsetOf<object>(null));
AssertExtensions.Throws<ArgumentNullException>("name", () => Marshal.OffsetOf(new object().GetType(), null));
AssertExtensions.Throws<ArgumentNullException>("name", () => Marshal.OffsetOf<object>(null));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Marshal.OffsetOf were a more entry-level method, I'd advocate taking a change for it to validate the field name directly so it could throw the ANE with the correct parameter name ("fieldName"). But I'm guessing our position right now is "it's an advanced enough API that the caller is expected to realize the parameter sharing between OffsetOf's fieldName and GetField's name."

Writing this down in case someone else is iffy about it and wants to have the discussion. But currently sighing and not recommending further action.

}

[Fact]
Expand Down
Loading