Skip to content
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
Expand Up @@ -91,9 +91,9 @@ public async Task VerifyInvalidValueTypeCreationAsync()
{
public void TestMethod()
{
var v1 = new TestStruct();
var v1 = {|#0:new TestStruct()|};

System.Console.WriteLine(new TestStruct());
System.Console.WriteLine({|#1:new TestStruct()|});
}

private struct TestStruct
Expand Down Expand Up @@ -121,8 +121,8 @@ private struct TestStruct

DiagnosticResult[] expected =
{
Diagnostic().WithLocation(5, 18),
Diagnostic().WithLocation(7, 34),
Diagnostic().WithLocation(0),
Diagnostic().WithLocation(1),
};

await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -139,11 +139,11 @@ public async Task VerifyTriviaPreservationAsync()
{
public void TestMethod()
{
var v1 = /* c1 */ new TestStruct(); // c2
var v1 = /* c1 */ {|#0:new TestStruct()|}; // c2

var v2 =
#if true
new TestStruct();
{|#1:new TestStruct()|};
#else
new TestStruct() { TestProperty = 3 };
#endif
Expand Down Expand Up @@ -179,8 +179,8 @@ private struct TestStruct

DiagnosticResult[] expected =
{
Diagnostic().WithLocation(5, 27),
Diagnostic().WithLocation(9, 13),
Diagnostic().WithLocation(0),
Diagnostic().WithLocation(1),
};

await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -198,7 +198,7 @@ public async Task VerifyGenericsTypeCreationAsync()
public T TestMethod1<T>()
where T : struct
{
return new T();
return {|#0:new T()|};
}

public T TestMethod2<T>()
Expand Down Expand Up @@ -227,7 +227,7 @@ public T TestMethod2<T>()

DiagnosticResult[] expected =
{
Diagnostic().WithLocation(6, 16),
Diagnostic().WithLocation(0),
};

await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -451,7 +451,7 @@ public async Task VerifyEnumMemberReplacementBehaviorAsync(string declarationBod
{{
public void TestMethod()
{{
var v1 = new MyEnum();
var v1 = {{|#0:new MyEnum()|}};
}}

private enum MyEnum {{ {declarationBody} }}
Expand All @@ -469,7 +469,7 @@ private enum MyEnum {{ {declarationBody} }}

DiagnosticResult[] expected =
{
Diagnostic().WithLocation(5, 18),
Diagnostic().WithLocation(0),
};

await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -492,7 +492,7 @@ public async Task VerifyEnumMemberDefaultBehaviorAsync(string declarationBody)
{{
public void TestMethod()
{{
var v1 = new MyEnum();
var v1 = {{|#0:new MyEnum()|}};
}}

private enum MyEnum {{ {declarationBody} }}
Expand All @@ -510,7 +510,7 @@ private enum MyEnum {{ {declarationBody} }}

DiagnosticResult[] expected =
{
Diagnostic().WithLocation(5, 18),
Diagnostic().WithLocation(0),
};

await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Lightup
{
using System;
using Microsoft.CodeAnalysis;

internal readonly struct IArgumentOperationWrapper : IOperationWrapper
{
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IArgumentOperation";
private static readonly Type WrappedType;

private readonly IOperation operation;

static IArgumentOperationWrapper()
{
WrappedType = WrapperHelper.GetWrappedType(typeof(IObjectCreationOperationWrapper));
}

private IArgumentOperationWrapper(IOperation operation)
{
this.operation = operation;
}

public IOperation WrappedOperation => this.operation;

public ITypeSymbol Type => this.WrappedOperation.Type;

public static IArgumentOperationWrapper FromOperation(IOperation operation)
{
if (operation == null)
{
return default;
}

if (!IsInstance(operation))
{
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'");
}

return new IArgumentOperationWrapper(operation);
}

public static bool IsInstance(IOperation operation)
{
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Lightup
{
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;

internal readonly struct IObjectCreationOperationWrapper : IOperationWrapper
{
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IObjectCreationOperation";
private static readonly Type WrappedType;

private static readonly Func<IOperation, IMethodSymbol> ConstructorAccessor;
private static readonly Func<IOperation, IOperation> InitializerAccessor;
private static readonly Func<IOperation, ImmutableArray<IOperation>> ArgumentsAccessor;

private readonly IOperation operation;

static IObjectCreationOperationWrapper()
{
WrappedType = WrapperHelper.GetWrappedType(typeof(IObjectCreationOperationWrapper));
ConstructorAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IMethodSymbol>(WrappedType, nameof(Constructor));
InitializerAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IOperation>(WrappedType, nameof(Initializer));
ArgumentsAccessor = LightupHelpers.CreateOperationListPropertyAccessor<IOperation>(WrappedType, nameof(Arguments));
}

private IObjectCreationOperationWrapper(IOperation operation)
{
this.operation = operation;
}

public IOperation WrappedOperation => this.operation;

public ITypeSymbol Type => this.WrappedOperation.Type;

public IMethodSymbol Constructor
{
get
{
return ConstructorAccessor(this.WrappedOperation);
}
}

public IObjectOrCollectionInitializerOperationWrapper Initializer
{
get
{
return IObjectOrCollectionInitializerOperationWrapper.FromOperation(InitializerAccessor(this.WrappedOperation));
}
}

public ImmutableArray<IOperation> Arguments
{
get
{
return ArgumentsAccessor(this.WrappedOperation);
}
}

public static IObjectCreationOperationWrapper FromOperation(IOperation operation)
{
if (operation == null)
{
return default;
}

if (!IsInstance(operation))
{
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'");
}

return new IObjectCreationOperationWrapper(operation);
}

public static bool IsInstance(IOperation operation)
{
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Lightup
{
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;

internal readonly struct IObjectOrCollectionInitializerOperationWrapper : IOperationWrapper
{
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IObjectOrCollectionInitializerOperation";
private static readonly Type WrappedType;

private static readonly Func<IOperation, ImmutableArray<IOperation>> InitializersAccessor;

private readonly IOperation operation;

static IObjectOrCollectionInitializerOperationWrapper()
{
WrappedType = WrapperHelper.GetWrappedType(typeof(IObjectOrCollectionInitializerOperationWrapper));
InitializersAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, ImmutableArray<IOperation>>(WrappedType, nameof(Initializers));
}

private IObjectOrCollectionInitializerOperationWrapper(IOperation operation)
{
this.operation = operation;
}

public IOperation WrappedOperation => this.operation;

public ITypeSymbol Type => this.WrappedOperation.Type;

public ImmutableArray<IOperation> Initializers
{
get
{
return InitializersAccessor(this.WrappedOperation);
}
}

public static IObjectOrCollectionInitializerOperationWrapper FromOperation(IOperation operation)
{
if (operation == null)
{
return default;
}

if (!IsInstance(operation))
{
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'");
}

return new IObjectOrCollectionInitializerOperationWrapper(operation);
}

public static bool IsInstance(IOperation operation)
{
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType);
}
}
}
32 changes: 32 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers/Lightup/IOperationWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable enable

namespace StyleCop.Analyzers.Lightup
{
using Microsoft.CodeAnalysis;

internal interface IOperationWrapper
{
IOperation? WrappedOperation { get; }

////IOperationWrapper Parent { get; }

////OperationKind Kind { get; }

////SyntaxNode Syntax { get; }

ITypeSymbol? Type { get; }

////Optional<object> ConstantValue { get; }

////IEnumerable<IOperationWrapper> Children { get; }

////string Language { get; }

////bool IsImplicit { get; }

////SemanticModel SemanticModel { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Lightup
{
using System;
using Microsoft.CodeAnalysis;

internal readonly struct ITypeParameterObjectCreationOperationWrapper : IOperationWrapper
{
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.ITypeParameterObjectCreationOperation";
private static readonly Type WrappedType;

private static readonly Func<IOperation, IOperation> InitializerAccessor;

private readonly IOperation operation;

static ITypeParameterObjectCreationOperationWrapper()
{
WrappedType = WrapperHelper.GetWrappedType(typeof(ITypeParameterObjectCreationOperationWrapper));
InitializerAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IOperation>(WrappedType, nameof(Initializer));
}

private ITypeParameterObjectCreationOperationWrapper(IOperation operation)
{
this.operation = operation;
}

public IOperation WrappedOperation => this.operation;

public ITypeSymbol Type => this.WrappedOperation.Type;

public IObjectOrCollectionInitializerOperationWrapper Initializer
{
get
{
return IObjectOrCollectionInitializerOperationWrapper.FromOperation(InitializerAccessor(this.WrappedOperation));
}
}

public static ITypeParameterObjectCreationOperationWrapper FromOperation(IOperation operation)
{
if (operation == null)
{
return default;
}

if (!IsInstance(operation))
{
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'");
}

return new ITypeParameterObjectCreationOperationWrapper(operation);
}

public static bool IsInstance(IOperation operation)
{
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType);
}
}
}
Loading