From a2a60b42857507d2ae47f1645682fc7e4bc1d568 Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh Date: Wed, 13 Dec 2023 15:33:04 +0100 Subject: [PATCH] Change ErrorListHelper.TryGetValue to use casting instead of `is` comparison --- .../ErrorListHelperTests.cs | 15 ++++++++++--- src/Infrastructure.VS/ErrorListHelper.cs | 22 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/Infrastructure.VS.UnitTests/ErrorListHelperTests.cs b/src/Infrastructure.VS.UnitTests/ErrorListHelperTests.cs index 2a684ded06..460336c843 100644 --- a/src/Infrastructure.VS.UnitTests/ErrorListHelperTests.cs +++ b/src/Infrastructure.VS.UnitTests/ErrorListHelperTests.cs @@ -29,11 +29,20 @@ using Moq; using SonarLint.VisualStudio.IssueVisualization.Models; using SonarLint.VisualStudio.TestInfrastructure; +using static SonarLint.VisualStudio.Infrastructure.VS.UnitTests.ErrorListHelperTests; + namespace SonarLint.VisualStudio.Infrastructure.VS.UnitTests { [TestClass] public class ErrorListHelperTests { + internal enum TestVsSuppressionState + { + Active, + Suppressed, + NotApplicable, + } + [TestMethod] public void MefCtor_CheckIsExported() { @@ -407,9 +416,9 @@ public void TryGetRuleIdAndSuppressionStateFromSelectedRow_NoSuppressionState_Re } [DataTestMethod] - [DataRow(Infrastructure.VS.SuppressionState.SuppressedEnumValue, true)] - [DataRow(Infrastructure.VS.SuppressionState.NotApplicableEnumValue, false)] - [DataRow(Infrastructure.VS.SuppressionState.ActiveEnumValue, false)] + [DataRow(TestVsSuppressionState.Suppressed, true)] + [DataRow(TestVsSuppressionState.NotApplicable, false)] + [DataRow(TestVsSuppressionState.Active, false)] public void TryGetRuleIdAndSuppressionStateFromSelectedRow_NoSuppressionState_ReturnsIsNotSuppressed(int suppressionState, bool expectedSuppression) { // Arrange diff --git a/src/Infrastructure.VS/ErrorListHelper.cs b/src/Infrastructure.VS/ErrorListHelper.cs index aa566a6291..796e6d5a3d 100644 --- a/src/Infrastructure.VS/ErrorListHelper.cs +++ b/src/Infrastructure.VS/ErrorListHelper.cs @@ -18,6 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +using System; using System.ComponentModel.Composition; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; @@ -123,8 +124,8 @@ public bool TryGetRoslynIssueFromSelectedRow(out IFilterableRoslynIssue filterab private static bool IsSuppressed(ITableEntryHandle handle) { - return handle.TryGetSnapshot(out var snapshot, out var index) - && TryGetValue(snapshot, index, Infrastructure.VS.SuppressionState.ColumnName, out int suppressionState) + return handle.TryGetSnapshot(out var snapshot, out var index) + && TryGetValue(snapshot, index, Infrastructure.VS.SuppressionState.ColumnName, out int suppressionState) && suppressionState == Infrastructure.VS.SuppressionState.SuppressedEnumValue; } @@ -200,13 +201,22 @@ private static bool TryGetSelectedTableEntry(IErrorList errorList, out ITableEnt private static bool TryGetValue(ITableEntriesSnapshot snapshot, int index, string columnName, out T value) { value = default; - if (snapshot.TryGetValue(index, columnName, out var objValue) && objValue is T outValue) + + try { - value = outValue; + if (!snapshot.TryGetValue(index, columnName, out var objValue) || objValue == null) + { + return false; + } + + value = (T)objValue; return true; + + } + catch (InvalidCastException) + { + return false; } - - return false; } } }