Skip to content

Commit

Permalink
Make more progress on IDE0044 ("Make field readonly") (dotnet#82356)
Browse files Browse the repository at this point in the history
* Make more progress on IDE0044 ("Make field readonly")

* Apply suggestions from code review

Co-authored-by: Jan Kotas <[email protected]>

---------

Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
stephentoub and jkotas authored Feb 20, 2023
1 parent 51cce42 commit 6d325e1
Show file tree
Hide file tree
Showing 76 changed files with 233 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace System.Diagnostics
{
internal sealed class EditAndContinueHelper
{
#pragma warning disable CA1823, 169, 414 // field is not used from managed.
#pragma warning disable CA1823, 169, 414, IDE0044 // field is not used from managed.
private object? _objectReference;
#pragma warning restore CA1823, 169, 414
#pragma warning restore CA1823, 169, 414, IDE0044
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace System.Reflection.Emit
{
public class CustomAttributeBuilder
{
internal ConstructorInfo m_con;
private object?[] m_constructorArgs;
private byte[] m_blob;
internal readonly ConstructorInfo m_con;
private readonly object?[] m_constructorArgs;
private readonly byte[] m_blob;

// public constructor to form the custom attribute with constructor and constructor
// parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Reflection.Emit
internal sealed class DynamicILGenerator : ILGenerator
{
internal DynamicScope m_scope;
private int m_methodSigToken;
private readonly int m_methodSigToken;

internal DynamicILGenerator(DynamicMethod method, byte[] methodSignature, int size)
: base(method, size)
Expand Down Expand Up @@ -571,13 +571,13 @@ private int GetTokenForSig(byte[] sig)
internal sealed class DynamicResolver : Resolver
{
#region Private Data Members
private __ExceptionInfo[]? m_exceptions;
private byte[]? m_exceptionHeader;
private DynamicMethod m_method;
private byte[] m_code;
private byte[] m_localSignature;
private int m_stackSize;
private DynamicScope m_scope;
private readonly __ExceptionInfo[]? m_exceptions;
private readonly byte[]? m_exceptionHeader;
private readonly DynamicMethod m_method;
private readonly byte[] m_code;
private readonly byte[] m_localSignature;
private readonly int m_stackSize;
private readonly DynamicScope m_scope;
#endregion

#region Internal Methods
Expand Down Expand Up @@ -857,13 +857,13 @@ internal override void ResolveToken(int token, out IntPtr typeHandle, out IntPtr
public class DynamicILInfo
{
#region Private Data Members
private DynamicMethod m_method;
private DynamicScope m_scope;
private readonly DynamicMethod m_method;
private readonly DynamicScope m_scope;
private byte[] m_exceptions;
private byte[] m_code;
private byte[] m_localSignature;
private int m_maxStackSize;
private int m_methodSignature;
private readonly int m_methodSignature;
#endregion

#region Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace System.Reflection.Emit
public sealed class LocalBuilder : LocalVariableInfo
{
#region Private Data Members
private int m_localIndex;
private Type m_localType;
private MethodInfo m_methodBuilder;
private bool m_isPinned;
private readonly int m_localIndex;
private readonly Type m_localType;
private readonly MethodInfo m_methodBuilder;
private readonly bool m_isPinned;
#endregion

#region Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal int GetToken()
private readonly string? _name;
private readonly int _position;
private readonly ParameterAttributes _attributes;
private RuntimeMethodBuilder _methodBuilder;
private int _token;
private readonly RuntimeMethodBuilder _methodBuilder;
private readonly int _token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,6 @@ internal RuntimeEnumBuilder(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
internal RuntimeTypeBuilder m_typeBuilder;

private FieldBuilder m_underlyingField;
private readonly FieldBuilder m_underlyingField;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuil
customBuilder.CreateCustomAttribute(m_module, m_evToken);
}

// These are package private so that TypeBuilder can access them.
private string m_name; // The name of the event
private int m_evToken; // The token of this event
private RuntimeModuleBuilder m_module;
private EventAttributes m_attributes;
private RuntimeTypeBuilder m_type;
private readonly string m_name; // The name of the event
private readonly int m_evToken; // The token of this event
private readonly RuntimeModuleBuilder m_module;
private readonly EventAttributes m_attributes;
private readonly RuntimeTypeBuilder m_type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace System.Reflection.Emit
internal sealed class RuntimeFieldBuilder : FieldBuilder
{
#region Private Data Members
private int m_fieldTok;
private RuntimeTypeBuilder m_typeBuilder;
private string m_fieldName;
private FieldAttributes m_Attributes;
private Type m_fieldType;
private readonly int m_fieldTok;
private readonly RuntimeTypeBuilder m_typeBuilder;
private readonly string m_fieldName;
private readonly FieldAttributes m_Attributes;
private readonly Type m_fieldType;
#endregion

#region Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ public override bool IsDefined(Type attributeType, bool inherit)
public override Type? ReflectedType => m_containingType;

// These are package private so that TypeBuilder can access them.
private string m_name; // The name of the property
private int m_tkProperty; // The token of this property
private RuntimeModuleBuilder m_moduleBuilder;
private PropertyAttributes m_attributes; // property's attribute flags
private Type m_returnType; // property's return type
private readonly string m_name; // The name of the property
private readonly int m_tkProperty; // The token of this property
private readonly RuntimeModuleBuilder m_moduleBuilder;
private readonly PropertyAttributes m_attributes; // property's attribute flags
private readonly Type m_returnType; // property's return type
private MethodInfo? m_getMethod;
private MethodInfo? m_setMethod;
private RuntimeTypeBuilder m_containingType;
private readonly RuntimeTypeBuilder m_containingType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace System.Reflection.Emit
internal sealed class SymbolMethod : MethodInfo
{
#region Private Data Members
private ModuleBuilder m_module;
private Type m_containingType;
private string m_name;
private CallingConventions m_callingConvention;
private Type m_returnType;
private int m_token;
private Type[] m_parameterTypes;
private readonly ModuleBuilder m_module;
private readonly Type m_containingType;
private readonly string m_name;
private readonly CallingConventions m_callingConvention;
private readonly Type m_returnType;
private readonly int m_token;
private readonly Type[] m_parameterTypes;
#endregion

#region Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace System.Reflection
internal sealed unsafe class MdFieldInfo : RuntimeFieldInfo
{
#region Private Data Members
private int m_tkField;
private readonly int m_tkField;
private string? m_name;
private RuntimeType? m_fieldType;
private FieldAttributes m_fieldAttributes;
private readonly FieldAttributes m_fieldAttributes;
#endregion

#region Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ public bool IsValidToken(int token)

internal sealed class MetadataException : Exception
{
private int m_hr;
private readonly int m_hr;
internal MetadataException(int hr) { m_hr = hr; }

public override string ToString() => $"MetadataException HResult = {m_hr:x}.";
public override string ToString() => $"{nameof(MetadataException)} HResult = {m_hr:x}.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal sealed unsafe class RtFieldInfo : RuntimeFieldInfo, IRuntimeFieldInfo
{
#region Private Data Members
// aggressive caching
private IntPtr m_fieldHandle;
private FieldAttributes m_fieldAttributes;
private readonly IntPtr m_fieldHandle;
private readonly FieldAttributes m_fieldAttributes;
// lazy caching
private string? m_name;
private RuntimeType? m_fieldType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal sealed partial class RuntimeAssembly : Assembly
private sealed class ManifestResourceStream : UnmanagedMemoryStream
{
// ensures the RuntimeAssembly is kept alive for as long as the stream lives
private RuntimeAssembly _manifestAssembly;
private readonly RuntimeAssembly _manifestAssembly;

internal unsafe ManifestResourceStream(RuntimeAssembly manifestAssembly, byte* pointer, long length, long capacity, FileAccess access) : base(pointer, length, capacity, access)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ namespace System.Reflection
internal sealed partial class RuntimeConstructorInfo : ConstructorInfo, IRuntimeMethodInfo
{
#region Private Data Members
private volatile RuntimeType m_declaringType;
private RuntimeTypeCache m_reflectedTypeCache;
private readonly RuntimeType m_declaringType;
private readonly RuntimeTypeCache m_reflectedTypeCache;
private string? m_toString;
private ParameterInfo[]? m_parameters; // Created lazily when GetParameters() is called.
#pragma warning disable CA1823, 414, 169
#pragma warning disable CA1823, 414, 169, IDE0044
private object? _empty1; // These empties are used to ensure that RuntimeConstructorInfo and RuntimeMethodInfo are have a layout which is sufficiently similar
private object? _empty2;
private object? _empty3;
#pragma warning restore CA1823, 414, 169
private IntPtr m_handle;
private MethodAttributes m_methodAttributes;
private BindingFlags m_bindingFlags;
#pragma warning restore CA1823, 414, 169, IDE0044
private readonly IntPtr m_handle;
private readonly MethodAttributes m_methodAttributes;
private readonly BindingFlags m_bindingFlags;
private Signature? m_signature;
private ConstructorInvoker? m_invoker;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace System.Reflection
internal sealed unsafe class RuntimeEventInfo : EventInfo
{
#region Private Data Members
private int m_token;
private EventAttributes m_flags;
private readonly int m_token;
private readonly EventAttributes m_flags;
private string? m_name;
private void* m_utf8name;
private RuntimeTypeCache m_reflectedTypeCache;
private RuntimeMethodInfo? m_addMethod;
private RuntimeMethodInfo? m_removeMethod;
private RuntimeMethodInfo? m_raiseMethod;
private MethodInfo[]? m_otherMethod;
private RuntimeType m_declaringType;
private BindingFlags m_bindingFlags;
private readonly void* m_utf8name;
private readonly RuntimeTypeCache m_reflectedTypeCache;
private readonly RuntimeMethodInfo? m_addMethod;
private readonly RuntimeMethodInfo? m_removeMethod;
private readonly RuntimeMethodInfo? m_raiseMethod;
private readonly MethodInfo[]? m_otherMethod;
private readonly RuntimeType m_declaringType;
private readonly BindingFlags m_bindingFlags;
#endregion

#region Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace System.Reflection
internal abstract class RuntimeFieldInfo : FieldInfo
{
#region Private Data Members
private BindingFlags m_bindingFlags;
protected RuntimeTypeCache m_reflectedTypeCache;
protected RuntimeType m_declaringType;
private readonly BindingFlags m_bindingFlags;
protected readonly RuntimeTypeCache m_reflectedTypeCache;
protected readonly RuntimeType m_declaringType;
#endregion

#region Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ namespace System.Reflection
internal sealed partial class RuntimeMethodInfo : MethodInfo, IRuntimeMethodInfo
{
#region Private Data Members
private IntPtr m_handle;
private RuntimeTypeCache m_reflectedTypeCache;
private readonly IntPtr m_handle;
private readonly RuntimeTypeCache m_reflectedTypeCache;
private string? m_name;
private string? m_toString;
private ParameterInfo[]? m_parameters;
private ParameterInfo? m_returnParameter;
private BindingFlags m_bindingFlags;
private MethodAttributes m_methodAttributes;
private readonly BindingFlags m_bindingFlags;
private readonly MethodAttributes m_methodAttributes;
private Signature? m_signature;
private RuntimeType m_declaringType;
private object? m_keepalive;
private readonly RuntimeType m_declaringType;
private readonly object? m_keepalive;
private MethodInvoker? m_invoker;

internal InvocationFlags InvocationFlags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ public override void GetPEKind(out PortableExecutableKinds peKind, out ImageFile
#pragma warning disable CA1823, 169
// If you add any data members, you need to update the native declaration ReflectModuleBaseObject.
private RuntimeType m_runtimeType;
private RuntimeAssembly m_runtimeAssembly;
private IntPtr m_pRefClass;
private IntPtr m_pData;
private IntPtr m_pGlobals;
private IntPtr m_pFields;
private readonly RuntimeAssembly m_runtimeAssembly;
private readonly IntPtr m_pRefClass;
private readonly IntPtr m_pData;
private readonly IntPtr m_pGlobals;
private readonly IntPtr m_pFields;
#pragma warning restore CA1823, 169
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ private static ParameterInfo[] GetParameters(
#endregion

#region Private Data Members
private int m_tkParamDef;
private MetadataImport m_scope;
private Signature? m_signature;
private readonly int m_tkParamDef;
private readonly MetadataImport m_scope;
private readonly Signature? m_signature;
private volatile bool m_nameIsCached;
private readonly bool m_noMetadata;
private bool m_noDefaultValue;
private MethodBase? m_originalMember;
private readonly MethodBase? m_originalMember;
#endregion

#region Internal Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ namespace System.Reflection
internal sealed unsafe class RuntimePropertyInfo : PropertyInfo
{
#region Private Data Members
private int m_token;
private readonly int m_token;
private string? m_name;
private void* m_utf8name;
private PropertyAttributes m_flags;
private RuntimeTypeCache m_reflectedTypeCache;
private RuntimeMethodInfo? m_getterMethod;
private RuntimeMethodInfo? m_setterMethod;
private MethodInfo[]? m_otherMethod;
private RuntimeType m_declaringType;
private BindingFlags m_bindingFlags;
private readonly void* m_utf8name;
private readonly PropertyAttributes m_flags;
private readonly RuntimeTypeCache m_reflectedTypeCache;
private readonly RuntimeMethodInfo? m_getterMethod;
private readonly RuntimeMethodInfo? m_setterMethod;
private readonly MethodInfo[]? m_otherMethod;
private readonly RuntimeType m_declaringType;
private readonly BindingFlags m_bindingFlags;
private Signature? m_signature;
private ParameterInfo[]? m_parameters;
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private static unsafe void DispatchTailCalls(
}
}

#pragma warning disable 0414
#pragma warning disable 0414, IDE0044
// Type that represents a managed view of the unmanaged GCFrame
// data structure in coreclr. The type layouts between the two should match.
internal unsafe ref struct GCFrameRegistration
Expand All @@ -401,7 +401,7 @@ public GCFrameRegistration(void* allocation, uint elemCount, bool areByRefs = tr
m_MaybeInterior = areByRefs ? 1 : 0;
}
}
#pragma warning restore 0414
#pragma warning restore 0414, IDE0044

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern unsafe void RegisterForGCReporting(GCFrameRegistration* pRegistration);
Expand Down
Loading

0 comments on commit 6d325e1

Please sign in to comment.