Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ErrorListHelper.TryGetValue to use casting instead of is comparison #5109

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}