提交 7177e45f 编写于 作者: C Cyrus Najmabadi

Simplify null checks

上级 7c5642ec
......@@ -629,7 +629,7 @@ private static ImmutableArray<ISymbol> GetSymbols(ImmutableArray<Func<SemanticMo
private static Func<SemanticModel, INamedTypeSymbol> GetTypeSymbol(string typeMetadataName)
{
return s => s == null ? null : s.Compilation.GetTypeByMetadataName(typeMetadataName);
return s => s?.Compilation.GetTypeByMetadataName(typeMetadataName);
}
internal static IEnumerable<SyntaxToken> CreateModifierTokens(Editing.DeclarationModifiers modifiers, string language)
......@@ -781,7 +781,7 @@ internal class TestContext : IDisposable
public static async Task<TestContext> CreateAsync(string initial, string expected, string forceLanguage = null, bool ignoreResult = false)
{
var language = forceLanguage != null ? forceLanguage : GetLanguage(initial);
var language = forceLanguage ?? GetLanguage(initial);
var isVisualBasic = language == LanguageNames.VisualBasic;
var workspace = CreateWorkspaceFromFile(initial.NormalizeLineEndings(), isVisualBasic, null, null);
var semanticModel = await workspace.CurrentSolution.Projects.Single().Documents.Single().GetSemanticModelAsync();
......
......@@ -269,17 +269,12 @@ public override IEnumerable<SyntaxNode> GetOptionNodes(SyntaxNode parent)
}
public override IEnumerable<SyntaxNode> GetImportNodes(SyntaxNode parent)
{
switch (parent.Kind())
=> parent.Kind() switch
{
case SyntaxKind.CompilationUnit:
return ((CompilationUnitSyntax)parent).Usings;
case SyntaxKind.NamespaceDeclaration:
return ((NamespaceDeclarationSyntax)parent).Usings;
default:
return SpecializedCollections.EmptyEnumerable<SyntaxNode>();
}
}
SyntaxKind.CompilationUnit => ((CompilationUnitSyntax)parent).Usings,
SyntaxKind.NamespaceDeclaration => ((NamespaceDeclarationSyntax)parent).Usings,
_ => SpecializedCollections.EmptyEnumerable<SyntaxNode>(),
};
private static IEnumerable<SyntaxNode> GetAttributeNodes(SyntaxList<AttributeListSyntax> attributeDeclarationList)
{
......@@ -1507,21 +1502,14 @@ public override bool IsAccessorNode(SyntaxNode node)
}
public override MethodKind GetAccessorKind(SyntaxNode node)
{
switch (node.Kind())
=> node.Kind() switch
{
case SyntaxKind.GetAccessorDeclaration:
return MethodKind.PropertyGet;
case SyntaxKind.SetAccessorDeclaration:
return MethodKind.PropertySet;
case SyntaxKind.AddAccessorDeclaration:
return MethodKind.EventAdd;
case SyntaxKind.RemoveAccessorDeclaration:
return MethodKind.EventRemove;
default:
throw Exceptions.ThrowEUnexpected();
}
}
SyntaxKind.GetAccessorDeclaration => MethodKind.PropertyGet,
SyntaxKind.SetAccessorDeclaration => MethodKind.PropertySet,
SyntaxKind.AddAccessorDeclaration => MethodKind.EventAdd,
SyntaxKind.RemoveAccessorDeclaration => MethodKind.EventRemove,
_ => throw Exceptions.ThrowEUnexpected(),
};
private static SyntaxKind GetAccessorSyntaxKind(MethodKind methodKind)
{
......@@ -2919,23 +2907,15 @@ private Document DeleteMember(Document document, SyntaxNode node)
}
public override Document Delete(Document document, SyntaxNode node)
{
switch (node.Kind())
{
case SyntaxKind.VariableDeclarator:
return Delete(document, (VariableDeclaratorSyntax)node);
case SyntaxKind.EnumMemberDeclaration:
return Delete(document, (EnumMemberDeclarationSyntax)node);
case SyntaxKind.Attribute:
return Delete(document, (AttributeSyntax)node);
case SyntaxKind.AttributeArgument:
return Delete(document, (AttributeArgumentSyntax)node);
case SyntaxKind.Parameter:
return Delete(document, (ParameterSyntax)node);
default:
return DeleteMember(document, node);
}
}
=> node.Kind() switch
{
SyntaxKind.VariableDeclarator => Delete(document, (VariableDeclaratorSyntax)node),
SyntaxKind.EnumMemberDeclaration => Delete(document, (EnumMemberDeclarationSyntax)node),
SyntaxKind.Attribute => Delete(document, (AttributeSyntax)node),
SyntaxKind.AttributeArgument => Delete(document, (AttributeArgumentSyntax)node),
SyntaxKind.Parameter => Delete(document, (ParameterSyntax)node),
_ => DeleteMember(document, node),
};
public override string GetMethodXml(SyntaxNode node, SemanticModel semanticModel)
{
......
......@@ -2923,19 +2923,13 @@ private static SyntaxNode AddBaseList(SyntaxNode declaration, BaseListSyntax bas
}
private static BaseListSyntax GetBaseList(SyntaxNode declaration)
{
switch (declaration.Kind())
=> declaration.Kind() switch
{
case SyntaxKind.ClassDeclaration:
return ((ClassDeclarationSyntax)declaration).BaseList;
case SyntaxKind.StructDeclaration:
return ((StructDeclarationSyntax)declaration).BaseList;
case SyntaxKind.InterfaceDeclaration:
return ((InterfaceDeclarationSyntax)declaration).BaseList;
default:
return null;
}
}
SyntaxKind.ClassDeclaration => ((ClassDeclarationSyntax)declaration).BaseList,
SyntaxKind.StructDeclaration => ((StructDeclarationSyntax)declaration).BaseList,
SyntaxKind.InterfaceDeclaration => ((InterfaceDeclarationSyntax)declaration).BaseList,
_ => null,
};
private static SyntaxNode WithBaseList(SyntaxNode declaration, BaseListSyntax baseList)
=> declaration.Kind() switch
......
......@@ -287,29 +287,19 @@ public static bool IsCloseParenInStatement(this SyntaxToken token)
return false;
}
switch (statement)
{
case IfStatementSyntax ifStatement:
return ifStatement.CloseParenToken.Equals(token);
case SwitchStatementSyntax switchStatement:
return switchStatement.CloseParenToken.Equals(token);
case WhileStatementSyntax whileStatement:
return whileStatement.CloseParenToken.Equals(token);
case DoStatementSyntax doStatement:
return doStatement.CloseParenToken.Equals(token);
case ForStatementSyntax forStatement:
return forStatement.CloseParenToken.Equals(token);
case CommonForEachStatementSyntax foreachStatement:
return foreachStatement.CloseParenToken.Equals(token);
case LockStatementSyntax lockStatement:
return lockStatement.CloseParenToken.Equals(token);
case UsingStatementSyntax usingStatement:
return usingStatement.CloseParenToken.Equals(token);
case FixedStatementSyntax fixedStatement:
return fixedStatement.CloseParenToken.Equals(token);
}
return false;
return statement switch
{
IfStatementSyntax ifStatement => ifStatement.CloseParenToken.Equals(token),
SwitchStatementSyntax switchStatement => switchStatement.CloseParenToken.Equals(token),
WhileStatementSyntax whileStatement => whileStatement.CloseParenToken.Equals(token),
DoStatementSyntax doStatement => doStatement.CloseParenToken.Equals(token),
ForStatementSyntax forStatement => forStatement.CloseParenToken.Equals(token),
CommonForEachStatementSyntax foreachStatement => foreachStatement.CloseParenToken.Equals(token),
LockStatementSyntax lockStatement => lockStatement.CloseParenToken.Equals(token),
UsingStatementSyntax usingStatement => usingStatement.CloseParenToken.Equals(token),
FixedStatementSyntax fixedStatement => fixedStatement.CloseParenToken.Equals(token),
_ => false,
};
}
public static bool IsDotInMemberAccessOrQualifiedName(this SyntaxToken token)
......
......@@ -330,18 +330,12 @@ private void AddInitializerSuppressOperations(List<SuppressOperation> list, Synt
}
private InitializerExpressionSyntax GetInitializerNode(SyntaxNode node)
{
switch (node)
=> node switch
{
case ObjectCreationExpressionSyntax objectCreationNode:
return objectCreationNode.Initializer;
case ArrayCreationExpressionSyntax arrayCreationNode:
return arrayCreationNode.Initializer;
case ImplicitArrayCreationExpressionSyntax implicitArrayNode:
return implicitArrayNode.Initializer;
}
return null;
}
ObjectCreationExpressionSyntax objectCreationNode => objectCreationNode.Initializer,
ArrayCreationExpressionSyntax arrayCreationNode => arrayCreationNode.Initializer,
ImplicitArrayCreationExpressionSyntax implicitArrayNode => implicitArrayNode.Initializer,
_ => null,
};
}
}
......@@ -67,18 +67,13 @@ internal Option(string feature, OptionGroup group, string name, T defaultValue,
throw new ArgumentNullException(nameof(feature));
}
if (group == null)
{
throw new ArgumentNullException(nameof(group));
}
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(nameof(name));
}
this.Feature = feature;
this.Group = group;
this.Group = group ?? throw new ArgumentNullException(nameof(group));
this.Name = name;
this.DefaultValue = defaultValue;
this.StorageLocations = storageLocations.ToImmutableArray();
......
......@@ -62,18 +62,13 @@ internal PerLanguageOption(string feature, OptionGroup group, string name, T def
throw new ArgumentNullException(nameof(feature));
}
if (group == null)
{
throw new ArgumentNullException(nameof(group));
}
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(nameof(name));
}
this.Feature = feature;
this.Group = group;
this.Group = group ?? throw new ArgumentNullException(nameof(group));
this.Name = name;
this.DefaultValue = defaultValue;
this.StorageLocations = storageLocations.ToImmutableArray();
......
......@@ -348,7 +348,7 @@ public static NormalizedTextSpanCollection Difference(NormalizedTextSpanCollecti
return true;
}
if (object.ReferenceEquals(left, null) || object.ReferenceEquals(right, null))
if (left is null || right is null)
{
return false;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册