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 13 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/Common.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ dotnet_diagnostic.CA1805.severity = suggestion
dotnet_diagnostic.CA1806.severity = none

# Initialize reference type static fields inline
dotnet_diagnostic.CA1810.severity = suggestion
dotnet_diagnostic.CA1810.severity = warning

# Avoid uninstantiated internal classes
dotnet_diagnostic.CA1812.severity = none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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
27 changes: 4 additions & 23 deletions src/Build/Evaluation/ProjectRootElementCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,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>
#pragma warning disable CA1802 // Use literals where appropriate
private static readonly int s_maximumStrongCacheSize = 200;
#pragma warning restore CA1802 // Use literals where appropriate
private static readonly int s_maximumStrongCacheSize =
int.TryParse(Environment.GetEnvironmentVariable("MSBUILDPROJECTROOTELEMENTCACHESIZE"), out int cacheSize) ? cacheSize : 200;

/// <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 @@ -123,24 +122,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 @@ -126,7 +126,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 @@ -34,12 +34,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 @@ -376,17 +376,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 @@ -22,16 +22,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 @@ -23,7 +23,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 @@ -41,13 +41,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
19 changes: 9 additions & 10 deletions src/Tasks/CultureInfoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,34 @@ namespace Microsoft.Build.Tasks
/// </summary>
internal static class CultureInfoCache
{
private static readonly HashSet<string> ValidCultureNames;
private static readonly HashSet<string> ValidCultureNames = InitializeValidCultureNames();

static CultureInfoCache()
static HashSet<string> InitializeValidCultureNames()
{
ValidCultureNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

#if !FEATURE_CULTUREINFO_GETCULTURES
if (!AssemblyUtilities.CultureInfoHasGetCultures())
{
ValidCultureNames = HardcodedCultureNames;
return;
return HardcodedCultureNames;
}
#endif

HashSet<string> validCultureNames = new(StringComparer.OrdinalIgnoreCase);
foreach (CultureInfo cultureName in AssemblyUtilities.GetAllCultures())
{
ValidCultureNames.Add(cultureName.Name);
validCultureNames.Add(cultureName.Name);
}

// https://docs.microsoft.com/en-gb/windows/desktop/Intl/using-pseudo-locales-for-localization-testing
// These pseudo-locales are available in versions of Windows from Vista and later.
// However, from Windows 10, version 1803, they are not returned when enumerating the
// installed cultures, even if the registry keys are set. Therefore, add them to the list manually.
var pseudoLocales = new[] { "qps-ploc", "qps-ploca", "qps-plocm", "qps-Latn-x-sh" };
string[] pseudoLocales = new[] { "qps-ploc", "qps-ploca", "qps-plocm", "qps-Latn-x-sh" };

foreach (string pseudoLocale in pseudoLocales)
{
ValidCultureNames.Add(pseudoLocale);
validCultureNames.Add(pseudoLocale);
}

return validCultureNames;
}

/// <summary>
Expand Down
27 changes: 2 additions & 25 deletions src/Tasks/GenerateResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,29 +538,6 @@ public GenerateResource()
// do nothing
}

#if FEATURE_COM_INTEROP
/// <summary>
/// Static constructor checks the registry opt-out for mark-of-the-web rejection.
/// </summary>
static GenerateResource()
{
if (!NativeMethodsShared.IsWindows)
{
allowMOTW = true;
return;
}
try
{
object allowUntrustedFiles = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\SDK", "AllowProcessOfUntrustedResourceFiles", null);
if (allowUntrustedFiles is String)
{
allowMOTW = ((string)allowUntrustedFiles).Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
catch { }
}
#endif

/// <summary>
/// Logs a Resgen.exe command line that indicates what parameters were
/// passed to this task. Since this task is replacing Resgen, and we used
Expand Down Expand Up @@ -923,7 +900,7 @@ public override bool Execute()
}

#if FEATURE_COM_INTEROP
private static bool allowMOTW;
private static readonly bool AllowMOTW = !NativeMethodsShared.IsWindows || (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\SDK", "AllowProcessOfUntrustedResourceFiles", null) is string allowUntrustedFiles && allowUntrustedFiles.Equals("true", StringComparison.OrdinalIgnoreCase));

private const string CLSID_InternetSecurityManager = "7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4";

Expand All @@ -944,7 +921,7 @@ public override bool Execute()
private bool IsDangerous(String filename)
{
// If they are opted out, there's no work to do
if (allowMOTW)
if (AllowMOTW)
{
return false;
}
Expand Down
53 changes: 16 additions & 37 deletions src/Tasks/GetFrameworkPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,6 @@ namespace Microsoft.Build.Tasks
/// </summary>
public class GetFrameworkPath : TaskExtension
{
static GetFrameworkPath()
{
s_path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Latest));
s_version11Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version11));
s_version20Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version20));
s_version30Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version30));
s_version35Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version35));
s_version40Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version40));
s_version45Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version45));
s_version451Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version451));
s_version452Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version452));
s_version46Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version46));
s_version461Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version461));
s_version462Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version462));
s_version47Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version47));
s_version471Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version471));
s_version472Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version472));
s_version48Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version48));
}

#region ITask Members

/// <summary>
Expand All @@ -52,23 +32,22 @@ public override bool Execute()
// it still seems to give an advantage perhaps because there is one less string copy.
// In a large build, this adds up.
// PERF NOTE: We also only find paths we are actually asked for (via <Output> tags)

private static readonly Lazy<string> s_path;
private static readonly Lazy<string> s_version11Path;
private static readonly Lazy<string> s_version20Path;
private static readonly Lazy<string> s_version30Path;
private static readonly Lazy<string> s_version35Path;
private static readonly Lazy<string> s_version40Path;
private static readonly Lazy<string> s_version45Path;
private static readonly Lazy<string> s_version451Path;
private static readonly Lazy<string> s_version452Path;
private static readonly Lazy<string> s_version46Path;
private static readonly Lazy<string> s_version461Path;
private static readonly Lazy<string> s_version462Path;
private static readonly Lazy<string> s_version47Path;
private static readonly Lazy<string> s_version471Path;
private static readonly Lazy<string> s_version472Path;
private static readonly Lazy<string> s_version48Path;
private static readonly Lazy<string> s_path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Latest));
private static readonly Lazy<string> s_version11Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version11));
private static readonly Lazy<string> s_version20Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version20));
private static readonly Lazy<string> s_version30Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version30));
private static readonly Lazy<string> s_version35Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version35));
private static readonly Lazy<string> s_version40Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version40));
private static readonly Lazy<string> s_version45Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version45));
private static readonly Lazy<string> s_version451Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version451));
private static readonly Lazy<string> s_version452Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version452));
private static readonly Lazy<string> s_version46Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version46));
private static readonly Lazy<string> s_version461Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version461));
private static readonly Lazy<string> s_version462Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version462));
private static readonly Lazy<string> s_version47Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version47));
private static readonly Lazy<string> s_version471Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version471));
private static readonly Lazy<string> s_version472Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version472));
private static readonly Lazy<string> s_version48Path = new Lazy<string>(() => ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version48));

/// <summary>
/// Path to the latest framework, whatever version it happens to be
Expand Down
Loading