diff --git a/src/Workspaces/Core/Portable/CodeStyle/CodeStyleOption.cs b/src/Workspaces/Core/Portable/CodeStyle/CodeStyleOption.cs index 0003cb8023b54..b5c8bba1c9102 100644 --- a/src/Workspaces/Core/Portable/CodeStyle/CodeStyleOption.cs +++ b/src/Workspaces/Core/Portable/CodeStyle/CodeStyleOption.cs @@ -25,7 +25,7 @@ internal CodeStyleOption(CodeStyleOption2 codeStyleOptionImpl) => _codeStyleOptionImpl = codeStyleOptionImpl; public CodeStyleOption(T value, NotificationOption notification) - : this(new CodeStyleOption2(value, (NotificationOption2)notification)) + : this(new CodeStyleOption2(value, new NotificationOption2(notification.Severity))) { } diff --git a/src/Workspaces/Core/Portable/CodeStyle/CodeStyleOption2_operators.cs b/src/Workspaces/Core/Portable/CodeStyle/CodeStyleOption2_operators.cs index 2e7879a923557..771f202d0c306 100644 --- a/src/Workspaces/Core/Portable/CodeStyle/CodeStyleOption2_operators.cs +++ b/src/Workspaces/Core/Portable/CodeStyle/CodeStyleOption2_operators.cs @@ -16,7 +16,7 @@ internal sealed partial class CodeStyleOption2 return null; } - return new CodeStyleOption(option.Value, (NotificationOption?)option.Notification); + return new CodeStyleOption(option.Value, (NotificationOption)option.Notification); } [return: NotNullIfNotNull("option")] diff --git a/src/Workspaces/Core/Portable/CodeStyle/NotificationOption2_operators.cs b/src/Workspaces/Core/Portable/CodeStyle/NotificationOption2_operators.cs index f1d083df55c7f..e0408ee654b37 100644 --- a/src/Workspaces/Core/Portable/CodeStyle/NotificationOption2_operators.cs +++ b/src/Workspaces/Core/Portable/CodeStyle/NotificationOption2_operators.cs @@ -2,41 +2,14 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Diagnostics.CodeAnalysis; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CodeStyle { - internal sealed partial class NotificationOption2 + internal readonly partial record struct NotificationOption2 { - [return: NotNullIfNotNull("notificationOption")] - public static explicit operator NotificationOption2?(NotificationOption? notificationOption) - { - if (notificationOption is null) - { - return null; - } - - return notificationOption.Severity switch - { - ReportDiagnostic.Suppress => None, - ReportDiagnostic.Hidden => Silent, - ReportDiagnostic.Info => Suggestion, - ReportDiagnostic.Warn => Warning, - ReportDiagnostic.Error => Error, - _ => throw ExceptionUtilities.UnexpectedValue(notificationOption.Severity), - }; - } - - [return: NotNullIfNotNull("notificationOption")] - public static explicit operator NotificationOption?(NotificationOption2? notificationOption) - { - if (notificationOption is null) - { - return null; - } - - return notificationOption.Severity switch + public static explicit operator NotificationOption(NotificationOption2 notificationOption) + => notificationOption.Severity switch { ReportDiagnostic.Suppress => NotificationOption.None, ReportDiagnostic.Hidden => NotificationOption.Silent, @@ -45,6 +18,5 @@ internal sealed partial class NotificationOption2 ReportDiagnostic.Error => NotificationOption.Error, _ => throw ExceptionUtilities.UnexpectedValue(notificationOption.Severity), }; - } } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleHelpers.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleHelpers.cs index 2726e0a2d89d8..e7f53a264823b 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleHelpers.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleHelpers.cs @@ -59,7 +59,7 @@ public static bool TryGetCodeStyleValue( /// a NotificationOption, so will default to . /// public static bool TryGetCodeStyleValueAndOptionalNotification( - string arg, NotificationOption2 defaultNotification, [NotNullWhen(true)] out string? value, [NotNullWhen(true)] out NotificationOption2? notification) + string arg, NotificationOption2 defaultNotification, [NotNullWhen(true)] out string? value, [NotNullWhen(true)] out NotificationOption2 notification) { var args = arg.Split(':'); Debug.Assert(args.Length > 0); @@ -86,7 +86,7 @@ public static bool TryGetCodeStyleValueAndOptionalNotification( // We only support 0 or 1 args. Anything else can't be parsed properly. value = null; - notification = null; + notification = default; return false; } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleOption2`1.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleOption2`1.cs index 42207019791e7..c40d2ff605060 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleOption2`1.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleOption2`1.cs @@ -53,7 +53,7 @@ static CodeStyleOption2() public CodeStyleOption2(T value, NotificationOption2 notification) { Value = value; - _notification = notification ?? throw new ArgumentNullException(nameof(notification)); + _notification = notification; } public T Value { get; } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleOptions2.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleOptions2.cs index 5970fc48fd2c3..f7d068d7c7836 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleOptions2.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/CodeStyleOptions2.cs @@ -309,13 +309,6 @@ private static CodeStyleOption2 ParseAccessibili private static string GetAccessibilityModifiersRequiredEditorConfigString(CodeStyleOption2 option, CodeStyleOption2 defaultValue) { - // If they provide 'never', they don't need a notification level. - if (option.Notification == null) - { - Debug.Assert(s_accessibilityModifiersRequiredMap.ContainsValue(AccessibilityModifiersRequired.Never)); - return s_accessibilityModifiersRequiredMap.GetKeyOrDefault(AccessibilityModifiersRequired.Never)!; - } - Debug.Assert(s_accessibilityModifiersRequiredMap.ContainsValue(option.Value)); return $"{s_accessibilityModifiersRequiredMap.GetKeyOrDefault(option.Value)}{GetEditorConfigStringNotificationPart(option, defaultValue)}"; } @@ -425,7 +418,7 @@ private static string GetParenthesesPreferenceEditorConfigString(CodeStyleOption { Debug.Assert(s_parenthesesPreferenceMap.ContainsValue(option.Value)); var value = s_parenthesesPreferenceMap.GetKeyOrDefault(option.Value) ?? s_parenthesesPreferenceMap.GetKeyOrDefault(ParenthesesPreference.AlwaysForClarity); - return option.Notification == null ? value! : $"{value}{GetEditorConfigStringNotificationPart(option, defaultValue)}"; + return $"{value}{GetEditorConfigStringNotificationPart(option, defaultValue)}"; } private static CodeStyleOption2 ParseUnusedParametersPreference(string optionString, CodeStyleOption2 defaultValue) @@ -443,7 +436,7 @@ private static string GetUnusedParametersPreferenceEditorConfigString(CodeStyleO { Debug.Assert(s_unusedParametersPreferenceMap.ContainsValue(option.Value)); var value = s_unusedParametersPreferenceMap.GetKeyOrDefault(option.Value) ?? s_unusedParametersPreferenceMap.GetKeyOrDefault(defaultValue.Value); - return option.Notification == null ? value! : $"{value}{GetEditorConfigStringNotificationPart(option, defaultValue)}"; + return $"{value}{GetEditorConfigStringNotificationPart(option, defaultValue)}"; } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/NotificationOption2.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/NotificationOption2.cs index 022bb5480e878..daf32c5d62b3a 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/NotificationOption2.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CodeStyle/NotificationOption2.cs @@ -2,11 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; - -#if CODE_STYLE -using WorkspacesResources = Microsoft.CodeAnalysis.CodeStyleResources; -#endif +using System.Runtime.Serialization; namespace Microsoft.CodeAnalysis.CodeStyle { @@ -14,54 +10,34 @@ namespace Microsoft.CodeAnalysis.CodeStyle /// Offers different notification styles for enforcing /// a code style. Under the hood, it simply maps to /// - /// - /// This also supports various properties for databinding. - /// /// - internal sealed partial class NotificationOption2 : IEquatable + [DataContract] + internal readonly partial record struct NotificationOption2( + [property: DataMember(Order = 0)] ReportDiagnostic Severity) { /// /// Notification option to disable or suppress an option with . /// - public static readonly NotificationOption2 None = new(ReportDiagnostic.Suppress); + public static NotificationOption2 None => new(ReportDiagnostic.Suppress); /// /// Notification option for a silent or hidden option with . /// - public static readonly NotificationOption2 Silent = new(ReportDiagnostic.Hidden); + public static NotificationOption2 Silent => new(ReportDiagnostic.Hidden); /// /// Notification option for a suggestion or an info option with . /// - public static readonly NotificationOption2 Suggestion = new(ReportDiagnostic.Info); + public static NotificationOption2 Suggestion => new(ReportDiagnostic.Info); /// /// Notification option for a warning option with . /// - public static readonly NotificationOption2 Warning = new(ReportDiagnostic.Warn); + public static NotificationOption2 Warning => new(ReportDiagnostic.Warn); /// /// Notification option for an error option with . /// - public static readonly NotificationOption2 Error = new(ReportDiagnostic.Error); - - /// - /// Diagnostic severity associated with notification option. - /// - public ReportDiagnostic Severity { get; } - - private NotificationOption2(ReportDiagnostic severity) - { - Severity = severity; - } - - public override bool Equals(object? obj) - => obj is NotificationOption2 other && Equals(other); - - public bool Equals(NotificationOption2? other) - => other != null && Severity == other.Severity; - - public override int GetHashCode() - => Severity.GetHashCode(); + public static NotificationOption2 Error => new(ReportDiagnostic.Error); } }