提交 fef6618d 编写于 作者: O Omar Tawfik 提交者: Sam Harwell

PR comments

上级 7f077aa3
......@@ -1754,8 +1754,8 @@ static void M()
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
......@@ -1776,8 +1776,8 @@ static void M()
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType_NestedConditional()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
......@@ -1798,8 +1798,8 @@ static void M(bool choice)
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType_NestedCast()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
......@@ -1818,17 +1818,147 @@ static void M()
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_PointerType()
public async Task SuggestVarOnLambdasWithNestedStackAllocs()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
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];
}
}", new TestParameters(options: ImplicitTypeEverywhere()));
}", @"
class C
{
unsafe static void M()
{
var x = stackalloc int [10];
}
}", options: ImplicitTypeEverywhere());
}
}
}
......@@ -109,16 +109,20 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
var variable = variableDeclaration.Variables.Single();
var initializer = variable.Initializer.Value;
// Discourage using "var pointer = stackalloc", after introducing C# 7.2, as it might be confusing to users expecting a Span<>
// Check descendant nodes as well, because Span-creating stackallocs can be nested in other nodes (like conditional operators and casts).
// https://github.com/dotnet/roslyn/issues/22768
if (initializer.DescendantNodesAndSelf().Any(node => node.IsKind(SyntaxKind.StackAllocArrayCreationExpression)))
// 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)
&& initializer
.DescendantNodesAndSelf(descendIntoChildren: node => !node.IsAnyLambdaOrAnonymousMethod())
.Any(node => node.IsKind(SyntaxKind.StackAllocArrayCreationExpression)))
{
issueSpan = default;
return false;
}
if (AssignmentSupportsStylePreference(variable.Identifier, typeName, initializer, semanticModel, optionSet, cancellationToken))
if (AssignmentSupportsStylePreference(
variable.Identifier, typeName, initializer,
semanticModel, optionSet, cancellationToken))
{
issueSpan = candidateIssueSpan;
return true;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册