未验证 提交 28272087 编写于 作者: J Jinu 提交者: GitHub

Merge pull request #30532 from Scott-Caldwell/24138-Infer-bool-return-in-case-when

Infer return type of bool when generating methods in when clauses
......@@ -8367,6 +8367,46 @@ private int GenerateMethod()
{
throw new NotImplementedException();
}
}");
}
[WorkItem(24138, "https://github.com/dotnet/roslyn/issues/24138")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
public async Task TestInCaseWhenClause()
{
await TestInRegularAndScriptAsync(
@"
using System;
class Class
{
void M(object goo)
{
switch (goo)
{
case int i when [|GreaterThanZero(i)|]:
break;
}
}
}",
@"
using System;
class Class
{
void M(object goo)
{
switch (goo)
{
case int i when GreaterThanZero(i):
break;
}
}
private bool GreaterThanZero(int i)
{
throw new NotImplementedException();
}
}");
}
}
......
......@@ -167,6 +167,7 @@ private IEnumerable<TypeInferenceInfo> GetTypesSimple(ExpressionSyntax expressio
case ThrowExpressionSyntax throwExpression: return InferTypeInThrowExpression(throwExpression);
case ThrowStatementSyntax throwStatement: return InferTypeInThrowStatement(throwStatement);
case UsingStatementSyntax usingStatement: return InferTypeInUsingStatement(usingStatement);
case WhenClauseSyntax whenClause: return InferTypeInWhenClause(whenClause);
case WhileStatementSyntax whileStatement: return InferTypeInWhileStatement(whileStatement);
case YieldStatementSyntax yieldStatement: return InferTypeInYieldStatement(yieldStatement);
default: return SpecializedCollections.EmptyEnumerable<TypeInferenceInfo>();
......@@ -226,6 +227,7 @@ protected override IEnumerable<TypeInferenceInfo> InferTypesWorker_DoNotCallDire
case SwitchStatementSyntax switchStatement: return InferTypeInSwitchStatement(switchStatement, token);
case ThrowStatementSyntax throwStatement: return InferTypeInThrowStatement(throwStatement, token);
case UsingStatementSyntax usingStatement: return InferTypeInUsingStatement(usingStatement, token);
case WhenClauseSyntax whenClause: return InferTypeInWhenClause(whenClause, token);
case WhileStatementSyntax whileStatement: return InferTypeInWhileStatement(whileStatement, token);
case YieldStatementSyntax yieldStatement: return InferTypeInYieldStatement(yieldStatement, token);
default: return SpecializedCollections.EmptyEnumerable<TypeInferenceInfo>();
......@@ -2101,6 +2103,17 @@ private IEnumerable<TypeInferenceInfo> InferTypeInVariableComponentAssignment(Ex
elementNamesBuilder.Add(null);
}
private IEnumerable<TypeInferenceInfo> InferTypeInWhenClause(WhenClauseSyntax whenClause, SyntaxToken? previousToken = null)
{
// If we have a position, we have to be after the "when"
if (previousToken.HasValue && previousToken.Value != whenClause.WhenKeyword)
{
return SpecializedCollections.EmptyEnumerable<TypeInferenceInfo>();
}
return SpecializedCollections.SingletonEnumerable(new TypeInferenceInfo(Compilation.GetSpecialType(SpecialType.System_Boolean)));
}
private IEnumerable<TypeInferenceInfo> InferTypeInWhileStatement(WhileStatementSyntax whileStatement, SyntaxToken? previousToken = null)
{
// If we're position based, then we have to be after the "while("
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册