Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable CA1822 (Mark members as static) analyzer #66333

Merged
merged 5 commits into from
Mar 25, 2022
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 3 additions & 2 deletions eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ dotnet_diagnostic.CA1820.severity = none
dotnet_diagnostic.CA1821.severity = warning

# CA1822: Mark members as static
dotnet_diagnostic.CA1822.severity = none
dotnet_diagnostic.CA1822.severity = warning
dotnet_code_quality.CA1822.api_surface = private, internal

# CA1823: Avoid unused private fields
dotnet_diagnostic.CA1823.severity = warning
Expand Down Expand Up @@ -825,7 +826,7 @@ dotnet_diagnostic.CA5400.severity = none
# CA5401: Do not use CreateEncryptor with non-default IV
dotnet_diagnostic.CA5401.severity = none

# CA5402: Use CreateEncryptor with the default IV
# CA5402: Use CreateEncryptor with the default IV
dotnet_diagnostic.CA5402.severity = none

# CA5403: Do not hard-code certificate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ private static int ComputeBucket(int hashCode, int numBuckets)
[Conditional("DEBUG")]
public void VerifyUnifierConsistency()
{
#if DEBUG
// There's a point at which this check becomes gluttonous, even by checked build standards...
if (_nextFreeEntry >= 5000 && (0 != (_nextFreeEntry % 100)))
return;
Expand Down Expand Up @@ -287,7 +286,6 @@ public void VerifyUnifierConsistency()
// The assertion is "<=" rather than "==" because we allow an entry to "leak" until the next resize if
// a thread died between the time between we allocated the entry and the time we link it into the bucket stack.
Debug.Assert(numEntriesEncountered <= _nextFreeEntry);
#endif //DEBUG
}

private readonly int[] _buckets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ private static int ComputeBucket(int hashCode, int numBuckets)
[Conditional("DEBUG")]
public void VerifyUnifierConsistency()
{
#if DEBUG
// There's a point at which this check becomes gluttonous, even by checked build standards...
if (_nextFreeEntry >= 5000 || (0 != (_nextFreeEntry % 100)))
return;
Expand Down Expand Up @@ -372,7 +371,6 @@ public void VerifyUnifierConsistency()
// The assertion is "<=" rather than "==" because we allow an entry to "leak" until the next resize if
// a thread died between the time between we allocated the entry and the time we link it into the bucket stack.
Debug.Assert(numEntriesEncountered <= _nextFreeEntry);
#endif //DEBUG
}

private readonly int[] _buckets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ private static int ComputeBucket(int hashCode, int numBuckets)
[Conditional("DEBUG")]
public void VerifyUnifierConsistency()
{
#if DEBUG
// There's a point at which this check becomes gluttonous, even by checked build standards...
if (_nextFreeEntry >= 5000 && (0 != (_nextFreeEntry % 100)))
return;
Expand Down Expand Up @@ -367,7 +366,6 @@ public void VerifyUnifierConsistency()
// The assertion is "<=" rather than "==" because we allow an entry to "leak" until the next resize if
// a thread died between the time between we allocated the entry and the time we link it into the bucket stack.
Debug.Assert(numEntriesEncountered <= _nextFreeEntry);
#endif //DEBUG
}

