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

Conversation

elachlan
Copy link
Contributor

Relates to #7174

@elachlan
Copy link
Contributor Author

elachlan commented Jan 8, 2022

There are still 3 occurrences that I believe are false positives. I can add a suppression, but I think if this takes a while to merge it might be fixed in the analyzer by then.

@elachlan elachlan requested a review from Forgind January 18, 2022 23:31
Copy link
Member

@Forgind Forgind left a comment

Choose a reason for hiding this comment

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

Looks good! I'm assuming the false positive problem isn't about to be resolved, though we also shouldn't be merging right now because our last insertion failed for some reason, so we have a little time.

src/Tasks/CultureInfoCache.cs Outdated Show resolved Hide resolved
src/Tasks/GenerateResource.cs Outdated Show resolved Hide resolved
src/Tasks/GetFrameworkPath.cs Outdated Show resolved Hide resolved
@elachlan
Copy link
Contributor Author

Looks good! I'm assuming the false positive problem isn't about to be resolved, though we also shouldn't be merging right now because our last insertion failed for some reason, so we have a little time.

There is only one false positive and it has a suppression now. The other two were me not fully reading the analyzer article and understanding what it wanted me to do. Hence the fixes I just submitted.

Copy link
Member

@ladipro ladipro left a comment

Choose a reason for hiding this comment

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

Please note the tradeoffs being made when switching from an explicit static ctor to inline static field initializion. As the analyzer suggests, with beforefieldinit the JITted code may be slightly faster because it doesn't need to execute init checks. It may however lead to unwanted eager initialization. We should be especially careful when dealing with expensive initialization code. If there are scenarios where MSBuild "uses" the type but does not really call it or access its fields, then expensive initialization should be left in static ctors.

src/Build/Evaluation/ProjectRootElementCache.cs Outdated Show resolved Hide resolved
src/MSBuild/XMake.cs Show resolved Hide resolved
src/Shared/ExceptionHandling.cs Show resolved Hide resolved
src/Shared/FrameworkLocationHelper.cs Outdated Show resolved Hide resolved
Copy link
Member

@ladipro ladipro left a comment

Choose a reason for hiding this comment

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

Thank you for making the change. I left one comment which I feel is important. Please re-request review from @Forgind after addressing.

src/Shared/FrameworkLocationHelper.cs Outdated Show resolved Hide resolved
@ladipro ladipro self-requested a review January 27, 2022 08:47
@elachlan elachlan requested a review from Forgind January 27, 2022 10:40
@@ -72,7 +72,7 @@ internal static class FrameworkLocationHelper
internal static readonly Version visualStudioVersion170 = new Version(17, 0);

// keep this up-to-date; always point to the latest visual studio version.
internal static readonly Version visualStudioVersionLatest = visualStudioVersion160;
internal static readonly Version visualStudioVersionLatest = visualStudioVersion170;
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch!

Copy link
Member

@Forgind Forgind left a comment

Choose a reason for hiding this comment

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

Looks good to me!

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.

@Forgind Forgind added the merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now. label Jan 31, 2022
@AR-May AR-May merged commit 288ea72 into dotnet:main Jan 31, 2022
@elachlan elachlan deleted the CA1810 branch January 31, 2022 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants