Enable nullability in KeysConverter#6448
Conversation
46e1946 to
a8526b9
Compare
| for (int i = 0; i < tokens.Length; i++) | ||
| { | ||
| object obj = KeyNames[tokens[i]]; | ||
| object obj = KeyNames[tokens[i]]!; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I do not have practical example here but following code was written with intention that obj could be null.
There was a problem hiding this comment.
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.
|
@gpetrou , can you rebase here? |
a8526b9 to
6dea2db
Compare
RussKie
left a comment
There was a problem hiding this comment.
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)6dea2db to
387ba86
Compare
src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs
Outdated
Show resolved
Hide resolved
387ba86 to
96add00
Compare
Proposed changes
Microsoft Reviewers: Open in CodeFlow