private readonly int[] _buckets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public AttributeUsageAttribute(AttributeTargets validOn, bool allowMultiple, boo
// Allowing the set properties as it allows a more readable syntax in the specifiers (and are commonly used)
// The get properties will be needed only if these attributes are used at Runtime, however, the compiler
// is getting an internal error if the gets are not defined.

#pragma warning disable CA1822
marek-safar marked this conversation as resolved.
Show resolved Hide resolved
public bool AllowMultiple
{
get { return false; }
Expand All @@ -41,5 +41,6 @@ public bool Inherited
get { return false; }
set { }
}
#pragma warning restore CA1822
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Internal.Runtime
// Extensions to MethodTable that are specific to the use in Runtime.Base.
internal unsafe partial struct MethodTable
{
#pragma warning disable CA1822
internal MethodTable* GetArrayEEType()
{
#if INPLACE_RUNTIME
Expand Down Expand Up @@ -46,6 +47,7 @@ internal Exception GetClasslibException(ExceptionIDs id)
return EH.GetClasslibExceptionFromEEType(id, GetAssociatedModuleAddress());
#endif
}
#pragma warning restore CA1822

internal IntPtr GetClasslibFunction(ClassLibFunctionId id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private bool IsThunkInHeap(IntPtr thunkAddress)
return false;
}

private IntPtr TryGetThunkDataAddress(IntPtr thunkAddress)
private static IntPtr TryGetThunkDataAddress(IntPtr thunkAddress)
{
nuint thunkAddressValue = (nuint)(nint)ClearThumbBit(thunkAddress);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private IEnumerable<CustomAttributeData> GetMatchingCustomAttributesIterator(E e
//
// Internal helper to compute the AttributeUsage. This must be coded specially to avoid an infinite recursion.
//
private AttributeUsageAttribute GetAttributeUsage(Type attributeType)
private static AttributeUsageAttribute GetAttributeUsage(Type attributeType)
{
// This is only invoked when the seacher is called with "inherit: true", thus calling the searcher again
// with "inherit: false" will not cause infinite recursion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,14 +1146,16 @@ public bool MoveNext()
return false;
}

public void Dispose()
public object Clone()
{
return MemberwiseClone();
}

public object Clone()
#pragma warning disable CA1822 // https://github.com/dotnet/roslyn-analyzers/issues/5911
public void Dispose()
{
return MemberwiseClone();
}
#pragma warning restore CA1822
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public MethodBase? TargetSite
}
}

private IDictionary CreateDataContainer() => new ListDictionaryInternal();
private static IDictionary CreateDataContainer() => new ListDictionaryInternal();

private string? SerializationWatsonBuckets => null;
private static string? SerializationWatsonBuckets => null;

private string? CreateSourceName() => HasBeenThrown ? "<unknown>" : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private MulticastDelegate NewMulticastDelegate(Delegate[] invocationList, int in
return result;
}

private bool TrySetSlot(Delegate[] a, int index, Delegate o)
private static bool TrySetSlot(Delegate[] a, int index, Delegate o)
{
if (a[index] == null && System.Threading.Interlocked.CompareExchange<Delegate>(ref a[index], o, null) == null)
return true;
Expand Down Expand Up @@ -297,7 +297,7 @@ private Delegate[] DeleteFromInvocationList(Delegate[] invocationList, int invoc
return newInvocationList;
}

private bool EqualInvocationLists(Delegate[] a, Delegate[] b, int start, int count)
private static bool EqualInvocationLists(Delegate[] a, Delegate[] b, int start, int count)
{
for (int i = 0; i < count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ namespace System.Runtime.InteropServices
{
public abstract partial class CriticalHandle : CriticalFinalizerObject, IDisposable
{
internal void ReleaseHandleFailed()
{
}

internal void SetHandleInternal(IntPtr handle)
{
SetHandle(handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ public void StartProfileOptimization(string profile)
{
}

private Assembly InternalLoadFromPath(string? assemblyPath, string? nativeImagePath)
private static Assembly InternalLoadFromPath(string? assemblyPath, string? nativeImagePath)
{
// TODO: This is not passing down the AssemblyLoadContext,
// so it won't actually work properly when multiple assemblies with the same identity get loaded.
return ReflectionAugments.ReflectionCoreCallbacks.Load(assemblyPath);
}

#pragma warning disable CA1822
internal Assembly InternalLoad(byte[] arrAssembly, byte[] arrSymbols)
{
return ReflectionAugments.ReflectionCoreCallbacks.Load(arrAssembly, arrSymbols);
}
#pragma warning restore CA1822

private void ReferenceUnreferencedEvents()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public bool Equals(RuntimeFieldHandle handle)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int _rotl(int value, int shift)
private static int _rotl(int value, int shift)
{
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public bool Equals(RuntimeMethodHandle handle)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int _rotl(int value, int shift)
private static int _rotl(int value, int shift)
{
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private void PlatformSpecificInitializeExistingThread()
_stopped = new ManualResetEvent(false);
}

#pragma warning disable CA1822
private ThreadPriority GetPriorityLive()
{
return ThreadPriority.Normal;
Expand All @@ -37,6 +38,7 @@ private bool SetPriorityLive(ThreadPriority priority)
{
return true;
}
#pragma warning restore CA1822

[UnmanagedCallersOnly]
private static void OnThreadExit()
Expand Down Expand Up @@ -132,9 +134,7 @@ private static bool SetApartmentStateUnchecked(ApartmentState state, bool throwO
return true;
}

private void InitializeComOnNewThread()
{
}
partial void InitializeComOnNewThread();

internal static void InitializeComForFinalizerThread()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public sealed partial class Thread

private static volatile bool s_comInitializedOnFinalizerThread;

private void PlatformSpecificInitialize()
{
}
partial void PlatformSpecificInitialize();

// Platform-specific initialization of foreign threads, i.e. threads not created by Thread.Start
private void PlatformSpecificInitializeExistingThread()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void Initialize()
RegisterThreadExitCallback();
}

private unsafe void RegisterThreadExitCallback()
private static unsafe void RegisterThreadExitCallback()
{
RuntimeImports.RhSetThreadExitCallback(&OnThreadExit);
}
Expand Down Expand Up @@ -196,10 +196,8 @@ public int ManagedThreadId
get => _managedThreadId.Id;
}

partial void ThreadNameChanged(string? value)
{
// TODO: Inform the debugger and the profiler
}
// TODO: Inform the debugger and the profiler
// private void ThreadNameChanged(string? value) {}

public ThreadPriority Priority
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public virtual object? Target
/// and gets\create a new RCW in case it is alive.
/// </summary>
/// <returns></returns>
private object? TryGetComTarget()
private static object? TryGetComTarget()
{
#if ENABLE_WINRT
WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;
Expand All @@ -174,7 +174,7 @@ public virtual object? Target
/// go from the managed weak reference to the actual native object even though the managed counterpart might have been collected.
/// </summary>
/// <param name="target"></param>
private void TrySetComTarget(object? target)
private static void TrySetComTarget(object? target)
{
#if ENABLE_WINRT
WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private T? Target
/// and gets\create a new RCW in case it is alive.
/// </summary>
/// <returns></returns>
private object? TryGetComTarget()
private static object? TryGetComTarget()
{
#if ENABLE_WINRT
WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;
Expand All @@ -99,7 +99,7 @@ private T? Target
/// go from the managed weak reference to the actual native object even though the managed counterpart might have been collected.
/// </summary>
/// <param name="target"></param>
private void TrySetComTarget(object? target)
private static void TrySetComTarget(object? target)
{
#if ENABLE_WINRT
WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public RuntimeTypeInfo(RuntimeTypeHandle typeHandle)
_typeHandle = typeHandle;
}

private bool DoNotThrowForNames => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForNames", out bool doNotThrow) && doNotThrow;
private static bool DoNotThrowForNames => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForNames", out bool doNotThrow) && doNotThrow;

private bool DoNotThrowForAssembly => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForAssembly", out bool doNotThrow) && doNotThrow;
private static bool DoNotThrowForAssembly => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForAssembly", out bool doNotThrow) && doNotThrow;

private bool DoNotThrowForAttributes => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForAttributes", out bool doNotThrow) && doNotThrow;
private static bool DoNotThrowForAttributes => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForAttributes", out bool doNotThrow) && doNotThrow;

public override RuntimeTypeHandle TypeHandle => _typeHandle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static unsafe bool TryGetNativeReaderForBlob(NativeFormatModuleInfo modu
return false;
}

private unsafe bool GetMarshallersForDelegate(RuntimeTypeHandle delegateTypeHandle, out IntPtr openStub, out IntPtr closedStub, out IntPtr delegateCreationStub)
private static unsafe bool GetMarshallersForDelegate(RuntimeTypeHandle delegateTypeHandle, out IntPtr openStub, out IntPtr closedStub, out IntPtr delegateCreationStub)
{
int delegateHashcode = delegateTypeHandle.GetHashCode();
openStub = IntPtr.Zero;
Expand Down Expand Up @@ -127,7 +127,7 @@ private unsafe bool GetMarshallersForDelegate(RuntimeTypeHandle delegateTypeHand
return false;
}

private unsafe bool TryGetStructData(RuntimeTypeHandle structTypeHandle, out ExternalReferencesTable externalReferences, out NativeParser entryParser)
private static unsafe bool TryGetStructData(RuntimeTypeHandle structTypeHandle, out ExternalReferencesTable externalReferences, out NativeParser entryParser)
{
int structHashcode = structTypeHandle.GetHashCode();
externalReferences = default(ExternalReferencesTable);
Expand Down Expand Up @@ -156,7 +156,7 @@ private unsafe bool TryGetStructData(RuntimeTypeHandle structTypeHandle, out Ext
return false;
}

private unsafe bool TryGetMarshallersForStruct(RuntimeTypeHandle structTypeHandle, out IntPtr marshalStub, out IntPtr unmarshalStub, out IntPtr destroyStub, out bool hasInvalidLayout, out int size)
private static unsafe bool TryGetMarshallersForStruct(RuntimeTypeHandle structTypeHandle, out IntPtr marshalStub, out IntPtr unmarshalStub, out IntPtr destroyStub, out bool hasInvalidLayout, out int size)
{
marshalStub = IntPtr.Zero;
unmarshalStub = IntPtr.Zero;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public bool SupportsReflection(Type type)

internal ReflectionDomainSetup ReflectionDomainSetup { get; }

internal IEnumerable<Type> PrimitiveTypes => s_primitiveTypes;
internal static IEnumerable<Type> PrimitiveTypes => s_primitiveTypes;

private static readonly Type[] s_primitiveTypes =
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal sealed override RuntimeTypeInfo UncachedGetTypeCoreCaseSensitive(string
return null;
}

private bool TryResolveNamespaceDefinitionCaseSensitive(MetadataReader reader, string[] namespaceParts, ScopeDefinitionHandle scopeDefinitionHandle, out NamespaceDefinition namespaceDefinition)
private static bool TryResolveNamespaceDefinitionCaseSensitive(MetadataReader reader, string[] namespaceParts, ScopeDefinitionHandle scopeDefinitionHandle, out NamespaceDefinition namespaceDefinition)
{
namespaceDefinition = scopeDefinitionHandle.GetScopeDefinition(reader).RootNamespaceDefinition.GetNamespaceDefinition(reader);
NamespaceDefinitionHandleCollection candidates = namespaceDefinition.NamespaceDefinitions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public sealed override bool OkToIgnoreAmbiguity(EventInfo m1, EventInfo m2)
return false;
}

private MethodInfo GetAccessorMethod(EventInfo e)
private static MethodInfo GetAccessorMethod(EventInfo e)
{
MethodInfo accessor = e.AddMethod;
return accessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public sealed override bool OkToIgnoreAmbiguity(PropertyInfo m1, PropertyInfo m2
return false;
}

private MethodInfo GetAccessorMethod(PropertyInfo property)
private static MethodInfo GetAccessorMethod(PropertyInfo property)
{
MethodInfo accessor = property.GetMethod;
if (accessor == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public sealed override PropertyInfo GetImplicitlyOverriddenBaseClassProperty(Pro
return p.GetImplicitlyOverriddenBaseClassMember();
}

private FieldInfo GetFieldInfo(RuntimeTypeHandle declaringTypeHandle, FieldHandle fieldHandle)
private static FieldInfo GetFieldInfo(RuntimeTypeHandle declaringTypeHandle, FieldHandle fieldHandle)
{
RuntimeTypeInfo contextTypeInfo = declaringTypeHandle.GetTypeForRuntimeTypeHandle();
NativeFormatRuntimeNamedTypeInfo definingTypeInfo = contextTypeInfo.AnchoringTypeDefinitionForDeclaredMembers.CastToNativeFormatRuntimeNamedTypeInfo();
Expand Down
Loading