本文整理汇总了C#中SyntaxNodeAnalysisContext.IsGenerated方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNodeAnalysisContext.IsGenerated方法的具体用法?C# SyntaxNodeAnalysisContext.IsGenerated怎么用?C# SyntaxNodeAnalysisContext.IsGenerated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNodeAnalysisContext
的用法示例。
在下文中一共展示了SyntaxNodeAnalysisContext.IsGenerated方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AnalyzeNode
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var localDeclaration = (LocalDeclarationStatementSyntax)context.Node;
if (localDeclaration.IsConst) return;
var variableDeclaration = localDeclaration.ChildNodes()
.OfType<VariableDeclarationSyntax>()
.FirstOrDefault();
if (variableDeclaration.Type.IsVar) return;
var semanticModel = context.SemanticModel;
var variableTypeName = localDeclaration.Declaration.Type;
var variableType = semanticModel.GetTypeInfo(variableTypeName).ConvertedType;
foreach (var variable in variableDeclaration.Variables)
{
if (variable.Initializer == null) return;
var conversion = semanticModel.ClassifyConversion(variable.Initializer.Value, variableType);
if (!conversion.IsIdentity) return;
}
var diagnostic = Diagnostic.Create(Rule, variableDeclaration.Type.GetLocation());
context.ReportDiagnostic(diagnostic);
}
示例2: AnalyzeNode
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var objectCreationExpression = (ObjectCreationExpressionSyntax)context.Node;
var type = objectCreationExpression.Type;
var typeSymbol = context.SemanticModel.GetSymbolInfo(type).Symbol as ITypeSymbol;
if (!typeSymbol?.ToString().EndsWith("System.ArgumentException") ?? true) return;
var argumentList = objectCreationExpression.ArgumentList as ArgumentListSyntax;
if ((argumentList?.Arguments.Count ?? 0) < 2) return;
var paramNameLiteral = argumentList.Arguments[1].Expression as LiteralExpressionSyntax;
if (paramNameLiteral == null) return;
var paramNameOpt = context.SemanticModel.GetConstantValue(paramNameLiteral);
if (!paramNameOpt.HasValue) return;
var paramName = paramNameOpt.Value as string;
IList<string> parameters;
if (IsParamNameCompatibleWithCreatingContext(objectCreationExpression, paramName, out parameters)) return;
var props = parameters.ToImmutableDictionary(p => $"param{p}", p => p);
var diagnostic = Diagnostic.Create(Rule, paramNameLiteral.GetLocation(), props.ToImmutableDictionary(), paramName);
context.ReportDiagnostic(diagnostic);
}
示例3: Analyzer
private static void Analyzer(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var invocation = (InvocationExpressionSyntax)context.Node;
var identifier = invocation.Expression as IdentifierNameSyntax;
if (identifier == null) return;
var typeInfo = context.SemanticModel.GetTypeInfo(identifier, context.CancellationToken);
if (typeInfo.ConvertedType?.BaseType == null) return;
if (typeInfo.ConvertedType.BaseType.Name != typeof(MulticastDelegate).Name) return;
var symbol = context.SemanticModel.GetSymbolInfo(identifier).Symbol;
if (symbol is ILocalSymbol) return;
var invokedMethodSymbol = (typeInfo.ConvertedType as INamedTypeSymbol)?.DelegateInvokeMethod;
if (invokedMethodSymbol == null) return;
if (HasCheckForNullThatReturns(invocation, context.SemanticModel, symbol)) return;
if (IsInsideANullCheck(invocation, context.SemanticModel, symbol)) return;
if (IsPartOfATernaryThatChecksForNull(invocation, context.SemanticModel, symbol)) return;
if (IsPartOfALogicalOrThatChecksForNull(invocation, context.SemanticModel, symbol)) return;
if (IsPartOfALogicalAndThatChecksForNotNull(invocation, context.SemanticModel, symbol)) return;
if (symbol.IsReadOnlyAndInitializedForCertain(context)) return;
context.ReportDiagnostic(Diagnostic.Create(Rule, invocation.GetLocation(), identifier.Identifier.Text));
}
示例4: Analyze
private static void Analyze(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var typeDeclarationSyntax = context.Node as TypeDeclarationSyntax;
if (!CanOrder(typeDeclarationSyntax)) return;
context.ReportDiagnostic(Diagnostic.Create(Rule, typeDeclarationSyntax.Identifier.GetLocation()));
}
示例5: Analyzer
private static void Analyzer(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var ifStatement = context.Node as IfStatementSyntax;
if (ifStatement == null) return;
if (ifStatement.Else == null) return;
var blockIf = ifStatement.Statement as BlockSyntax;
var blockElse = ifStatement.Else.Statement as BlockSyntax;
if ((blockIf == null || blockIf.Statements.Count == 1) &&
(blockElse == null || blockElse.Statements.Count == 1))
{
var statementInsideIf = ifStatement.Statement is BlockSyntax ? ((BlockSyntax)ifStatement.Statement).Statements.Single() : ifStatement.Statement;
var elseStatement = ifStatement.Else;
var statementInsideElse = elseStatement.Statement is BlockSyntax ? ((BlockSyntax)elseStatement.Statement).Statements.Single() : elseStatement.Statement;
if (statementInsideIf is ReturnStatementSyntax && statementInsideElse is ReturnStatementSyntax)
{
var diagnostic = Diagnostic.Create(RuleForIfWithReturn, ifStatement.IfKeyword.GetLocation(), "You can use a ternary operator.");
context.ReportDiagnostic(diagnostic);
return;
}
var expressionInsideIf = statementInsideIf as ExpressionStatementSyntax;
var expressionInsideElse = statementInsideElse as ExpressionStatementSyntax;
if (expressionInsideIf != null && expressionInsideIf.Expression.IsKind(SyntaxKind.SimpleAssignmentExpression) && expressionInsideElse != null && expressionInsideElse.Expression.IsKind(SyntaxKind.SimpleAssignmentExpression))
{
var assignmentExpressionInsideIf = (AssignmentExpressionSyntax)expressionInsideIf.Expression;
var assignmentExpressionInsideElse = (AssignmentExpressionSyntax)expressionInsideElse.Expression;
var variableIdentifierInsideIf = assignmentExpressionInsideIf.Left as IdentifierNameSyntax;
var variableIdentifierInsideElse = assignmentExpressionInsideElse.Left as IdentifierNameSyntax;
if (variableIdentifierInsideIf == null || variableIdentifierInsideElse == null
|| variableIdentifierInsideIf.Identifier.Text != variableIdentifierInsideElse.Identifier.Text) return;
var diagnostic = Diagnostic.Create(RuleForIfWithAssignment, ifStatement.IfKeyword.GetLocation(), "You can use a ternary operator.");
context.ReportDiagnostic(diagnostic);
}
}
}
示例6: AnalyzeNode
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var lambda = context.Node as ExpressionSyntax;
var invocation = GetInvocationIfAny(lambda);
if (invocation == null || invocation.ArgumentList.Arguments.Count == 0) return;
var lambdaParameters = BuildParameters(lambda);
if (!MatchArguments(lambdaParameters, invocation.ArgumentList)) return;
var root = lambda.SyntaxTree.GetRoot();
var newRoot = root.ReplaceNode(lambda, invocation.Expression as ExpressionSyntax);
var semanticNode = GetNodeRootForAnalysis(lambda);
var newSemanticNode = newRoot.DescendantNodesAndSelf()
.Where(x => x.SpanStart == semanticNode.SpanStart && x.Span.OverlapsWith(context.Node.Span))
.LastOrDefault(x => x.Kind() == semanticNode.Kind());
if (newSemanticNode == null || ReplacementChangesSemantics(semanticNode, newSemanticNode, context.SemanticModel)) return;
var diagnostic = Diagnostic.Create(
Rule,
context.Node.GetLocation(),
invocation.Expression.ToString());
context.ReportDiagnostic(diagnostic);
}
示例7: AnalyzeNode
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var @switch = (SwitchStatementSyntax)context.Node;
if ([email protected](HasBraces))
context.ReportDiagnostic(Diagnostic.Create(Rule, @switch.GetLocation()));
}
示例8: AnalyzeConstructor
private static void AnalyzeConstructor(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var constructorMethod = (ConstructorDeclarationSyntax)context.Node;
var type = constructorMethod.FirstAncestorOrSelf<TypeDeclarationSyntax>();
if (type == null || !(type is ClassDeclarationSyntax || type is StructDeclarationSyntax)) return;
var parameters = constructorMethod.ParameterList.Parameters;
if (constructorMethod.Body == null) return;
var analysis = context.SemanticModel.AnalyzeDataFlow(constructorMethod.Body);
if (!analysis.Succeeded) return;
foreach (var par in parameters)
{
var parSymbol = context.SemanticModel.GetDeclaredSymbol(par);
if(!analysis.ReadInside.Any(s => s.Equals(parSymbol)))
{
var parameterName = par.Identifier.Text;
var properties = new Dictionary<string, string> { { nameof(parameterName), parameterName } }.ToImmutableDictionary();
var diag = Diagnostic.Create(Rule, par.GetLocation(), properties, parameterName);
context.ReportDiagnostic(diag);
}
}
}
示例9: AnalyzeMethod
private static void AnalyzeMethod(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var method = (MethodDeclarationSyntax)context.Node;
if (method.Identifier.ToString().EndsWith("Async")) return;
if (method.Modifiers.Any(SyntaxKind.NewKeyword, SyntaxKind.OverrideKeyword)) return;
var errorMessage = method.Identifier.ToString() + "Async";
var diag = Diagnostic.Create(Rule, method.Identifier.GetLocation(), errorMessage);
if (method.Modifiers.Any(SyntaxKind.AsyncKeyword))
{
context.ReportDiagnostic(diag);
return;
}
var returnType = context.SemanticModel.GetSymbolInfo(method.ReturnType).Symbol as INamedTypeSymbol;
if (returnType == null) return;
if (returnType.ToString() == "System.Threading.Tasks.Task" ||
(returnType.IsGenericType && returnType.ConstructedFrom.ToString() == "System.Threading.Tasks.Task<TResult>"))
{
context.ReportDiagnostic(diag);
}
}
示例10: Analyzer
private static void Analyzer(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var mainConstrutor = new MethodInformation(
nameof(Uri),
"System.Uri.Uri(string)",
args =>
{
{
if (args[0] == null)
{
return;
}
new Uri(args[0].ToString());
}
}
);
var constructorWithUriKind = new MethodInformation(
nameof(Uri),
"System.Uri.Uri(string, System.UriKind)",
args =>
{
if (args[0] == null)
{
return;
}
new Uri(args[0].ToString(), (UriKind)args[1]);
}
);
var checker = new MethodChecker(context, Rule);
checker.AnalyzeConstructor(mainConstrutor);
checker.AnalyzeConstructor(constructorWithUriKind);
}
示例11: AnalyzeInvocation
private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context, Compilation compilation)
{
if (context.IsGenerated()) return;
var methodInvokeSyntax = context.Node as InvocationExpressionSyntax;
var childNodes = methodInvokeSyntax.ChildNodes();
var methodCaller = childNodes.OfType<MemberAccessExpressionSyntax>().FirstOrDefault();
if (methodCaller == null) return;
var argumentsCount = CountArguments(childNodes);
var classSymbol = GetCallerClassSymbol(context.SemanticModel, methodCaller.Expression);
if (classSymbol == null || !classSymbol.MightContainExtensionMethods) return;
var methodSymbol = GetCallerMethodSymbol(context.SemanticModel, methodCaller.Name, argumentsCount);
if (methodSymbol == null || !methodSymbol.IsExtensionMethod) return;
if (ContainsDynamicArgument(context.SemanticModel, childNodes)) return;
ExpressionSyntax invocationStatement;
if (methodInvokeSyntax.Parent.IsNotKind(SyntaxKind.ArrowExpressionClause))
{
invocationStatement = (methodInvokeSyntax.FirstAncestorOrSelfThatIsAStatement() as ExpressionStatementSyntax).Expression;
}
else
{
invocationStatement = methodInvokeSyntax.FirstAncestorOrSelfOfType<ArrowExpressionClauseSyntax>().Expression;
}
if (invocationStatement == null) return;
if (IsSelectingADifferentMethod(childNodes, methodCaller.Name, context.Node.SyntaxTree, methodSymbol, invocationStatement, compilation)) return;
context.ReportDiagnostic(Diagnostic.Create(Rule, methodCaller.GetLocation(), methodSymbol.Name, classSymbol.Name));
}
示例12: Analyzer
private static void Analyzer(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var invocationExpression = (InvocationExpressionSyntax)context.Node;
var memberExpresion = invocationExpression.Expression as MemberAccessExpressionSyntax;
if (memberExpresion?.Name?.ToString() != "Match") return;
var memberSymbol = context.SemanticModel.GetSymbolInfo(memberExpresion).Symbol;
if (memberSymbol?.ToString() != "System.Text.RegularExpressions.Regex.Match(string, string)") return;
var argumentList = invocationExpression.ArgumentList as ArgumentListSyntax;
if ((argumentList?.Arguments.Count ?? 0) != 2) return;
var regexLiteral = argumentList.Arguments[1].Expression as LiteralExpressionSyntax;
if (regexLiteral == null) return;
var regexOpt = context.SemanticModel.GetConstantValue(regexLiteral);
var regex = regexOpt.Value as string;
try
{
System.Text.RegularExpressions.Regex.Match("", regex);
}
catch (ArgumentException e)
{
var diag = Diagnostic.Create(Rule, regexLiteral.GetLocation(), e.Message);
context.ReportDiagnostic(diag);
}
}
示例13: AnalyzeNode
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var localDeclaration = (LocalDeclarationStatementSyntax)context.Node;
if (localDeclaration.IsConst) return;
var variableDeclaration = localDeclaration.ChildNodes()
.OfType<VariableDeclarationSyntax>()
.FirstOrDefault();
if (variableDeclaration.Type.IsVar) return;
var isDynamic = (variableDeclaration.Type as IdentifierNameSyntax)?.Identifier.ValueText == "dynamic";
var semanticModel = context.SemanticModel;
var variableTypeName = localDeclaration.Declaration.Type;
var variableType = semanticModel.GetTypeInfo(variableTypeName).ConvertedType;
foreach (var variable in variableDeclaration.Variables)
{
if (variable.Initializer == null) return;
var conversion = semanticModel.ClassifyConversion(variable.Initializer.Value, variableType);
if (!conversion.IsIdentity) return;
if (isDynamic)
{
var expressionReturnType = semanticModel.GetTypeInfo(variable.Initializer.Value);
if (expressionReturnType.Type.SpecialType == SpecialType.System_Object) return;
}
}
var rule = variableType.IsPrimitive() ? RulePrimitives : RuleNonPrimitives;
var diagnostic = Diagnostic.Create(rule, variableDeclaration.Type.GetLocation());
context.ReportDiagnostic(diagnostic);
}
示例14: AnalyzeFormatInvocation
public static void AnalyzeFormatInvocation(SyntaxNodeAnalysisContext context, string methodName, string methodOverloadSignature, string methodWithArraySignature, DiagnosticDescriptor rule)
{
if (context.IsGenerated()) return;
var invocationExpression = (InvocationExpressionSyntax)context.Node;
var memberExpresion = invocationExpression.Expression as MemberAccessExpressionSyntax;
if (memberExpresion?.Name?.ToString() != methodName) return;
var memberSymbol = context.SemanticModel.GetSymbolInfo(memberExpresion).Symbol;
if (memberSymbol == null) return;
if (!memberSymbol.ToString().StartsWith(methodOverloadSignature)) return;
var argumentList = invocationExpression.ArgumentList as ArgumentListSyntax;
if (argumentList?.Arguments.Count < 2) return;
if (!argumentList.Arguments[0]?.Expression?.IsKind(SyntaxKind.StringLiteralExpression) ?? false) return;
if (memberSymbol.ToString() == methodWithArraySignature && argumentList.Arguments.Skip(1).Any(a => context.SemanticModel.GetTypeInfo(a.Expression).Type.TypeKind == TypeKind.Array)) return;
var formatLiteral = (LiteralExpressionSyntax)argumentList.Arguments[0].Expression;
var format = (string)context.SemanticModel.GetConstantValue(formatLiteral).Value;
var formatArgs = Enumerable.Range(1, argumentList.Arguments.Count - 1).Select(i => new object()).ToArray();
try
{
string.Format(format, formatArgs);
}
catch (FormatException)
{
return;
}
var diag = Diagnostic.Create(rule, invocationExpression.GetLocation());
context.ReportDiagnostic(diag);
}
示例15: AnalyzeNodeVariableDeclaration
private static void AnalyzeNodeVariableDeclaration(SyntaxNodeAnalysisContext context)
{
if (context.IsGenerated()) return;
var literal = context.Node as LiteralExpressionSyntax;
if (IsNoCandidateDiagnostic(literal)) return;
var diagnostic = Diagnostic.Create(Rule, literal.GetLocation());
context.ReportDiagnostic(diagnostic);
}