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

Enable and refactor to fix IDE0031 Use null propagation #7902

Merged
merged 1 commit into from
Oct 12, 2022

Conversation

elachlan
Copy link
Contributor

@elachlan elachlan commented Oct 6, 2022

Used the build in Analyzer's code fix.
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0031

The analyzer currently is broken and doesn't take into account #if DEBUG statements.
Issue: dotnet/roslyn#65880

Related: #7887

Microsoft Reviewers: Open in CodeFlow

@elachlan elachlan requested a review from a team as a code owner October 6, 2022 23:07
@ghost ghost assigned elachlan Oct 6, 2022
@elachlan
Copy link
Contributor Author

elachlan commented Oct 6, 2022

IDE0031 seems to not take into account #if DEBUG statements.

RussKie
RussKie previously approved these changes Oct 6, 2022
@RussKie
Copy link
Member

RussKie commented Oct 6, 2022

Thank you. Just to confirm, are there any manual changes or all changes are done by the auto fixers?

@RussKie
Copy link
Member

RussKie commented Oct 6, 2022

The build is failing.

@elachlan
Copy link
Contributor Author

elachlan commented Oct 6, 2022

The build is failing.

The analyzer is broken and doesn't take into account #if DEBUG statements. I am looking for where to raise an issue for it.

@elachlan elachlan marked this pull request as draft October 6, 2022 23:39
@ghost ghost added the draft draft PR label Oct 7, 2022
@elachlan
Copy link
Contributor Author

elachlan commented Oct 10, 2022

@RussKie could we remove the #if DEBUG statements here? Does Debug.Assert generate IL on release builds?

@elachlan
Copy link
Contributor Author

@RussKie could we remove the #if DEBUG statements here? Does Debug.Assert generate IL on release builds?

I just checked on sharplab.io, release builds remove the call to Debug.Assert so #if DEBUG is redundant. Unless I am missing some sort of config difference.

@RussKie
Copy link
Member

RussKie commented Oct 11, 2022

No, we can't simply remove #if DEBUG - some of those guard more than just Debug.Assert calls. The simplest thing to do is to suppress IDE0031 in these specific cases, e.g.

diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
index 4d3f98914..6e4d8a2c5 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
@@ -10915,6 +10915,7 @@ namespace System.Windows.Forms
 #if DEBUG
             int suspendCount = -44371;      // Arbitrary negative prime to surface bugs.
 #endif
+#pragma warning disable IDE0031 // Use null propagation
             if (ParentInternal is not null)
             {
 #if DEBUG
@@ -10922,6 +10923,7 @@ namespace System.Windows.Forms
 #endif
                 ParentInternal.SuspendLayout();
             }
+#pragma warning restore IDE0031 // Use null propagation
 
             try
             {

@RussKie RussKie added waiting-author-feedback The team requires more information from the author code cleanup cleanup code for unused apis/properties/comments - no functional changes. labels Oct 11, 2022
@RussKie
Copy link
Member

RussKie commented Oct 11, 2022

@elachlan
Copy link
Contributor Author

Does Debug.Assert generate IL on release builds?

I don't think so, https://sharplab.io/#v2:C4LghgzgtgPgAgJgIwFgBQcAMACOSB0AIgJZgDmAdgPYTDEDGEA3OunAMy4LYDC2A3umzDcnOABZsAWQAUAIypUANtgBmASgFCROwgFM5AVzL4AghAh6ATsBmqANNgBEcpWCfqWaHQF90PoA

In cases where an #if DEBUG is used to guard only Debug.Assert, can I remove the #if DEBUG?

@ghost ghost removed the waiting-author-feedback The team requires more information from the author label Oct 11, 2022
@RussKie
Copy link
Member

RussKie commented Oct 11, 2022

Yes, but you may find that there are variables that are defined in DEBUG only, e.g., in TransactionLayout class.

@RussKie RussKie added the waiting-author-feedback The team requires more information from the author label Oct 11, 2022
@elachlan
Copy link
Contributor Author

Yes, but you may find that there are variables that are defined in DEBUG only, e.g., in TransactionLayout class.

Thanks. I will add the supressions and remove any #if DEBUG guards if it makes sense.

@ghost ghost removed the waiting-author-feedback The team requires more information from the author label Oct 11, 2022
@elachlan elachlan force-pushed the IDE0031 branch 2 times, most recently from f8d1444 to a0a4c80 Compare October 11, 2022 04:24
@elachlan elachlan marked this pull request as ready for review October 11, 2022 04:50
@elachlan elachlan requested a review from RussKie October 11, 2022 05:09
@RussKie RussKie added waiting-review This item is waiting on review by one or more members of team and removed draft draft PR labels Oct 11, 2022
@RussKie
Copy link
Member

RussKie commented Oct 12, 2022

If I may ask you to consider adding fixup commits or just additional commits to ease the reviews whenever the changeset are this large. F-push makes it very hard to appreciate what changed.
We generally squash merge changes in this repo.
Thanks

@RussKie RussKie removed the waiting-review This item is waiting on review by one or more members of team label Oct 12, 2022
@RussKie RussKie merged commit 78bf439 into dotnet:main Oct 12, 2022
@ghost ghost added this to the 8.0 Preview1 milestone Oct 12, 2022
@elachlan
Copy link
Contributor Author

If I may ask you to consider adding fixup commits or just additional commits to ease the reviews whenever the changeset are this large. F-push makes it very hard to appreciate what changed. We generally squash merge changes in this repo. Thanks

Not a problem! I will do so in the future, I wasn't sure if you did squash merges and didn't want to pollute the main branch.

@elachlan elachlan deleted the IDE0031 branch October 12, 2022 06:18
@RussKie
Copy link
Member

RussKie commented Oct 12, 2022

Thank you for the consideration 👍

v-elnovikova pushed a commit to v-elnovikova/winforms that referenced this pull request Oct 18, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
code cleanup cleanup code for unused apis/properties/comments - no functional changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants