Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ dotnet_diagnostic.IDE0043.severity = silent

# IDE0044: MakeFieldReadonly
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044
dotnet_diagnostic.IDE0044.severity = silent
dotnet_diagnostic.IDE0044.severity = warning

# IDE0045: UseConditionalExpressionForAssignment
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0045
Expand Down
10 changes: 5 additions & 5 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ internal static class NativeMethods
/// <summary>Unix specific implementations of required functionality.</summary>
internal static class Unix
{
private static Dictionary<int, string> usernameCache = new();
private static Dictionary<int, string> groupnameCache = new();
private static readonly Dictionary<int, string> usernameCache = new();
private static readonly Dictionary<int, string> groupnameCache = new();

/// <summary>The type of a Unix file system item.</summary>
public enum ItemType
Expand Down Expand Up @@ -755,7 +755,7 @@ public class CommonStat
private const char CanExecute = 'x';

// helper for getting unix mode
private Dictionary<StatMask, char> modeMap = new()
private readonly Dictionary<StatMask, char> modeMap = new()
{
{ StatMask.OwnerRead, CanRead },
{ StatMask.OwnerWrite, CanWrite },
Expand All @@ -768,7 +768,7 @@ public class CommonStat
{ StatMask.OtherExecute, CanExecute },
};

private StatMask[] permissions = new StatMask[]
private readonly StatMask[] permissions = new StatMask[]
{
StatMask.OwnerRead,
StatMask.OwnerWrite,
Expand All @@ -782,7 +782,7 @@ public class CommonStat
};

// The item type and the character representation for the first element in the stat string
private Dictionary<ItemType, char> itemTypeTable = new()
private readonly Dictionary<ItemType, char> itemTypeTable = new()
{
{ ItemType.BlockDevice, 'b' },
{ ItemType.CharacterDevice, 'c' },
Expand Down
6 changes: 3 additions & 3 deletions src/System.Management.Automation/DscSupport/CimDSCParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ public override object Transform(EngineIntrinsics engineIntrinsics, object input
/// </summary>
internal class CimDSCParser
{
private CimMofDeserializer _deserializer;
private CimMofDeserializer.OnClassNeeded _onClassNeeded;
private readonly CimMofDeserializer _deserializer;
private readonly CimMofDeserializer.OnClassNeeded _onClassNeeded;

/// <summary>
/// </summary>
Expand Down Expand Up @@ -529,7 +529,7 @@ public static class DscClassCache

private const string reservedProperties = "^(Require|Trigger|Notify|Before|After|Subscribe)$";

private static PSTraceSource s_tracer = PSTraceSource.GetTracer("DSC", "DSC Class Cache");
private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("DSC", "DSC Class Cache");

// Constants for items in the module qualified name (Module\Version\ClassName)
private const int IndexModuleName = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/System.Management.Automation/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage(
"Style",
"IDE0044:Add readonly modifier",
Justification = "see src/System.Management.Automation/engine/ComInterop/README.md",
Scope = "NamespaceAndDescendants",
Target = "~N:System.Management.Automation.ComInterop")]
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ private static ModuleBuilder CreateModuleBuilder()
return mb;
}

private static Lazy<ModuleBuilder> s_moduleBuilder = new(CreateModuleBuilder, isThreadSafe: true);
private static object s_moduleBuilderUsageLock = new();
private static readonly Lazy<ModuleBuilder> s_moduleBuilder = new(CreateModuleBuilder, isThreadSafe: true);
private static readonly object s_moduleBuilderUsageLock = new();

internal static string GetEnumFullName(EnumMetadataEnum enumMetadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private List<string> GetMethodParameterSets(StaticCmdletMetadata staticCmdlet)
return new List<string>(parameterSetNames.Keys);
}

private Dictionary<CommonMethodMetadata, int> _staticMethodMetadataToUniqueId = new();
private readonly Dictionary<CommonMethodMetadata, int> _staticMethodMetadataToUniqueId = new();

private string GetMethodParameterSet(CommonMethodMetadata methodMetadata)
{
Expand Down Expand Up @@ -2100,7 +2100,7 @@ private void WriteGetCmdlet(TextWriter output)
/* 1 */ CodeGeneration.EscapeSingleQuotedStringContent(commandMetadata.Name));
}

private static object s_enumCompilationLock = new();
private static readonly object s_enumCompilationLock = new();

private static void CompileEnum(EnumMetadataEnum enumMetadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public void Dispose()
// TODO: Look into using SessionState.Internal.GetAliasTable() to find all user created aliases.
// But update Alias command logic to maintain reverse table that lists all aliases mapping
// to a single command definition, for performance.
private static string[] forEachNames = new string[]
private static readonly string[] forEachNames = new string[]
{
"ForEach-Object",
"foreach",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ internal void AddToArgumentList(CommandParameterInternal parameter, string argum
}
}

private List<string> _argumentList = new List<string>();
private readonly List<string> _argumentList = new List<string>();

/// <summary>
/// Gets a value indicating whether to use an ArgumentList or string for arguments when invoking a native executable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ internal sealed class InstructionList
private List<BranchLabel> _labels;

// list of (instruction index, cookie) sorted by instruction index:
#pragma warning disable IDE0044 // Add readonly modifier
private List<KeyValuePair<int, object>> _debugCookies = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

The null assignment can be removed.

#pragma warning restore IDE0044 // Variable is assigned when DEBUG is defined.

#region Debug View

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ internal class HyperVSocketEndPoint : EndPoint

private readonly System.Net.Sockets.AddressFamily _addressFamily;
private Guid _vmId;
#pragma warning disable IDE0044
private Guid _serviceId;
#pragma warning restore IDE0044 // https://github.com/PowerShell/PowerShell/issues/13995

public const System.Net.Sockets.AddressFamily AF_HYPERV = (System.Net.Sockets.AddressFamily)34;
public const int HYPERV_SOCK_ADDR_SIZE = 36;
Expand Down
4 changes: 2 additions & 2 deletions src/System.Management.Automation/logging/MshLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ internal static class MshLog
/// The value of this dictionary is never empty. A value of type DummyProvider means
/// no logging.
/// </summary>
private static ConcurrentDictionary<string, Collection<LogProvider>> s_logProviders =
private static readonly ConcurrentDictionary<string, Collection<LogProvider>> s_logProviders =
new ConcurrentDictionary<string, Collection<LogProvider>>();

private const string _crimsonLogProviderAssemblyName = "MshCrimsonLog";
private const string _crimsonLogProviderTypeName = "System.Management.Automation.Logging.CrimsonLogProvider";

private static Collection<string> s_ignoredCommands = new Collection<string>();
private static readonly Collection<string> s_ignoredCommands = new Collection<string>();

/// <summary>
/// Static constructor.
Expand Down
8 changes: 4 additions & 4 deletions src/System.Management.Automation/security/CatalogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public class CatalogInformation
internal static class CatalogHelper
{
// Catalog Version is (0X100 = 256) for Catalog Version 1
private static int catalogVersion1 = 256;
private const int catalogVersion1 = 256;

// Catalog Version is (0X200 = 512) for Catalog Version 2
private static int catalogVersion2 = 512;
private const int catalogVersion2 = 512;

// Hash Algorithms supported by Windows Catalog
private static string HashAlgorithmSHA1 = "SHA1";
private static string HashAlgorithmSHA256 = "SHA256";
private const string HashAlgorithmSHA1 = "SHA1";
private const string HashAlgorithmSHA256 = "SHA256";
private static PSCmdlet _cmdlet = null;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal enum RunPromptDecision
private ExecutionPolicy _executionPolicy;

// shellId supplied by runspace configuration
private string _shellId;
private readonly string _shellId;

/// <summary>
/// Initializes a new instance of the PSAuthorizationManager
Expand Down
6 changes: 3 additions & 3 deletions src/System.Management.Automation/security/SecuritySupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ public CmsMessageRecipient(string identifier)
this.Certificates = new X509Certificate2Collection();
}

private string _identifier = null;
private readonly string _identifier;

/// <summary>
/// Creates an instance of the CmsMessageRecipient class.
Expand All @@ -1007,7 +1007,7 @@ public CmsMessageRecipient(X509Certificate2 certificate)
this.Certificates = new X509Certificate2Collection();
}

private X509Certificate2 _pendingCertificate = null;
private readonly X509Certificate2 _pendingCertificate;

/// <summary>
/// Gets the certificate associated with this recipient.
Expand Down Expand Up @@ -1499,7 +1499,7 @@ internal static void CurrentDomain_ProcessExit(object sender, EventArgs e)
private static IntPtr s_amsiSession = IntPtr.Zero;

private static bool s_amsiInitFailed = false;
private static object s_amsiLockObject = new object();
private static readonly object s_amsiLockObject = new object();

/// <summary>
/// Reset the AMSI session (used to track related script invocations)
Expand Down
64 changes: 33 additions & 31 deletions src/System.Management.Automation/security/nativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,36 +1088,38 @@ internal static DWORD DestroyWintrustDataStruct(WINTRUST_DATA wtd)
[StructLayout(LayoutKind.Sequential)]
internal struct CRYPT_PROVIDER_CERT
{
#pragma warning disable IDE0044
private DWORD _cbStruct;
#pragma warning restore IDE0044
internal IntPtr pCert; // PCCERT_CONTEXT
private BOOL _fCommercial;
private BOOL _fTrustedRoot;
private BOOL _fSelfSigned;
private BOOL _fTestCert;
private DWORD _dwRevokedReason;
private DWORD _dwConfidence;
private DWORD _dwError;
private IntPtr _pTrustListContext; // CTL_CONTEXT*
private BOOL _fTrustListSignerCert;
private IntPtr _pCtlContext; // PCCTL_CONTEXT
private DWORD _dwCtlError;
private BOOL _fIsCyclic;
private IntPtr _pChainElement; // PCERT_CHAIN_ELEMENT
private readonly BOOL _fCommercial;
private readonly BOOL _fTrustedRoot;
private readonly BOOL _fSelfSigned;
private readonly BOOL _fTestCert;
private readonly DWORD _dwRevokedReason;
private readonly DWORD _dwConfidence;
private readonly DWORD _dwError;
private readonly IntPtr _pTrustListContext; // CTL_CONTEXT*
private readonly BOOL _fTrustListSignerCert;
private readonly IntPtr _pCtlContext; // PCCTL_CONTEXT
private readonly DWORD _dwCtlError;
private readonly BOOL _fIsCyclic;
private readonly IntPtr _pChainElement; // PCERT_CHAIN_ELEMENT
}

[StructLayout(LayoutKind.Sequential)]
internal struct CRYPT_PROVIDER_SGNR
Copy link
Collaborator

Choose a reason for hiding this comment

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

The same.

{
private DWORD _cbStruct;
private readonly DWORD _cbStruct;
private FILETIME _sftVerifyAsOf;
private DWORD _csCertChain;
private IntPtr _pasCertChain; // CRYPT_PROVIDER_CERT*
private DWORD _dwSignerType;
private IntPtr _psSigner; // CMSG_SIGNER_INFO*
private DWORD _dwError;
private readonly DWORD _csCertChain;
private readonly IntPtr _pasCertChain; // CRYPT_PROVIDER_CERT*
private readonly DWORD _dwSignerType;
private readonly IntPtr _psSigner; // CMSG_SIGNER_INFO*
private readonly DWORD _dwError;
internal DWORD csCounterSigners;
internal IntPtr pasCounterSigners; // CRYPT_PROVIDER_SGNR*
private IntPtr _pChainContext; // PCCERT_CHAIN_CONTEXT
private readonly IntPtr _pChainContext; // PCCERT_CHAIN_CONTEXT
}

[DllImport("wintrust.dll", SetLastError = true, CharSet = CharSet.Unicode)]
Expand Down Expand Up @@ -1993,33 +1995,33 @@ internal struct CRYPTCATMEMBER
[StructLayout(LayoutKind.Sequential)]
internal struct CRYPTCATATTRIBUTE
Copy link
Collaborator

Choose a reason for hiding this comment

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

The same.

{
private DWORD _cbStruct;
private readonly DWORD _cbStruct;

[MarshalAs(UnmanagedType.LPWStr)]
internal string pwszReferenceTag;

private DWORD _dwAttrTypeAndAction;
private readonly DWORD _dwAttrTypeAndAction;
internal DWORD cbValue;
internal System.IntPtr pbValue;
private DWORD _dwReserved;
private readonly DWORD _dwReserved;
}

[StructLayout(LayoutKind.Sequential)]
internal struct CRYPTCATSTORE
Copy link
Collaborator

Choose a reason for hiding this comment

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

The same.

{
private DWORD _cbStruct;
private readonly DWORD _cbStruct;
internal DWORD dwPublicVersion;

[MarshalAs(UnmanagedType.LPWStr)]
internal string pwszP7File;

private IntPtr _hProv;
private DWORD _dwEncodingType;
private DWORD _fdwStoreFlags;
private IntPtr _hReserved;
private IntPtr _hAttrs;
private IntPtr _hCryptMsg;
private IntPtr _hSorted;
private readonly IntPtr _hProv;
private readonly DWORD _dwEncodingType;
private readonly DWORD _fdwStoreFlags;
private readonly IntPtr _hReserved;
private readonly IntPtr _hAttrs;
private readonly IntPtr _hCryptMsg;
private readonly IntPtr _hSorted;
}

[DllImport("wintrust.dll", CharSet = CharSet.Unicode)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static SystemEnforcementMode GetSystemLockdownPolicy()
return s_systemLockdownPolicy.Value;
}

private static object s_systemLockdownPolicyLock = new object();
private static readonly object s_systemLockdownPolicyLock = new object();
private static SystemEnforcementMode? s_systemLockdownPolicy = null;
private static bool s_allowDebugOverridePolicy = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void CreateErrorRecord()
_errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(this), "ConsoleLoadFailure", ErrorCategory.ResourceUnavailable, null);
}

