本文整理汇总了C#中SonarAnalysisContext类的典型用法代码示例。如果您正苦于以下问题:C# SonarAnalysisContext类的具体用法?C# SonarAnalysisContext怎么用?C# SonarAnalysisContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SonarAnalysisContext类属于命名空间,在下文中一共展示了SonarAnalysisContext类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var assignment = (AssignmentExpressionSyntax)c.Node;
if (!IsDelegateSubtraction(assignment, c.SemanticModel) ||
ExpressionIsSimple(assignment.Right))
{
return;
}
c.ReportDiagnostic(Diagnostic.Create(Rule, assignment.GetLocation()));
},
SyntaxKind.SubtractAssignmentExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var binary = (BinaryExpressionSyntax)c.Node;
if (!IsDelegateSubtraction(binary, c.SemanticModel) ||
!IsTopLevelSubtraction(binary))
{
return;
}
if (!BinaryIsValidSubstraction(binary))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, binary.GetLocation()));
}
},
SyntaxKind.SubtractExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:33,代码来源:DelegateSubtraction.cs
示例2: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var expressionStatement = (ExpressionStatementSyntax)c.Node;
CheckExpressionForPureMethod(c, expressionStatement.Expression);
},
SyntaxKind.ExpressionStatement);
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var lambda = (LambdaExpressionSyntax)c.Node;
var symbol = c.SemanticModel.GetSymbolInfo(lambda).Symbol as IMethodSymbol;
if (symbol == null ||
!symbol.ReturnsVoid)
{
return;
}
var expression = lambda.Body as ExpressionSyntax;
CheckExpressionForPureMethod(c, expression);
},
SyntaxKind.ParenthesizedLambdaExpression,
SyntaxKind.SimpleLambdaExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:28,代码来源:ReturnValueIgnored.cs
示例3: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var argumentList = (AttributeArgumentListSyntax)c.Node;
if (!argumentList.Arguments.Any())
{
c.ReportDiagnostic(Diagnostic.Create(Rule, argumentList.GetLocation()));
}
},
SyntaxKind.AttributeArgumentList);
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var objectCreation = (ObjectCreationExpressionSyntax)c.Node;
var argumentList = objectCreation.ArgumentList;
if (argumentList != null &&
objectCreation.Initializer != null &&
!argumentList.Arguments.Any())
{
c.ReportDiagnostic(Diagnostic.Create(Rule, argumentList.GetLocation()));
}
},
SyntaxKind.ObjectCreationExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:27,代码来源:RedundantParentheses.cs
示例4: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var invocation = (InvocationExpressionSyntax)c.Node;
var methodSymbol = c.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
if (methodSymbol == null ||
methodSymbol.Name != "ToCharArray" ||
!methodSymbol.IsInType(KnownType.System_String) ||
methodSymbol.Parameters.Length != 0)
{
return;
}
if (!(invocation.Parent is ElementAccessExpressionSyntax) &&
!(invocation.Parent is ForEachStatementSyntax))
{
return;
}
var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
if (memberAccess == null)
{
return;
}
c.ReportDiagnostic(Diagnostic.Create(Rule, memberAccess.Name.GetLocation()));
},
SyntaxKind.InvocationExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:32,代码来源:RedundantToCharArrayCall.cs
示例5: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var ifStatement = (IfStatementSyntax)c.Node;
if (ifStatement.Else != null ||
ifStatement.Parent is ElseClauseSyntax)
{
return;
}
AssignmentExpressionSyntax assignment;
BinaryExpressionSyntax condition;
if (!TryGetNotEqualsCondition(ifStatement, out condition) ||
!TryGetSingleAssignment(ifStatement, out assignment))
{
return;
}
var expression1Condition = condition.Left?.RemoveParentheses();
var expression2Condition = condition.Right?.RemoveParentheses();
var expression1Assignment = assignment.Left?.RemoveParentheses();
var expression2Assignment = assignment.Right?.RemoveParentheses();
if (AreMatchingExpressions(expression1Condition, expression2Condition, expression2Assignment, expression1Assignment) ||
AreMatchingExpressions(expression1Condition, expression2Condition, expression1Assignment, expression2Assignment))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, condition.GetLocation()));
}
},
SyntaxKind.IfStatement);
}
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:33,代码来源:RedundantConditionalAroundAssignment.cs
示例6: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterCodeBlockStartActionInNonGenerated<SyntaxKind>(cbc =>
{
var unusedLocals = new List<ISymbol>();
cbc.RegisterSyntaxNodeAction(c =>
{
unusedLocals.AddRange(
((LocalDeclarationStatementSyntax) c.Node).Declaration.Variables
.Select(variable => c.SemanticModel.GetDeclaredSymbol(variable))
.Where(symbol => symbol != null));
},
SyntaxKind.LocalDeclarationStatement);
cbc.RegisterSyntaxNodeAction(c =>
{
var symbolsToNotReportOn = GetUsedSymbols(c.Node, c.SemanticModel);
foreach (var symbol in symbolsToNotReportOn)
{
unusedLocals.Remove(symbol);
}
},
SyntaxKind.IdentifierName);
cbc.RegisterCodeBlockEndAction(c =>
{
foreach (var unused in unusedLocals)
{
c.ReportDiagnostic(Diagnostic.Create(Rule, unused.Locations.First(), unused.Name));
}
});
});
}
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:34,代码来源:VariableUnused.cs
示例7: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var invocation = (InvocationExpressionSyntax)c.Node;
var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
if (memberAccess == null ||
!IsDisposableField(memberAccess.Expression, c.SemanticModel) ||
!IsDisposeMethodCalled(invocation, c.SemanticModel))
{
return;
}
var enclosingSymbol = c.SemanticModel.GetEnclosingSymbol(invocation.SpanStart);
if (enclosingSymbol == null)
{
return;
}
var enclosingMethodSymbol = enclosingSymbol as IMethodSymbol;
if (enclosingMethodSymbol == null ||
!IsMethodMatchingDisposeMethodName(enclosingMethodSymbol))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, memberAccess.Name.GetLocation()));
}
},
SyntaxKind.InvocationExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:29,代码来源:DisposeFromDispose.cs
示例8: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c => CheckEquals(c),
SyntaxKind.EqualsExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c => CheckLogicalAnd(c),
SyntaxKind.LogicalAndExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c => CheckNotEquals(c),
SyntaxKind.NotEqualsExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c => CheckLogicalOr(c),
SyntaxKind.LogicalOrExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c => CheckLogicalNot(c),
SyntaxKind.LogicalNotExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c => CheckConditional(c),
SyntaxKind.ConditionalExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:26,代码来源:BooleanLiteralUnnecessary.cs
示例9: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var methodCall = (InvocationExpressionSyntax) c.Node;
var methodParameterLookup = new MethodParameterLookup(methodCall, c.SemanticModel);
var argumentMappings = methodParameterLookup.GetAllArgumentParameterMappings()
.ToList();
var methodSymbol = methodParameterLookup.MethodSymbol;
if (methodSymbol == null)
{
return;
}
foreach (var argumentMapping in argumentMappings.Where(argumentMapping => ArgumentHasDefaultValue(argumentMapping, c.SemanticModel)))
{
var argument = argumentMapping.Argument;
var parameter = argumentMapping.Parameter;
c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation(), parameter.Name));
}
},
SyntaxKind.InvocationExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:25,代码来源:RedundantArgument.cs
示例10: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var ifNode = (IfStatementSyntax)c.Node;
var isTrue = ifNode.Condition.IsKind(SyntaxKind.TrueLiteralExpression);
var isFalse = ifNode.Condition.IsKind(SyntaxKind.FalseLiteralExpression);
if (!isTrue && !isFalse)
{
return;
}
if (isTrue)
{
ReportIfTrue(ifNode, c);
}
else
{
ReportIfFalse(ifNode, c);
}
},
SyntaxKind.IfStatement);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:26,代码来源:IfConditionalAlwaysTrueOrFalse.cs
示例11: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var lessThan = (BinaryExpressionSyntax) c.Node;
int constValue;
if (ExpressionNumericConverter.TryGetConstantIntValue(lessThan.Left, out constValue) &&
constValue == 0 &&
IsIndexOfCall(lessThan.Right, c.SemanticModel))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(lessThan.SyntaxTree,
TextSpan.FromBounds(lessThan.Left.SpanStart, lessThan.OperatorToken.Span.End))));
}
},
SyntaxKind.LessThanExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var greaterThan = (BinaryExpressionSyntax)c.Node;
int constValue;
if (ExpressionNumericConverter.TryGetConstantIntValue(greaterThan.Right, out constValue) &&
constValue == 0 &&
IsIndexOfCall(greaterThan.Left, c.SemanticModel))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(greaterThan.SyntaxTree,
TextSpan.FromBounds(greaterThan.OperatorToken.SpanStart, greaterThan.Right.Span.End))));
}
},
SyntaxKind.GreaterThanExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:32,代码来源:IndexOfCheckAgainstZero.cs
示例12: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var parameter = (ParameterSyntax)c.Node;
if (!parameter.AttributeLists.Any())
{
return;
}
var attributes = AttributeSyntaxSymbolMapping.GetAttributesForParameter(parameter, c.SemanticModel)
.ToList();
var defaultParameterValueAttribute = attributes
.FirstOrDefault(a => a.Symbol.IsInType(KnownType.System_Runtime_InteropServices_DefaultParameterValueAttribute));
if (defaultParameterValueAttribute == null)
{
return;
}
var optionalAttribute = attributes
.FirstOrDefault(a => a.Symbol.IsInType(KnownType.System_Runtime_InteropServices_OptionalAttribute));
if (optionalAttribute == null)
{
c.ReportDiagnostic(Diagnostic.Create(Rule, defaultParameterValueAttribute.SyntaxNode.GetLocation()));
}
},
SyntaxKind.Parameter);
}
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:32,代码来源:MethodParameterMissingOptional.cs
示例13: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var method = (MethodDeclarationSyntax)c.Node;
var methodSymbol = c.SemanticModel.GetDeclaredSymbol(method);
IMethodSymbol overriddenMember;
if (methodSymbol == null ||
!methodSymbol.TryGetOverriddenOrInterfaceMember(out overriddenMember))
{
return;
}
for (int i = 0; i < methodSymbol.Parameters.Length; i++)
{
var overridingParameter = methodSymbol.Parameters[i];
var overriddenParameter = overriddenMember.Parameters[i];
var parameterSyntax = method.ParameterList.Parameters[i];
ReportParameterIfNeeded(overridingParameter, overriddenParameter, parameterSyntax,
isExplicitImplementation: methodSymbol.ExplicitInterfaceImplementations.Any(),
context: c);
}
},
SyntaxKind.MethodDeclaration);
}
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:29,代码来源:MethodOverrideChangedDefaultValue.cs
示例14: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
if (c.SemanticModel.Compilation.IsTest() ||
c.SemanticModel.Compilation.Options.OutputKind != OutputKind.DynamicallyLinkedLibrary)
{
//this rule only makes sense in libraries
return;
}
var awaitExpression = (AwaitExpressionSyntax)c.Node;
var expression = awaitExpression.Expression;
if (expression == null)
{
return;
}
var type = c.SemanticModel.GetTypeInfo(expression).Type;
if (type.Is(KnownType.System_Threading_Tasks_Task))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, expression.GetLocation()));
}
},
SyntaxKind.AwaitExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:27,代码来源:TaskConfigureAwait.cs
示例15: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterCodeBlockStartActionInNonGenerated<SyntaxKind>(
cb =>
{
var methodDeclaration = cb.CodeBlock as MethodDeclarationSyntax;
if (methodDeclaration == null)
{
return;
}
var methodSymbol = cb.OwningSymbol as IMethodSymbol;
if (methodSymbol == null ||
!GetHashCodeEqualsOverride.MethodIsRelevant(methodSymbol, MethodNames))
{
return;
}
cb.RegisterSyntaxNodeAction(
c =>
{
CheckInvocationInsideMethod(c, methodSymbol);
},
SyntaxKind.InvocationExpression);
});
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:26,代码来源:GuardConditionOnEqualsOverride.cs
示例16: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var invocation = (InvocationExpressionSyntax)c.Node;
var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
if (memberAccess == null ||
!invocation.HasExactlyNArguments(1))
{
return;
}
var methodSymbol = c.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
if (!methodSymbol.IsInType(KnownType.System_Type))
{
return;
}
var argument = invocation.ArgumentList.Arguments.First().Expression;
CheckForIsAssignableFrom(c, invocation, memberAccess, methodSymbol, argument);
CheckForIsInstanceOfType(c, invocation, memberAccess, methodSymbol);
},
SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var binary = (BinaryExpressionSyntax)c.Node;
CheckGetTypeAndTypeOfEquality(binary.Left, binary.Right, binary.GetLocation(), c);
CheckGetTypeAndTypeOfEquality(binary.Right, binary.Left, binary.GetLocation(), c);
},
SyntaxKind.EqualsExpression,
SyntaxKind.NotEqualsExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:35,代码来源:GetTypeWithIsAssignableFrom.cs
示例17: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var unaryExpression = (PrefixUnaryExpressionSyntax) c.Node;
var op = unaryExpression.OperatorToken;
var prevToken = op.GetPreviousToken();
var opLocation = op.GetLocation();
var opStartPosition = opLocation.GetLineSpan().StartLinePosition;
var prevStartPosition = prevToken.GetLocation().GetLineSpan().StartLinePosition;
if (prevToken.IsKind(SyntaxKind.EqualsToken) &&
prevStartPosition.Line == opStartPosition.Line &&
prevStartPosition.Character == opStartPosition.Character - 1)
{
c.ReportDiagnostic(Diagnostic.Create(Rule, opLocation, $"{op.Text}="));
}
},
SyntaxKind.UnaryMinusExpression,
SyntaxKind.UnaryPlusExpression,
SyntaxKind.LogicalNotExpression);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:25,代码来源:ReversedOperators.cs
示例18: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSymbolAction(
c =>
{
var symbol = c.Symbol as INamedTypeSymbol;
if (!symbol.IsClass() ||
!symbol.IsAbstract ||
ClassHasInheritedAbstractMembers(symbol))
{
return;
}
if (AbstractClassShouldBeInterface(symbol))
{
ReportClass(symbol, MessageToInterface, c);
return;
}
if (AbstractClassShouldBeConcreteClass(symbol))
{
ReportClass(symbol, MessageToConcreteClass, c);
return;
}
},
SymbolKind.NamedType);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:27,代码来源:ClassShouldNotBeAbstract.cs
示例19: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var method = (MethodDeclarationSyntax)c.Node;
var methodSymbol = c.SemanticModel.GetDeclaredSymbol(method);
if (methodSymbol == null ||
!methodSymbol.IsOverride ||
methodSymbol.OverriddenMethod == null)
{
return;
}
var lastParameter = method.ParameterList.Parameters.LastOrDefault();
if (lastParameter == null)
{
return;
}
var paramsKeyword = lastParameter.Modifiers.FirstOrDefault(
modifier => modifier.IsKind(SyntaxKind.ParamsKeyword));
if (paramsKeyword != default(SyntaxToken) &&
IsNotSemanticallyParams(lastParameter, c.SemanticModel))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, paramsKeyword.GetLocation()));
}
},
SyntaxKind.MethodDeclaration);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:32,代码来源:MethodOverrideAddsParams.cs
示例20: Initialize
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var declaration = (LocalDeclarationStatementSyntax) c.Node;
var variables = declaration.Declaration.Variables;
List<ISymbol> members = null;
foreach (var variable in variables)
{
var variableSymbol = c.SemanticModel.GetDeclaredSymbol(variable);
if (variableSymbol == null)
{
return;
}
if (members == null)
{
members = GetMembers(variableSymbol.ContainingType);
}
ReportOnVariableMatchingField(members, variable.Identifier, c);
}
},
SyntaxKind.LocalDeclarationStatement);
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:27,代码来源:VariableShadowsField.cs
注:本文中的SonarAnalysisContext类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论