本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.ReturnStatementSyntax类的典型用法代码示例。如果您正苦于以下问题:C# ReturnStatementSyntax类的具体用法?C# ReturnStatementSyntax怎么用?C# ReturnStatementSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ReturnStatementSyntax类属于Microsoft.CodeAnalysis.CSharp.Syntax命名空间,在下文中一共展示了ReturnStatementSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateNewReturnStm
private StatementSyntax CreateNewReturnStm(ReturnStatementSyntax ret)
{
var statements = new List<StatementSyntax>();
var arg = DocumentWeaver.ParametersToArg(_m.ParameterList.Parameters, x => x.Modifiers.Count(y => y.Kind() == SyntaxKind.OutKeyword || y.Kind() == SyntaxKind.RefKeyword) > 0);
if (DocumentWeaver.IsFunction(_m))
{
//pushResult
var left = SyntaxFactory.IdentifierName(DocumentWeaver.RESULTMARKER);
var newNode = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, left, ret.Expression);
var returnVar = SyntaxFactory.ReturnStatement(left);
//specialvar = xxxx
statements.Add(SyntaxFactory.ExpressionStatement(newNode));
//pushresult (speacialvar)
statements.Add(DocumentWeaver.ReportStatement("PushResult", SyntaxFactory.Argument(SyntaxFactory.IdentifierName(DocumentWeaver.RESULTMARKER))));
//place here values of ref/out args
if (arg != null)
statements.Add(DocumentWeaver.ReportStatement("PushOutArgs", SyntaxFactory.Argument(arg)));
//return specialvar
statements.Add(returnVar);
}
else
{
//place here values of ref/out args
if (arg != null)
statements.Add(DocumentWeaver.ReportStatement("PushOutArgs", SyntaxFactory.Argument(arg)));
//if void method there is no expression after return, so we add the same expression 'return;'
statements.Add(ret);
}
return SyntaxFactory.Block(statements);
}
示例2: GetMatch
static bool GetMatch(IfStatementSyntax node, out ExpressionSyntax c, out ReturnStatementSyntax e1, out ReturnStatementSyntax e2, out ReturnStatementSyntax rs)
{
rs = e1 = e2 = null;
c = node.Condition;
//attempt to match if(condition) return else return
e1 = ConvertIfStatementToNullCoalescingExpressionAction.GetSimpleStatement(node.Statement) as ReturnStatementSyntax;
if (e1 == null)
return false;
e2 = node.Else != null ? ConvertIfStatementToNullCoalescingExpressionAction.GetSimpleStatement(node.Else.Statement) as ReturnStatementSyntax : null;
//match
if (e1 != null && e2 != null)
{
return true;
}
//attempt to match if(condition) return; return
if (e1 != null)
{
var parentBlock = node.Parent as BlockSyntax;
if (parentBlock == null)
return false;
var index = parentBlock.Statements.IndexOf(node);
if (index + 1 < parentBlock.Statements.Count)
{
rs = parentBlock.Statements[index + 1] as ReturnStatementSyntax;
}
if (rs != null)
{
e2 = rs;
return true;
}
}
return false;
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:35,代码来源:ConvertIfStatementToReturnStatementAction.cs
示例3: GetMatch
static bool GetMatch(IfStatementSyntax node, out ExpressionSyntax c, out ReturnStatementSyntax e1, out ReturnStatementSyntax e2, out ReturnStatementSyntax rs)
{
rs = e1 = e2 = null;
c = node.Condition;
//attempt to match if(condition) return else return
e1 = ConvertIfStatementToNullCoalescingExpressionAction.GetSimpleStatement(node.Statement) as ReturnStatementSyntax;
if (e1 == null)
return false;
e2 = node.Else != null ? ConvertIfStatementToNullCoalescingExpressionAction.GetSimpleStatement(node.Else.Statement) as ReturnStatementSyntax : null;
//match
if (e1 != null && e2 != null)
{
return true;
}
//attempt to match if(condition) return
if (e1 != null)
{
rs = node.Parent.ChildThatContainsPosition(node.GetTrailingTrivia().Max(t => t.FullSpan.End) + 1).AsNode() as ReturnStatementSyntax;
if (rs != null)
{
e2 = rs;
return true;
}
}
return false;
}
示例4: VisitReturnStatement
public override void VisitReturnStatement(ReturnStatementSyntax node)
{
if (node.Expression != null && node.Expression.Kind() == SyntaxKind.NullLiteralExpression)
{
AddMessage(node.Expression, "Can't return null");
}
}
示例5: VisitReturnStatement
public override SyntaxNode VisitReturnStatement(ReturnStatementSyntax node)
{
if (!IsInLambda(node))
{
modified = true;
return CreateNewReturnStm(node);
}
return base.VisitReturnStatement(node);
}
示例6: GetMemberAccessExpressionFromReturn
private static MemberAccessExpressionSyntax GetMemberAccessExpressionFromReturn(ReturnStatementSyntax returnIf, ReturnStatementSyntax returnElse)
{
if (returnIf?.Expression == null || returnElse?.Expression == null) return null;
var nullLiteral = returnElse.Expression as LiteralExpressionSyntax;
if (nullLiteral == null) return null;
if (!nullLiteral.IsKind(SyntaxKind.NullLiteralExpression)) return null;
var memberAccessExpression = returnIf.Expression as MemberAccessExpressionSyntax;
return memberAccessExpression;
}
示例7: ReturnStatement
public static string ReturnStatement(ReturnStatementSyntax statement)
{
var output = "return";
if (statement.Expression != null)
{
output += " " + SyntaxNode(statement.Expression);
}
return output + Semicolon(statement.SemicolonToken);
}
示例8: ReplaceWithEmptyEnumerable
private async Task<Document> ReplaceWithEmptyEnumerable(Document document, ReturnStatementSyntax returnNullStatement, MethodDeclarationSyntax method, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
var typeSymbol = semanticModel.GetSymbolInfo(method.ReturnType, cancellationToken).Symbol as INamedTypeSymbol;
var genericTypeArgument = typeSymbol.TypeArguments.Single();
var empty = SyntaxFactory.ParseExpression($"Enumerable.Empty<{genericTypeArgument.Name}>()");
var returnEmptyStatement = returnNullStatement.WithExpression(empty);
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = root.ReplaceNode(returnNullStatement, returnEmptyStatement);
return document.WithSyntaxRoot(newRoot) ;
}
示例9: UseExistenceOperatorAsyncWithReturnAsync
private static async Task<Document> UseExistenceOperatorAsyncWithReturnAsync(Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken, ReturnStatementSyntax returnIf)
{
var newMemberAccess = ((MemberAccessExpressionSyntax)returnIf.Expression).ToConditionalAccessExpression();
var newReturn = SyntaxFactory.ReturnStatement(newMemberAccess)
.WithLeadingTrivia(ifStatement.GetLeadingTrivia())
.WithTrailingTrivia(ifStatement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
var root = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = root.ReplaceNode(ifStatement, newReturn);
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument;
}
示例10: VisitReturnStatement
public override SyntaxNode VisitReturnStatement(ReturnStatementSyntax node)
{
var newNode = node.WithExpression(
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.ParseName("System.Threading.Tasks.Task")
.WithAdditionalAnnotations(Simplifier.Annotation),
SyntaxFactory.IdentifierName("FromResult")),
SyntaxFactory.ArgumentList().AddArguments(SyntaxFactory.Argument(node.Expression))));
return base.VisitReturnStatement(newNode);
}
示例11: TryGetNewReturnStatement
public static bool TryGetNewReturnStatement(IfStatementSyntax ifStatement, SemanticModel semanticModel, out ReturnStatementSyntax returnStatement)
{
returnStatement = null;
var conditional = new ReturnConditionalAnalyzer(ifStatement, semanticModel).CreateConditional();
if (conditional == null)
{
return false;
}
returnStatement = SyntaxFactory.ReturnStatement(conditional);
return true;
}
示例12: TryGetReturnStatements
private static bool TryGetReturnStatements(IfStatementSyntax ifStatement, out ReturnStatementSyntax whenTrueStatement, out ReturnStatementSyntax whenFalseStatement)
{
Debug.Assert(ifStatement != null);
Debug.Assert(ifStatement.Else != null);
whenTrueStatement = null;
whenFalseStatement = null;
var statement = ifStatement.Statement.SingleStatementOrSelf() as ReturnStatementSyntax;
if (statement == null)
{
return false;
}
var elseStatement = ifStatement.Else.Statement.SingleStatementOrSelf() as ReturnStatementSyntax;
if (elseStatement == null)
{
return false;
}
whenTrueStatement = statement;
whenFalseStatement = elseStatement;
return true;
}
示例13: VisitReturnStatement
public override void VisitReturnStatement(ReturnStatementSyntax node)
{
if (node.Expression != null)
{
Visit(node.Expression, _enclosing);
}
}
示例14: BindReturn
private BoundReturnStatement BindReturn(ReturnStatementSyntax syntax, DiagnosticBag diagnostics)
{
var expressionSyntax = syntax.Expression;
BoundExpression arg = null;
if (expressionSyntax != null)
{
arg = BindValue(expressionSyntax, diagnostics, BindValueKind.RValue);
}
else
{
// If this is a void return statement in a script, return default(T).
var interactiveInitializerMethod = this.ContainingMemberOrLambda as SynthesizedInteractiveInitializerMethod;
if (interactiveInitializerMethod != null)
{
arg = new BoundDefaultOperator(interactiveInitializerMethod.GetNonNullSyntaxNode(), interactiveInitializerMethod.ResultType);
}
}
bool hasErrors;
if (IsDirectlyInIterator)
{
diagnostics.Add(ErrorCode.ERR_ReturnInIterator, syntax.ReturnKeyword.GetLocation());
hasErrors = true;
}
else if (arg != null)
{
hasErrors = arg.HasErrors || ((object)arg.Type != null && arg.Type.IsErrorType());
}
else
{
hasErrors = false;
}
if (hasErrors)
{
return new BoundReturnStatement(syntax, arg, hasErrors: true);
}
TypeSymbol retType = GetCurrentReturnType();
// The return type could be null; we might be attempting to infer the return type either
// because of method type inference, or because we are attempting to do error analysis
// on a lambda expression of unknown return type.
if ((object)retType != null)
{
if (retType.SpecialType == SpecialType.System_Void || IsTaskReturningAsyncMethod())
{
if (arg != null)
{
var container = this.ContainingMemberOrLambda;
var lambda = container as LambdaSymbol;
if ((object)lambda != null)
{
// Error case: void-returning or async task-returning method or lambda with "return x;"
var errorCode = retType.SpecialType == SpecialType.System_Void
? ErrorCode.ERR_RetNoObjectRequiredLambda
: ErrorCode.ERR_TaskRetNoObjectRequiredLambda;
// Anonymous function converted to a void returning delegate cannot return a value
Error(diagnostics, errorCode, syntax.ReturnKeyword);
// COMPATIBILITY: The native compiler also produced an error
// COMPATIBILITY: "Cannot convert lambda expression to delegate type 'Action' because some of the
// COMPATIBILITY: return types in the block are not implicitly convertible to the delegate return type"
// COMPATIBILITY: This error doesn't make sense in the "void" case because the whole idea of
// COMPATIBILITY: "conversion to void" is a bit unusual, and we've already given a good error.
}
else
{
// Error case: void-returning or async task-returning method or lambda with "return x;"
var errorCode = retType.SpecialType == SpecialType.System_Void
? ErrorCode.ERR_RetNoObjectRequired
: ErrorCode.ERR_TaskRetNoObjectRequired;
Error(diagnostics, errorCode, syntax.ReturnKeyword, container);
}
}
}
else
{
if (arg == null)
{
// Error case: non-void-returning or Task<T>-returning method or lambda but just have "return;"
var requiredType = IsGenericTaskReturningAsyncMethod()
? retType.GetMemberTypeArgumentsNoUseSiteDiagnostics().Single()
: retType;
Error(diagnostics, ErrorCode.ERR_RetObjectRequired, syntax.ReturnKeyword, requiredType);
}
else
{
arg = CreateReturnConversion(syntax, diagnostics, arg, retType);
}
}
}
else
{
// Check that the returned expression is not void.
if ((object)arg?.Type != null && arg.Type.SpecialType == SpecialType.System_Void)
{
//.........这里部分代码省略.........
示例15: VisitReturnStatement
public override SyntaxNode VisitReturnStatement(ReturnStatementSyntax node)
{
if (node.Expression != null)
{
_output.Write(node.ReturnKeyword, "return ");
this.VisitExpression(node.Expression);
}
else
{
_output.Write(node.ReturnKeyword, "return");
}
return node;
}