private Collection<PSSnapInException> _PSSnapInExceptions = new Collection<PSSnapInException>();
private readonly Collection<PSSnapInException> _PSSnapInExceptions = new Collection<PSSnapInException>();

internal Collection<PSSnapInException> PSSnapInExceptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ internal string AbsoluteModulePath
/// </summary>
public Collection<string> Formats { get; }

private string _descriptionIndirect;
private string _descriptionFallback = string.Empty;
private readonly string _descriptionIndirect;
private readonly string _descriptionFallback = string.Empty;
private string _description;
/// <summary>
/// Description of mshsnapin.
Expand All @@ -281,8 +281,8 @@ public string Description
}
}

private string _vendorIndirect;
private string _vendorFallback = string.Empty;
private readonly string _vendorIndirect;
private readonly string _vendorFallback = string.Empty;
private string _vendor;
/// <summary>
/// Vendor of mshsnapin.
Expand Down Expand Up @@ -1335,10 +1335,10 @@ private static IList<DefaultPSSnapInInformation> DefaultMshSnapins
}

private static IList<DefaultPSSnapInInformation> s_defaultMshSnapins = null;
private static object s_syncObject = new object();
private static readonly object s_syncObject = new object();

#endregion

private static PSTraceSource s_mshsnapinTracer = PSTraceSource.GetTracer("MshSnapinLoadUnload", "Loading and unloading mshsnapins", false);
private static readonly PSTraceSource s_mshsnapinTracer = PSTraceSource.GetTracer("MshSnapinLoadUnload", "Loading and unloading mshsnapins", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void CreateErrorRecord()
}
}

private bool _warning = false;
private readonly bool _warning = false;

private ErrorRecord _errorRecord;
private bool _isErrorRecordOriginallyNull;
Expand Down Expand Up @@ -146,8 +146,8 @@ public override ErrorRecord ErrorRecord
}
}

private string _PSSnapin = string.Empty;
private string _reason = string.Empty;
private readonly string _PSSnapin = string.Empty;
private readonly string _reason = string.Empty;

/// <summary>
/// Gets message for this exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class PSEtwLogProvider : LogProvider
{
private static readonly EventProvider etwProvider;
internal static readonly Guid ProviderGuid = new Guid("F90714A8-5509-434A-BF6D-B1624C8A19A2");
private static EventDescriptor _xferEventDescriptor = new EventDescriptor(0x1f05, 0x1, 0x11, 0x5, 0x14, 0x0, (long)0x4000000000000000);
private static readonly EventDescriptor _xferEventDescriptor = new EventDescriptor(0x1f05, 0x1, 0x11, 0x5, 0x14, 0x0, (long)0x4000000000000000);

/// <summary>
/// Class constructor.
Expand Down
Loading