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

CA1810 Initialize reference type static fields inline #7179

Merged
merged 17 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -85,7 +85,7 @@
<Rule Id="CA1802" Action="Info" /> <!-- Use literals where appropriate -->
<Rule Id="CA1805" Action="Info" /> <!-- Do not initialize unnecessarily -->
<Rule Id="CA1806" Action="None" />
<Rule Id="CA1810" Action="Info" /> <!-- Initialize reference type static fields inline -->
<Rule Id="CA1810" Action="Warning" /> <!-- Initialize reference type static fields inline -->
<Rule Id="CA1812" Action="None" /> <!-- Avoid uninstantiated internal classes -->
<Rule Id="CA1814" Action="None" />
<Rule Id="CA1815" Action="None" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ internal static class ItemGroupLoggingHelper
/// to materialize the Message as that's a declaration assembly. We inject the logic
/// here.
/// </summary>
#pragma warning disable CA1810 // Initialize reference type static fields inline
elachlan marked this conversation as resolved.
Show resolved Hide resolved
static ItemGroupLoggingHelper()
#pragma warning restore CA1810 // Initialize reference type static fields inline
{
BuildEventArgs.ResourceStringFormatter = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword;
TaskParameterEventArgs.MessageGetter = GetTaskParameterText;
Expand Down
25 changes: 4 additions & 21 deletions src/Build/Evaluation/ProjectRootElementCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,21 @@ internal class ProjectRootElementCache : ProjectRootElementCacheBase
/// If this number is increased much higher, the datastructure may
/// need to be changed from a linked list, since it's currently O(n).
/// </remarks>
private static readonly int s_maximumStrongCacheSize = 200;
private static readonly int s_maximumStrongCacheSize =
Convert.ToInt32(Environment.GetEnvironmentVariable("MSBUILDPROJECTROOTELEMENTCACHESIZE") ?? "200", NumberFormatInfo.InvariantInfo);
elachlan marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Whether the cache should log activity to the Debug.Out stream
/// </summary>
private static bool s_debugLogCacheActivity;
private static bool s_debugLogCacheActivity = Environment.GetEnvironmentVariable("MSBUILDDEBUGXMLCACHE") == "1";

/// <summary>
/// Whether the cache should check file content for cache entry invalidation.
/// </summary>
/// <remarks>
/// Value shall be true only in case of testing. Outside QA tests it shall be false.
/// </remarks>
private static bool s_сheckFileContent;
private static bool s_сheckFileContent = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MSBUILDCACHECHECKFILECONTENT"));

#if DEBUG
/// <summary>
Expand Down Expand Up @@ -119,24 +120,6 @@ internal class ProjectRootElementCache : ProjectRootElementCacheBase
/// </summary>
private Object _locker = new Object();

/// <summary>
/// Static constructor to choose cache size.
/// </summary>
static ProjectRootElementCache()
{
// Configurable in case a customer has related perf problems after shipping and so that
// we can measure different values for perf easily.
string userSpecifiedSize = Environment.GetEnvironmentVariable("MSBUILDPROJECTROOTELEMENTCACHESIZE");
if (!String.IsNullOrEmpty(userSpecifiedSize))
{
// Not catching as this is an undocumented setting
s_maximumStrongCacheSize = Convert.ToInt32(userSpecifiedSize, NumberFormatInfo.InvariantInfo);
}

s_debugLogCacheActivity = Environment.GetEnvironmentVariable("MSBUILDDEBUGXMLCACHE") == "1";
s_сheckFileContent = !String.IsNullOrEmpty(Environment.GetEnvironmentVariable("MSBUILDCACHECHECKFILECONTENT"));
}

/// <summary>
/// Creates an empty cache.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/MSBuild/XMake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ public enum ExitType
/// <summary>
/// Static constructor
/// </summary>
#pragma warning disable CA1810 // Initialize reference type static fields inline
elachlan marked this conversation as resolved.
Show resolved Hide resolved
static MSBuildApp()
#pragma warning restore CA1810 // Initialize reference type static fields inline
Copy link
Member

Choose a reason for hiding this comment

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

nit: I think I'd slightly prefer putting the restore below the end of this function, but I don't care too much.

{
try
{
Expand Down
7 changes: 1 addition & 6 deletions src/Shared/ExceptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ namespace Microsoft.Build.Shared
/// </summary>
internal static class ExceptionHandling
{
private static readonly string s_debugDumpPath;

static ExceptionHandling()
{
s_debugDumpPath = GetDebugDumpPath();
}
private static readonly string s_debugDumpPath = GetDebugDumpPath();
elachlan marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the location of the directory used for diagnostic log files.
Expand Down
10 changes: 2 additions & 8 deletions src/Shared/FrameworkLocationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,11 @@ private static readonly (Version, Version)[,] s_explicitFallbackRulesForPathToDo
};
#endif // FEATURE_WIN32_REGISTRY

private static readonly IReadOnlyDictionary<Version, DotNetFrameworkSpec> s_dotNetFrameworkSpecDict;
private static readonly IReadOnlyDictionary<Version, VisualStudioSpec> s_visualStudioSpecDict;
private static readonly IReadOnlyDictionary<Version, DotNetFrameworkSpec> s_dotNetFrameworkSpecDict = s_dotNetFrameworkSpecs.ToDictionary(spec => spec.Version);
private static readonly IReadOnlyDictionary<Version, VisualStudioSpec> s_visualStudioSpecDict = s_visualStudioSpecs.ToDictionary(spec => spec.Version);
elachlan marked this conversation as resolved.
Show resolved Hide resolved

#endregion // Static member variables

static FrameworkLocationHelper()
{
s_dotNetFrameworkSpecDict = s_dotNetFrameworkSpecs.ToDictionary(spec => spec.Version);
s_visualStudioSpecDict = s_visualStudioSpecs.ToDictionary(spec => spec.Version);
}

#region Static properties

internal static string PathToDotNetFrameworkV11
Expand Down
11 changes: 1 addition & 10 deletions src/Shared/MSBuildNameIgnoreCaseComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@ internal class MSBuildNameIgnoreCaseComparer : IConstrainedEqualityComparer<stri
/// <summary>
/// The processor architecture on which we are running, but default it will be x86
/// </summary>
private static readonly NativeMethodsShared.ProcessorArchitectures s_runningProcessorArchitecture;

/// <summary>
/// We need a static constructor to retrieve the running ProcessorArchitecture that way we can
elachlan marked this conversation as resolved.
Show resolved Hide resolved
/// avoid using optimized code that will not run correctly on IA64 due to alignment issues
/// </summary>
static MSBuildNameIgnoreCaseComparer()
{
s_runningProcessorArchitecture = NativeMethodsShared.ProcessorArchitecture;
}
private static readonly NativeMethodsShared.ProcessorArchitectures s_runningProcessorArchitecture = NativeMethodsShared.ProcessorArchitecture;

/// <summary>
/// The default immutable comparer instance.
Expand Down
9 changes: 1 addition & 8 deletions src/Shared/TypeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class TypeLoader
/// <summary>
/// AssemblyContextLoader used to load DLLs outside of msbuild.exe directory
/// </summary>
private static readonly CoreClrAssemblyLoader s_coreClrAssemblyLoader;
private static readonly CoreClrAssemblyLoader s_coreClrAssemblyLoader = new CoreClrAssemblyLoader();
#endif

/// <summary>
Expand All @@ -39,13 +39,6 @@ internal class TypeLoader
/// </summary>
private Func<Type, object, bool> _isDesiredType;

#if FEATURE_ASSEMBLYLOADCONTEXT
static TypeLoader()
{
s_coreClrAssemblyLoader = new CoreClrAssemblyLoader();
}
#endif

/// <summary>
/// Constructor.
/// </summary>
Expand Down