Skip to content

Commit

Permalink
Change ErrorListHelper.TryGetValue to use casting instead of is com…
Browse files Browse the repository at this point in the history
…parison (#5109)
  • Loading branch information
georgii-borovinskikh-sonarsource authored Dec 13, 2023
1 parent 328711d commit b36761a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/Infrastructure.VS.UnitTests/ErrorListHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions src/Infrastructure.VS/ErrorListHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -200,13 +201,22 @@ private static bool TryGetSelectedTableEntry(IErrorList errorList, out ITableEnt
private static bool TryGetValue<T>(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;
}
}
}

0 comments on commit b36761a

Please sign in to comment.