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

CA1065: Ignore System.Diagnostics.UnreachableException #7200

Merged
merged 1 commit into from
Feb 27, 2024
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -67,6 +67,7 @@ public override void Initialize(AnalysisContext context)
{
Compilation compilation = compilationStartContext.Compilation;
INamedTypeSymbol? exceptionType = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemException);
INamedTypeSymbol? unreachableExceptionType = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemDiagnosticsUnreachableException);
if (exceptionType == null)
{
return;
Expand Down Expand Up @@ -101,7 +102,9 @@ public override void Initialize(AnalysisContext context)
}

// Get ThrowOperation's ExceptionType
if (throwOperation.GetThrownExceptionType() is INamedTypeSymbol thrownExceptionType && thrownExceptionType.DerivesFrom(exceptionType))
if (throwOperation.GetThrownExceptionType() is INamedTypeSymbol thrownExceptionType &&
thrownExceptionType.DerivesFrom(exceptionType) &&
!SymbolEqualityComparer.Default.Equals(thrownExceptionType, unreachableExceptionType))
{
// If no exceptions are allowed or if the thrown exceptions is not an allowed one..
if (methodCategory.AllowedExceptions.IsEmpty || !methodCategory.AllowedExceptions.Any(n => thrownExceptionType.IsAssignableTo(n, compilation)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -744,6 +744,46 @@ End Class
return VerifyVB.VerifyAnalyzerAsync(code);
}

[Fact, WorkItem(6001, "https://github.com/dotnet/roslyn-analyzers/issues/6001")]
public Task UnreachableException_NoDiagnostic()
{
const string code = """
public class ShouldNotViolate
{
static ShouldNotViolate()
{
throw new System.Diagnostics.UnreachableException();
}
}
""";

return new VerifyCS.Test
{
TestCode = code,
FixedCode = code,
ReferenceAssemblies = ReferenceAssemblies.Net.Net70
}.RunAsync();
}

[Fact, WorkItem(6001, "https://github.com/dotnet/roslyn-analyzers/issues/6001")]
public Task VB_UnreachableException_NoDiagnostic()
{
const string code = """
Public Class ShouldNotViolate
Shared Sub New()
Throw New System.Diagnostics.UnreachableException()
End Sub
End Class
""";

return new VerifyVB.Test
{
TestCode = code,
FixedCode = code,
ReferenceAssemblies = ReferenceAssemblies.Net.Net70
}.RunAsync();
}

#endregion

#region Operator tests
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Compiler/WellKnownTypeNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ internal static class WellKnownTypeNames
public const string SystemDiagnosticsProcessStartInfo = "System.Diagnostics.ProcessStartInfo";
public const string SystemDiagnosticsTraceListener = "System.Diagnostics.TraceListener";
public const string SystemDiagnosticsTracingEventSource = "System.Diagnostics.Tracing.EventSource";
public const string SystemDiagnosticsUnreachableException = "System.Diagnostics.UnreachableException";
public const string SystemDirectoryDirectoryEntry = "System.DirectoryServices.DirectoryEntry";
public const string SystemDirectoryServicesActiveDirectoryADSearcher = "System.DirectoryServices.ActiveDirectory.ADSearcher";
public const string SystemDirectoryServicesDirectorySearcher = "System.DirectoryServices.DirectorySearcher";
Expand Down