提交 1330b3fd 编写于 作者: C Cyrus Najmabadi

Use switch expression

上级 19261358
......@@ -505,15 +505,12 @@ public static bool SupportAnalysisKind(this DiagnosticAnalyzerService service, D
return true;
}
switch (kind)
return kind switch
{
case AnalysisKind.Syntax:
return analyzer.SupportsSyntaxDiagnosticAnalysis();
case AnalysisKind.Semantic:
return analyzer.SupportsSemanticDiagnosticAnalysis();
default:
return Contract.FailWithReturn<bool>("shouldn't reach here");
}
AnalysisKind.Syntax => analyzer.SupportsSyntaxDiagnosticAnalysis(),
AnalysisKind.Semantic => analyzer.SupportsSemanticDiagnosticAnalysis(),
_ => Contract.FailWithReturn<bool>("shouldn't reach here"),
};
}
public static async Task<IEnumerable<Diagnostic>> ComputeDocumentDiagnosticAnalyzerDiagnosticsAsync(
......@@ -529,21 +526,13 @@ public static bool SupportAnalysisKind(this DiagnosticAnalyzerService service, D
try
{
Task<ImmutableArray<Diagnostic>> analyzeAsync;
switch (kind)
var analyzeAsync = kind switch
{
case AnalysisKind.Syntax:
analyzeAsync = analyzer.AnalyzeSyntaxAsync(document, cancellationToken);
break;
case AnalysisKind.Semantic:
analyzeAsync = analyzer.AnalyzeSemanticsAsync(document, cancellationToken);
break;
default:
throw ExceptionUtilities.UnexpectedValue(kind);
}
AnalysisKind.Syntax => analyzer.AnalyzeSyntaxAsync(document, cancellationToken),
AnalysisKind.Semantic => analyzer.AnalyzeSemanticsAsync(document, cancellationToken),
_ => throw ExceptionUtilities.UnexpectedValue(kind),
};
var diagnostics = (await analyzeAsync.ConfigureAwait(false)).NullToEmpty();
if (compilationOpt != null)
......
......@@ -142,19 +142,13 @@ private static string CreateLogMessage<T>(T key, string kind)
}
private static string GetLogInfo<T>(T key)
{
switch (key)
=> key switch
{
case SolutionState solutionState:
return solutionState.FilePath;
case ProjectState projectState:
return projectState.FilePath;
case DocumentState documentState:
return documentState.FilePath;
}
return "no detail";
}
SolutionState solutionState => solutionState.FilePath,
ProjectState projectState => projectState.FilePath,
DocumentState documentState => documentState.FilePath,
_ => "no detail",
};
}
private struct SolutionChecksumFinder
......
......@@ -148,23 +148,13 @@ public override long Seek(long offset, SeekOrigin origin)
long target;
try
{
switch (origin)
target = origin switch
{
case SeekOrigin.Begin:
target = offset;
break;
case SeekOrigin.Current:
target = checked(offset + position);
break;
case SeekOrigin.End:
target = checked(offset + length);
break;
default:
throw new ArgumentOutOfRangeException(nameof(origin));
}
SeekOrigin.Begin => offset,
SeekOrigin.Current => checked(offset + position),
SeekOrigin.End => checked(offset + length),
_ => throw new ArgumentOutOfRangeException(nameof(origin)),
};
}
catch (OverflowException)
{
......
......@@ -106,31 +106,19 @@ private static Solution GetMultipleProjectSolution(params string[] sourceTexts)
}
private static Solution GetSolution(WorkspaceKind workspaceKind)
{
switch (workspaceKind)
=> workspaceKind switch
{
case WorkspaceKind.SingleClass:
return GetSingleProjectSolution(SingleClass);
case WorkspaceKind.SingleClassWithSingleMethod:
return GetSingleProjectSolution(SingleClassWithSingleMethod);
case WorkspaceKind.SingleClassWithSingleProperty:
return GetSingleProjectSolution(SingleClassWithSingleProperty);
case WorkspaceKind.SingleClassWithSingleField:
return GetSingleProjectSolution(SingleClassWithSingleField);
case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleMethod:
return GetMultipleProjectSolution(SingleClassWithSingleMethod, SingleClassWithSingleMethod);
case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleProperty:
return GetMultipleProjectSolution(SingleClassWithSingleProperty, SingleClassWithSingleProperty);
case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleField:
return GetMultipleProjectSolution(SingleClassWithSingleField, SingleClassWithSingleField);
case WorkspaceKind.NestedClass:
return GetSingleProjectSolution(NestedClass);
case WorkspaceKind.TwoNamespacesWithIdenticalClasses:
return GetSingleProjectSolution(Namespace1, Namespace2);
default:
return null;
}
}
WorkspaceKind.SingleClass => GetSingleProjectSolution(SingleClass),
WorkspaceKind.SingleClassWithSingleMethod => GetSingleProjectSolution(SingleClassWithSingleMethod),
WorkspaceKind.SingleClassWithSingleProperty => GetSingleProjectSolution(SingleClassWithSingleProperty),
WorkspaceKind.SingleClassWithSingleField => GetSingleProjectSolution(SingleClassWithSingleField),
WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleMethod => GetMultipleProjectSolution(SingleClassWithSingleMethod, SingleClassWithSingleMethod),
WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleProperty => GetMultipleProjectSolution(SingleClassWithSingleProperty, SingleClassWithSingleProperty),
WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleField => GetMultipleProjectSolution(SingleClassWithSingleField, SingleClassWithSingleField),
WorkspaceKind.NestedClass => GetSingleProjectSolution(NestedClass),
WorkspaceKind.TwoNamespacesWithIdenticalClasses => GetSingleProjectSolution(Namespace1, Namespace2),
_ => null,
};
private static Project GetProject(WorkspaceKind workspaceKind)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册