Skip to content

Commit

Permalink
C# 8 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jan 17, 2020
1 parent a37a174 commit c2635ad
Show file tree
Hide file tree
Showing 22 changed files with 60 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"cSpell.words": [
"LLMBRO",
"MBRO",
"app",
"nuget",
"nupkg",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,11 @@ private static ArgumentListSyntax GetArguments(InvocationExpressionSyntax invoca
{
var arguments = invocation.ArgumentList;
var argumentSyntax = invocation.ArgumentList.Arguments.Last();
var lambdaExpression = argumentSyntax.Expression as LambdaExpressionSyntax;

if (lambdaExpression == null)
if (!(argumentSyntax.Expression is LambdaExpressionSyntax lambdaExpression))
return arguments;

var awaitExpression = lambdaExpression.Body as AwaitExpressionSyntax;
if (awaitExpression == null)
if (!(lambdaExpression.Body is AwaitExpressionSyntax awaitExpression))
return arguments;

var lambdaExpressionWithoutAsyncKeyword = RemoveAsyncKeywordFromLambdaExpression(lambdaExpression, awaitExpression);
Expand All @@ -132,12 +130,12 @@ private static ExpressionSyntax RemoveAsyncKeywordFromLambdaExpression(LambdaExp
{
if (lambdaExpression is SimpleLambdaExpressionSyntax simpleLambdaExpression)
return simpleLambdaExpression.ReplaceNode(awaitExpression, awaitExpression.Expression)
.WithAsyncKeyword(default(SyntaxToken))
.WithAsyncKeyword(default)
.WithLeadingTrivia(simpleLambdaExpression.AsyncKeyword.LeadingTrivia);

if (lambdaExpression is ParenthesizedLambdaExpressionSyntax parenthesizedLambdaExpression)
return parenthesizedLambdaExpression.ReplaceNode(awaitExpression, awaitExpression.Expression)
.WithAsyncKeyword(default(SyntaxToken))
.WithAsyncKeyword(default)
.WithLeadingTrivia(parenthesizedLambdaExpression.AsyncKeyword.LeadingTrivia);

return lambdaExpression;
Expand Down
2 changes: 1 addition & 1 deletion src/xunit.analyzers.fixes/CodeActions/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public static async Task<Document> SetBaseClass(Document document, ClassDeclarat
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
var baseTypeNode = generator.TypeExpression(semanticModel.Compilation.GetTypeByMetadataName(baseType));
var baseTypes = generator.GetBaseAndInterfaceTypes(declaration);
var updatedDeclaration = default(SyntaxNode);

SyntaxNode updatedDeclaration;
if (baseTypes?.Count == 0 || semanticModel.GetTypeInfo(baseTypes[0], cancellationToken).Type?.TypeKind != TypeKind.Class)
updatedDeclaration = generator.AddBaseType(declaration, baseTypeNode);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var syntaxNode = root.FindNode(context.Span);
var invocation = syntaxNode.FirstAncestorOrSelf<InvocationExpressionSyntax>();

var typeOfExpression = invocation.ArgumentList.Arguments[0].Expression as TypeOfExpressionSyntax;
if (typeOfExpression == null)
if (!(invocation.ArgumentList.Arguments[0].Expression is TypeOfExpressionSyntax typeOfExpression))
return;

var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
if (memberAccess == null)
if (!(invocation.Expression is MemberAccessExpressionSyntax memberAccess))
return;

var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
if (method.Parameters.Length > 1 && method.Parameters[1].Type.SpecialType.Equals(SpecialType.System_String))
return;

var invocationExpression = arguments.First().Expression as InvocationExpressionSyntax;
if (invocationExpression == null)
if (!(arguments.First().Expression is InvocationExpressionSyntax invocationExpression))
return;

var symbolInfo = context.SemanticModel.GetSymbolInfo(invocationExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
if (arguments.Count != 1)
return;

var invocationExpression = arguments.First().Expression as InvocationExpressionSyntax;
if (invocationExpression == null)
if (!(arguments.First().Expression is InvocationExpressionSyntax invocationExpression))
return;

var symbolInfo = context.SemanticModel.GetSymbolInfo(invocationExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
!method.TypeArguments[0].SpecialType.Equals(SpecialType.System_Boolean))
return;

var literalFirstArgument = arguments.First().Expression as LiteralExpressionSyntax;
if (literalFirstArgument == null)
if (!(arguments.First().Expression is LiteralExpressionSyntax literalFirstArgument))
return;

var isTrue = literalFirstArgument.IsKind(SyntaxKind.TrueLiteralExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public AssertIsTypeShouldNotBeUsedForAbstractType()

protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, IMethodSymbol method)
{
var genericName = invocation.GetSimpleName() as GenericNameSyntax;
if (genericName == null)
if (!(invocation.GetSimpleName() is GenericNameSyntax genericName))
return;

var typeSyntax = genericName.TypeArgumentList.Arguments[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
if (arguments.Count != 2)
return;

var typeOfExpression = arguments[0].Expression as TypeOfExpressionSyntax;
if (typeOfExpression == null)
if (!(arguments[0].Expression is TypeOfExpressionSyntax typeOfExpression))
return;

var typeInfo = context.SemanticModel.GetTypeInfo(typeOfExpression.Type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
if (arguments.Count != 1)
return;

var invocationExpression = arguments.First().Expression as InvocationExpressionSyntax;
if (invocationExpression == null)
if (!(arguments.First().Expression is InvocationExpressionSyntax invocationExpression))
return;

var symbolInfo = context.SemanticModel.GetSymbolInfo(invocationExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
if (arguments.Count != 1)
return;

var invocationExpression = arguments.First().Expression as InvocationExpressionSyntax;
if (invocationExpression == null)
if (!(arguments.First().Expression is InvocationExpressionSyntax invocationExpression))
return;

var symbolInfo = context.SemanticModel.GetSymbolInfo(invocationExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
if (arguments.Count != 1)
return;

var invocationExpression = arguments.First().Expression as InvocationExpressionSyntax;
if (invocationExpression == null)
if (!(arguments.First().Expression is InvocationExpressionSyntax invocationExpression))
return;

var symbolInfo = context.SemanticModel.GetSymbolInfo(invocationExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
private static SymbolInfo GetThrowExpressionSymbol(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation)
{
var argumentExpression = invocation.ArgumentList.Arguments.Last().Expression;
var lambdaExpression = argumentExpression as LambdaExpressionSyntax;

if (lambdaExpression == null)
if (!(argumentExpression is LambdaExpressionSyntax lambdaExpression))
return context.SemanticModel.GetSymbolInfo(argumentExpression);

var awaitExpression = lambdaExpression.Body as AwaitExpressionSyntax;
if (awaitExpression == null)
if (!(lambdaExpression.Body is AwaitExpressionSyntax awaitExpression))
return context.SemanticModel.GetSymbolInfo(lambdaExpression.Body);

return context.SemanticModel.GetSymbolInfo(awaitExpression.Expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExp
if (arguments.Count != 2)
return;

var typeOfExpression = arguments[0].Expression as TypeOfExpressionSyntax;
if (typeOfExpression == null)
if (!(arguments[0].Expression is TypeOfExpressionSyntax typeOfExpression))
return;

var typeInfo = context.SemanticModel.GetTypeInfo(typeOfExpression.Type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ internal override void AnalyzeCompilation(CompilationStartAnalysisContext compil
if (!Equals(semanticModel.GetTypeInfo(attribute).Type, xunitContext.Core.ClassDataAttributeType))
return;

var argumentExpression = attribute.ArgumentList?.Arguments.FirstOrDefault()?.Expression as TypeOfExpressionSyntax;
if (argumentExpression == null)
if (!(attribute.ArgumentList?.Arguments.FirstOrDefault()?.Expression is TypeOfExpressionSyntax argumentExpression))
return;

var classType = (INamedTypeSymbol)semanticModel.GetTypeInfo(argumentExpression.Type).Type;
Expand Down
28 changes: 9 additions & 19 deletions src/xunit.analyzers/InlineDataShouldBeUniqueWithinTheory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,11 @@ private bool AreArgumentsEqual(ImmutableArray<object> xArguments, ImmutableArray
break;

case TypedConstant xArgArray when xArgArray.Kind == TypedConstantKind.Array && !xArgArray.IsNull:
switch (y)
return y switch
{
case TypedConstant yArgArray when yArgArray.Kind == TypedConstantKind.Array:
return AreArgumentsEqual(
xArgArray.Values.Cast<object>().ToImmutableArray(),
yArgArray.Values.Cast<object>().ToImmutableArray());

default:
return false;
}

TypedConstant yArgArray when yArgArray.Kind == TypedConstantKind.Array => AreArgumentsEqual(xArgArray.Values.Cast<object>().ToImmutableArray(), yArgArray.Values.Cast<object>().ToImmutableArray()),
_ => false,
};
default:
return false;
}
Expand All @@ -174,16 +168,12 @@ private static bool IsSingleNullByInlineDataOrByDefaultParamValue(ImmutableArray
if (args.Length != 1)
return false;

switch (args[0])
return (args[0]) switch
{
case TypedConstant xSingleNull when xSingleNull.Kind == TypedConstantKind.Array && xSingleNull.IsNull:
return true;

case IParameterSymbol xParamDefaultNull when xParamDefaultNull.ExplicitDefaultValue == null:
return true;
}

return false;
TypedConstant xSingleNull when xSingleNull.Kind == TypedConstantKind.Array && xSingleNull.IsNull => true,
IParameterSymbol xParamDefaultNull when xParamDefaultNull.ExplicitDefaultValue == null => true,
_ => false,
};
}

/// <summary>
Expand Down
15 changes: 7 additions & 8 deletions src/xunit.analyzers/MemberDataShouldReferenceValidMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ compilation is CSharpCompilation cSharpCompilation
return;

var constantValue = semanticModel.GetConstantValue(memberNameArgument.Expression, symbolContext.CancellationToken);
var memberName = constantValue.Value as string;
if (memberName == null)
if (!(constantValue.Value is string memberName))
return;

var memberTypeArgument = attribute.ArgumentList.Arguments.FirstOrDefault(a => a.NameEquals?.Name.Identifier.ValueText == "MemberType");
Expand Down Expand Up @@ -162,13 +161,13 @@ compilation is CSharpCompilation cSharpCompilation

static ITypeSymbol GetMemberType(ISymbol memberSymbol)
{
switch (memberSymbol)
return memberSymbol switch
{
case IPropertySymbol prop: return prop.Type;
case IFieldSymbol field: return field.Type;
case IMethodSymbol method: return method.ReturnType;
default: return null;
}
IPropertySymbol prop => prop.Type,
IFieldSymbol field => field.Type,
IMethodSymbol method => method.ReturnType,
_ => null,
};
}

static ISymbol FindMemberSymbol(string memberName, ITypeSymbol type)
Expand Down
14 changes: 5 additions & 9 deletions src/xunit.analyzers/SyntaxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ internal static bool ContainsAttributeType(this SyntaxList<AttributeListSyntax>

internal static SimpleNameSyntax GetSimpleName(this InvocationExpressionSyntax invocation)
{
switch (invocation.Expression)
return invocation.Expression switch
{
case MemberAccessExpressionSyntax memberAccess:
return memberAccess.Name;

case SimpleNameSyntax simpleName:
return simpleName;
}

return null;
MemberAccessExpressionSyntax memberAccess => memberAccess.Name,
SimpleNameSyntax simpleName => simpleName,
_ => null,
};
}

internal static bool IsEnumValueExpression(this ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken = default(CancellationToken))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async void DoesNotFindErrorForFactMethodWithNoDataAttributes()
[InlineData("ClassData(typeof(string))")]
public async void DoesNotFindErrorForTheoryMethodWithDataAttributes(string dataAttribute)
{
var source = "public class TestClass { [Xunit.Theory, Xunit." + dataAttribute + "] public void TestMethod() { } }";
var source = "public class TestClass { [Xunit.Theory, Xunit." + dataAttribute + "] public void TestMethod() { } }";

await Verify.VerifyAnalyzerAsync(source);
}
Expand Down Expand Up @@ -48,7 +48,7 @@ public async void DoesNotFindErrorForDerivedFactMethodWithDataAttributes(string
[InlineData("ClassData(typeof(string))")]
public async void FindsErrorForFactMethodsWithDataAttributes(string dataAttribute)
{
var source = "public class TestClass { [Xunit.Fact, Xunit." + dataAttribute + "] public void TestMethod() { } }";
var source = "public class TestClass { [Xunit.Fact, Xunit." + dataAttribute + "] public void TestMethod() { } }";

var expected = Verify.Diagnostic().WithSpan(1, 59 + dataAttribute.Length, 1, 69 + dataAttribute.Length);
await Verify.VerifyAnalyzerAsync(source, expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ForNonRelatedToInlineDataMethod : InlineDataShouldBeUniqueWithinThe
[Fact]
public async void DoesNotFindError_WhenNoDataAttributes()
{
var source = "public class TestClass { [Xunit.Fact] public void TestMethod() { } }";
var source = "public class TestClass { [Xunit.Fact] public void TestMethod() { } }";

await Verify.VerifyAnalyzerAsync(source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class OtherClass { public static System.Collections.Generic.IEnumerable<o
[Fact]
public async void DoesNotFindError_ForNameofOnSameClass()
{
var source = "public partial class TestClass { [Xunit.MemberData(nameof(Data))] public void TestMethod() { } }";
var source = "public partial class TestClass { [Xunit.MemberData(nameof(Data))] public void TestMethod() { } }";

await new Verify.Test
{
Expand All @@ -25,7 +25,7 @@ public async void DoesNotFindError_ForNameofOnSameClass()
[Fact]
public async void DoesNotFindError_ForNameofOnOtherClass()
{
var source = "public partial class TestClass { [Xunit.MemberData(nameof(OtherClass.OtherData), MemberType = typeof(OtherClass))] public void TestMethod() { } }";
var source = "public partial class TestClass { [Xunit.MemberData(nameof(OtherClass.OtherData), MemberType = typeof(OtherClass))] public void TestMethod() { } }";

await new Verify.Test
{
Expand All @@ -36,7 +36,7 @@ public async void DoesNotFindError_ForNameofOnOtherClass()
[Fact]
public async void FindsError_ForStringReferenceOnSameClass()
{
var source = "public partial class TestClass { [Xunit.MemberData(\"Data\")] public void TestMethod() { } }";
var source = "public partial class TestClass { [Xunit.MemberData(\"Data\")] public void TestMethod() { } }";

await new Verify.Test
{
Expand All @@ -51,7 +51,7 @@ public async void FindsError_ForStringReferenceOnSameClass()
[Fact]
public async void FindsError_ForStringReferenceOnOtherClass()
{
var source = "public partial class TestClass { [Xunit.MemberData(\"OtherData\", MemberType = typeof(OtherClass))] public void TestMethod() { } }";
var source = "public partial class TestClass { [Xunit.MemberData(\"OtherData\", MemberType = typeof(OtherClass))] public void TestMethod() { } }";

await new Verify.Test
{
Expand Down
12 changes: 11 additions & 1 deletion test/xunit.analyzers.tests/TupleMemberDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ namespace Xunit.Analyzers
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public sealed class TupleMemberDataAttribute : MemberDataAttributeBase
{
private static readonly Type[] TupleOpenGenericTypes = { typeof(Tuple<>), typeof(Tuple<,>), typeof(Tuple<,,>), typeof(Tuple<,,,>), typeof(Tuple<,,,,>), typeof(Tuple<,,,,,>), typeof(Tuple<,,,,,,>), };
private static readonly Type[] TupleOpenGenericTypes =
{
typeof(Tuple<>),
typeof(Tuple<,>),
typeof(Tuple<,,>),
typeof(Tuple<,,,>),
typeof(Tuple<,,,,>),
typeof(Tuple<,,,,,>),
typeof(Tuple<,,,,,,>),
};

private static readonly ConcurrentDictionary<Type, Func<object, object[]>> ConverterCache = new ConcurrentDictionary<Type, Func<object, object[]>>();

public TupleMemberDataAttribute(string memberName, params object[] parameters)
Expand Down

0 comments on commit c2635ad

Please sign in to comment.