Skip to content

Enable nullability in KeysConverter#6448

Merged
dreddy-work merged 9 commits intodotnet:mainfrom
gpetrou:KeysConverter
Feb 28, 2022
Merged

Enable nullability in KeysConverter#6448
dreddy-work merged 9 commits intodotnet:mainfrom
gpetrou:KeysConverter

Conversation

@gpetrou
Copy link
Copy Markdown
Contributor

@gpetrou gpetrou commented Dec 31, 2021

Proposed changes

  • Enable nullability in KeysConverter.
Microsoft Reviewers: Open in CodeFlow

@gpetrou gpetrou requested a review from a team as a code owner December 31, 2021 09:23
@ghost ghost assigned gpetrou Dec 31, 2021
@gpetrou gpetrou force-pushed the KeysConverter branch 3 times, most recently from 46e1946 to a8526b9 Compare January 2, 2022 15:55
@dreddy-work dreddy-work added the waiting-author-feedback The team requires more information from the author label Jan 3, 2022
for (int i = 0; i < tokens.Length; i++)
{
object obj = KeyNames[tokens[i]];
object obj = KeyNames[tokens[i]]!;
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.

I don't think it is - private IDictionary KeyNames. Instead of using ! I think we should change the signature of KeyNames to private IDictionary<string, Keys> KeyNames.

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.

I do not have practical example here but following code was written with intention that obj could be null.

Copy link
Copy Markdown
Contributor

@RussKie RussKie Feb 20, 2022

Choose a reason for hiding this comment

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

I followed the code and I can't see how we can have null in the dictionary. Maybe I'm missing some flow...
With this change, the value is a Keys enum, which can never be null. So the null-check clause below should be removed.

@dreddy-work
Copy link
Copy Markdown
Member

@gpetrou , can you rebase here?

@ghost ghost removed the waiting-author-feedback The team requires more information from the author label Feb 20, 2022
Copy link
Copy Markdown
Contributor

@RussKie RussKie left a comment

Choose a reason for hiding this comment

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

We should avoid bang operators, and in this case I do not see them as appropriate.
I believe it should be annotated like this:

diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs
index 024ab7ee1..dd124b10c 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs
@@ -21,12 +21,6 @@ namespace System.Windows.Forms
         private List<string>? _displayOrder;
         private StandardValuesCollection? _values;
 
-        private void AddKey(string key, Keys value)
-        {
-            _keyNames![key] = value;
-            _displayOrder!.Add(key);
-        }
-
         [MemberNotNull(nameof(_keyNames))]
         [MemberNotNull(nameof(_displayOrder))]
         private void Initialize()
@@ -71,12 +65,20 @@ namespace System.Windows.Forms
             AddKey("7", Keys.D7);
             AddKey("8", Keys.D8);
             AddKey("9", Keys.D9);
+
+            void AddKey(string key, Keys value)
+            {
+                _keyNames[key] = value;
+                _displayOrder!.Add(key);
+            }
         }
 
         /// <summary>
         ///  Access to a lookup table of name/value pairs for keys.  These are localized
         ///  names.
         /// </summary>
+        [MemberNotNull(nameof(_keyNames))]
+        [MemberNotNull(nameof(_displayOrder))]
         private IDictionary<string, Keys> KeyNames
         {
             get
@@ -87,10 +89,14 @@ namespace System.Windows.Forms
                     Initialize();
                 }
 
+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
                 return _keyNames;
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.
             }
         }
 
+        [MemberNotNull(nameof(_keyNames))]
+        [MemberNotNull(nameof(_displayOrder))]
         private List<string> DisplayOrder
         {
             get
@@ -101,7 +107,9 @@ namespace System.Windows.Forms
                     Initialize();
                 }
 
+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
                 return _displayOrder;
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.
             }
         }
 
@@ -255,7 +263,7 @@ namespace System.Windows.Forms
                     for (int i = 0; i < DisplayOrder.Count; i++)
                     {
                         string keyString = DisplayOrder[i];
-                        Keys keyValue = _keyNames![keyString];
+                        Keys keyValue = _keyNames[keyString];
                         if (((int)keyValue & (int)modifiers) != 0)
                         {
                             if (asString)
@@ -289,7 +297,7 @@ namespace System.Windows.Forms
                     for (int i = 0; i < DisplayOrder.Count; i++)
                     {
                         string keyString = DisplayOrder[i];
-                        Keys keyValue = _keyNames![keyString];
+                        Keys keyValue = _keyNames[keyString];
                         if (keyValue.Equals(keyOnly))
                         {
                             if (asString)

@RussKie RussKie added the waiting-author-feedback The team requires more information from the author label Feb 20, 2022
@ghost ghost removed the waiting-author-feedback The team requires more information from the author label Feb 21, 2022
Copy link
Copy Markdown
Contributor

@RussKie RussKie left a comment

Choose a reason for hiding this comment

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

👍

@RussKie RussKie added the waiting-author-feedback The team requires more information from the author label Feb 21, 2022
@ghost ghost removed the waiting-author-feedback The team requires more information from the author label Feb 23, 2022
@dreddy-work dreddy-work requested a review from RussKie February 24, 2022 17:58
@gpetrou gpetrou requested a review from dreddy-work February 27, 2022 08:49
@dreddy-work dreddy-work added the ready-to-merge PRs that are ready to merge but worth notifying the internal team. label Feb 28, 2022
@dreddy-work dreddy-work merged commit 156616a into dotnet:main Feb 28, 2022
@ghost ghost added this to the 7.0 Preview3 milestone Feb 28, 2022
@RussKie RussKie removed the ready-to-merge PRs that are ready to merge but worth notifying the internal team. label Feb 28, 2022
@gpetrou gpetrou deleted the KeysConverter branch March 1, 2022 06:14
@ghost ghost locked as resolved and limited conversation to collaborators Mar 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants