Skip to content

Commit

Permalink
Merge pull request #22779 from dotnet/fix-22768-suggest-var-for-stack…
Browse files Browse the repository at this point in the history
…alloc

Do not suggest using implicit type for stackalloc initialization
  • Loading branch information
sharwell authored Nov 3, 2017
2 parents b2a5819 + e7e9fa4 commit 089a2d3
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1749,5 +1749,216 @@ static void M()
}",
options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType()
{
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
{
unsafe public Span(void* pointer, int length) { }
}
}
class C
{
static void M()
{
[|Span<int>|] x = stackalloc int [10];
}
}", new TestParameters(options: ImplicitTypeEverywhere()));
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType_NestedConditional()
{
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
{
unsafe public Span(void* pointer, int length) { }
}
}
class C
{
static void M(bool choice)
{
[|Span<int>|] x = choice ? stackalloc int [10] : stackalloc int [100];
}
}", new TestParameters(options: ImplicitTypeEverywhere()));
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType_NestedCast()
{
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
{
unsafe public Span(void* pointer, int length) { }
}
}
class C
{
static void M()
{
[|Span<int>|] x = (Span<int>)stackalloc int [100];
}
}", new TestParameters(options: ImplicitTypeEverywhere()));
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnLambdasWithNestedStackAllocs()
{
await TestInRegularAndScriptAsync(@"
using System.Linq;
class C
{
unsafe static void M()
{
[|int|] x = new int[] { 1, 2, 3 }.First(i =>
{
int* y = stackalloc int[10];
return i == 1;
});
}
}", @"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(i =>
{
int* y = stackalloc int[10];
return i == 1;
});
}
}", options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnAnonymousMethodsWithNestedStackAllocs()
{
await TestInRegularAndScriptAsync(@"
using System.Linq;
class C
{
unsafe static void M()
{
[|int|] x = new int[] { 1, 2, 3 }.First(delegate (int i)
{
int* y = stackalloc int[10];
return i == 1;
});
}
}", @"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(delegate (int i)
{
int* y = stackalloc int[10];
return i == 1;
});
}
}", options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnStackAllocsNestedInLambdas()
{
await TestInRegularAndScriptAsync(@"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(i =>
{
[|int*|] y = stackalloc int[10];
return i == 1;
});
}
}", @"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(i =>
{
var y = stackalloc int[10];
return i == 1;
});
}
}", options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnStackAllocsNestedInAnonymousMethods()
{
await TestInRegularAndScriptAsync(@"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(delegate (int i)
{
[|int*|] y = stackalloc int[10];
return i == 1;
});
}
}", @"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(delegate (int i)
{
var y = stackalloc int[10];
return i == 1;
});
}
}", options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnStackAllocsInOuterMethodScope()
{
await TestInRegularAndScriptAsync(@"
class C
{
unsafe static void M()
{
[|int*|] x = stackalloc int [10];
}
}", @"
class C
{
unsafe static void M()
{
var x = stackalloc int [10];
}
}", options: ImplicitTypeEverywhere());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,25 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
}

var variable = variableDeclaration.Variables.Single();
var initializer = variable.Initializer.Value;

// Do not suggest var replacement for stackalloc span expressions.
// This will change the bound type from a span to a pointer.
if (!variableDeclaration.Type.IsKind(SyntaxKind.PointerType))
{
var containsStackAlloc = initializer
.DescendantNodesAndSelf(descendIntoChildren: node => !node.IsAnyLambdaOrAnonymousMethod())
.Any(node => node.IsKind(SyntaxKind.StackAllocArrayCreationExpression));

if (containsStackAlloc)
{
issueSpan = default;
return false;
}
}

if (AssignmentSupportsStylePreference(
variable.Identifier, typeName, variable.Initializer.Value,
variable.Identifier, typeName, initializer,
semanticModel, optionSet, cancellationToken))
{
issueSpan = candidateIssueSpan;
Expand Down

0 comments on commit 089a2d3

Please sign in to comment.