Skip to content

CA1810 Initialize reference type static fields inline - #7179

Merged
AR-May merged 17 commits into
dotnet:mainfrom
elachlan:CA1810
Jan 31, 2022
Merged

AR-May merged 17 commits into
dotnet:mainfrom
elachlan:CA1810

Conversation

@elachlan

Copy link
Copy Markdown
Contributor

Relates to #7174

Comment thread src/MSBuild/XMake.cs Resolved
Comment thread src/Shared/FrameworkLocationHelper.cs Resolved
@elachlan

elachlan commented Jan 8, 2022

Copy link
Copy Markdown
Contributor Author

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

@Forgind Forgind left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread src/Tasks/CultureInfoCache.cs Resolved
Comment thread src/Tasks/GenerateResource.cs Resolved
Comment thread src/Tasks/GetFrameworkPath.cs Resolved
@elachlan

Copy link
Copy Markdown
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.

@ladipro ladipro left a comment

Copy link
Copy Markdown
Member

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.

Comment thread src/MSBuild/XMake.cs Resolved
Comment thread src/Shared/ExceptionHandling.cs Resolved
Comment thread src/Shared/FrameworkLocationHelper.cs Resolved

@ladipro ladipro left a comment

Copy link
Copy Markdown
Member

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.

Comment thread src/Shared/FrameworkLocationHelper.cs Resolved
@ladipro
ladipro self-requested a review January 27, 2022 08:47
@elachlan
elachlan requested a review from Forgind January 27, 2022 10:40

// 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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch!

@Forgind Forgind left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me!

Comment thread src/MSBuild/XMake.cs
/// </summary>
#pragma warning disable CA1810 // Initialize reference type static fields inline
static MSBuildApp()
#pragma warning restore CA1810 // Initialize reference type static fields inline

Copy link
Copy Markdown
Contributor

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