Skip to content

Commit

Permalink
NET-1033 AnalysisContext: Rename SemanticModel to Model
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource authored and sonartech committed Jan 24, 2025
1 parent 53c2810 commit 7df445f
Show file tree
Hide file tree
Showing 354 changed files with 720 additions and 720 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ context.Node is CompilationUnitSyntax compilationUnitSyntax
&& context.ContainingSymbol.IsGlobalNamespace(); // Needed to avoid the duplicate calls from Roslyn 4.0.0

public static bool IsInExpressionTree(this SonarSyntaxNodeReportingContext context) =>
context.Node.IsInExpressionTree(context.SemanticModel);
context.Node.IsInExpressionTree(context.Model);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
{
var name = ((MemberAccessExpressionSyntax)c.Node).Name;
if (name.Identifier.ValueText == nameof(List<int>.ForEach)
&& c.SemanticModel.GetSymbolInfo(name).Symbol is IMethodSymbol method
&& c.Model.GetSymbolInfo(name).Symbol is IMethodSymbol method
&& method.Is(KnownType.System_Collections_Generic_List_T, nameof(List<int>.ForEach)))
{
c.ReportIssue(Rule, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
if (c.Node is SimpleLambdaExpressionSyntax { Parameter: { Identifier.ValueText: not ("x" or "_") } parameter, Parent: { } parent } lambda
&& parent.FirstAncestorOrSelf<LambdaExpressionSyntax>() is null
&& !IsSonarContextAction(c)
&& !MatchesDelegateParameterName(c.SemanticModel, lambda))
&& !MatchesDelegateParameterName(c.Model, lambda))
{
c.ReportIssue(Rule, parameter);
}
Expand All @@ -48,7 +48,7 @@ private static bool IsFuncOrAction(INamedTypeSymbol delegateType) =>
delegateType.IsAny(KnownType.System_Func_T_TResult, KnownType.System_Action_T);

private static bool IsSonarContextAction(SonarSyntaxNodeReportingContext context) =>
context.SemanticModel.GetSymbolInfo(context.Node).Symbol is IMethodSymbol lambda
context.Model.GetSymbolInfo(context.Node).Symbol is IMethodSymbol lambda
&& lambda.ReturnsVoid
&& IsSonarContextName(lambda.Parameters.Single().Type.Name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public sealed class UseNullInsteadOfDefault : StylingAnalyzer
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(c =>
{
if (IsReferenceType(c.Node, c.SemanticModel))
if (IsReferenceType(c.Node, c.Model))
{
c.ReportIssue(Rule, c.Node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void Verify(
{
var memberAccess = (MemberAccessExpressionSyntax)c.Node;
if (memberAccess.NameIs(methodName)
&& c.SemanticModel.GetSymbolInfo(memberAccess).Symbol is IMethodSymbol method
&& c.Model.GetSymbolInfo(memberAccess).Symbol is IMethodSymbol method
&& method.ContainingType.Is(KnownType.System_Text_RegularExpressions_Regex))
{
c.ReportIssue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected override void Initialize(SonarAnalysisContext context)
private void ValidateDeclaration(SonarSyntaxNodeReportingContext context, SyntaxToken identifier)
{
if (FindRename(identifier.ValueText) is { } name
&& context.SemanticModel.GetDeclaredSymbol(context.Node).GetSymbolType() is { } type
&& context.Model.GetDeclaredSymbol(context.Node).GetSymbolType() is { } type
&& type.Name == name.TypeName)
{
context.ReportIssue(Rule, identifier, identifier.ValueText.Replace(name.UsedName, name.SuggestedName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected override void Initialize(SonarAnalysisContext context)
base.Initialize(context);
context.RegisterNodeAction(c =>
{
if (IsDateTimeConstructorWithoutKindParameter(c.Node, c.SemanticModel))
if (IsDateTimeConstructorWithoutKindParameter(c.Node, c.Model))
{
c.ReportIssue(Rule, c.Node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override void Initialize(SonarAnalysisContext context)
var assignment = (AssignmentExpressionSyntax)c.Node;


if (c.SemanticModel.GetSymbolInfo(assignment.Left).Symbol is IEventSymbol @event &&
if (c.Model.GetSymbolInfo(assignment.Left).Symbol is IEventSymbol @event &&
assignment.Right is AnonymousFunctionExpressionSyntax)
{
c.ReportIssue(rule, assignment.OperatorToken.CreateLocation(assignment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public sealed class ArgumentSpecifiedForCallerInfoParameter : SonarDiagnosticAna
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(c =>
{
if (new CSharpMethodParameterLookup((InvocationExpressionSyntax)c.Node, c.SemanticModel) is { MethodSymbol: { } } methodParameterLookup
if (new CSharpMethodParameterLookup((InvocationExpressionSyntax)c.Node, c.Model) is { MethodSymbol: { } } methodParameterLookup
&& methodParameterLookup.GetAllArgumentParameterMappings() is { } argumentMappings)
{
foreach (var argumentMapping in argumentMappings.Where(x =>
x.Symbol.GetAttributes(CallerInfoAttributesToReportOn).Any()
&& !IsArgumentPassthroughOfParameter(c.SemanticModel, x.Node, x.Symbol)))
&& !IsArgumentPassthroughOfParameter(c.Model, x.Node, x.Symbol)))
{
c.ReportIssue(Rule, argumentMapping.Node);
}
Expand Down
10 changes: 5 additions & 5 deletions analyzers/src/SonarAnalyzer.CSharp/Rules/ArrayCovariance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ protected override void Initialize(SonarAnalysisContext context)
private static void RaiseOnArrayCovarianceInSimpleAssignmentExpression(SonarSyntaxNodeReportingContext context)
{
var assignment = (AssignmentExpressionSyntax)context.Node;
VerifyExpression(assignment.Right, context.SemanticModel.GetTypeInfo(assignment.Left).Type, context);
VerifyExpression(assignment.Right, context.Model.GetTypeInfo(assignment.Left).Type, context);
}

private static void RaiseOnArrayCovarianceInVariableDeclaration(SonarSyntaxNodeReportingContext context)
{
var variableDeclaration = (VariableDeclarationSyntax)context.Node;
var baseType = context.SemanticModel.GetTypeInfo(variableDeclaration.Type).Type;
var baseType = context.Model.GetTypeInfo(variableDeclaration.Type).Type;

foreach (var declaration in variableDeclaration.Variables.Where(syntax => syntax.Initializer != null))
{
Expand All @@ -53,7 +53,7 @@ private static void RaiseOnArrayCovarianceInVariableDeclaration(SonarSyntaxNodeR
private static void RaiseOnArrayCovarianceInInvocationExpression(SonarSyntaxNodeReportingContext context)
{
var invocation = (InvocationExpressionSyntax)context.Node;
var methodParameterLookup = new CSharpMethodParameterLookup(invocation, context.SemanticModel);
var methodParameterLookup = new CSharpMethodParameterLookup(invocation, context.Model);

foreach (var argument in invocation.ArgumentList.Arguments)
{
Expand All @@ -69,14 +69,14 @@ private static void RaiseOnArrayCovarianceInInvocationExpression(SonarSyntaxNode
private static void RaiseOnArrayCovarianceInCastExpression(SonarSyntaxNodeReportingContext context)
{
var castExpression = (CastExpressionSyntax)context.Node;
var baseType = context.SemanticModel.GetTypeInfo(castExpression.Type).Type;
var baseType = context.Model.GetTypeInfo(castExpression.Type).Type;

VerifyExpression(castExpression.Expression, baseType, context);
}

private static void VerifyExpression(SyntaxNode node, ITypeSymbol baseType, SonarSyntaxNodeReportingContext context)
{
foreach (var pair in GetPossibleTypes(node, context.SemanticModel).Where(pair => AreCovariantArrayTypes(pair.Symbol, baseType)))
foreach (var pair in GetPossibleTypes(node, context.Model).Where(pair => AreCovariantArrayTypes(pair.Symbol, baseType)))
{
context.ReportIssue(Rule, pair.Node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
symbolStartContext.RegisterSyntaxNodeAction(c =>
{
var methodNode = (MethodDeclarationSyntax)c.Node;
var methodSymbol = c.SemanticModel.GetDeclaredSymbol(methodNode);
var methodSymbol = c.Model.GetDeclaredSymbol(methodNode);
var methodAttributes = methodSymbol.GetAttributesWithInherited();

if (methodSymbol.IsControllerActionMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override void Initialize(SonarAnalysisContext context) =>

private static void ProcessControllerMethods(SonarSyntaxNodeReportingContext context, ConcurrentDictionary<ITypeSymbol, bool> examinedTypes)
{
if (context.SemanticModel.GetDeclaredSymbol(context.Node) is IMethodSymbol method
if (context.Model.GetDeclaredSymbol(context.Node) is IMethodSymbol method
&& method.IsControllerActionMethod())
{
var modelParameterTypes = method.Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ private static void ProcessCodeBlock(SonarCodeBlockStartAnalysisContext<SyntaxKi
{
if (!isModelValidated)
{
isModelValidated = IsCheckingValidityProperty(nodeContext.Node, nodeContext.SemanticModel);
isModelValidated = IsCheckingValidityProperty(nodeContext.Node, nodeContext.Model);
}
}, PropertyAccessSyntaxNodesToVisit);
codeBlockContext.RegisterNodeAction(nodeContext =>
{
if (!isModelValidated)
{
isModelValidated = IsTryValidateInvocation(nodeContext.Node, nodeContext.SemanticModel);
isModelValidated = IsTryValidateInvocation(nodeContext.Node, nodeContext.Model);
}
}, SyntaxKind.InvocationExpression);
codeBlockContext.RegisterCodeBlockEndAction(blockEnd =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
symbolStart.RegisterSyntaxNodeAction(nodeContext =>
{
var methodDeclaration = (MethodDeclarationSyntax)nodeContext.Node;
if (nodeContext.SemanticModel.GetDeclaredSymbol(methodDeclaration, nodeContext.Cancel) is { } method
if (nodeContext.Model.GetDeclaredSymbol(methodDeclaration, nodeContext.Cancel) is { } method
&& !method.ContainingType.IsAbstract
&& method.IsControllerActionMethod()
&& method.GetAttributesWithInherited().Any(x => !CanBeIgnored(x.GetAttributeRouteTemplate())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void RegisterCodeBlockActions(
codeBlockStart.RegisterNodeAction(nodeContext =>
{
var argument = (ArgumentSyntax)nodeContext.Node;
var model = nodeContext.SemanticModel;
var model = nodeContext.Model;
if (allConstantAccesses
&& AddMatchingArgumentToCandidates(model, codeBlockCandidates, argument, argumentDescriptors)
&& model.GetConstantValue(argument.Expression) is not { HasValue: true, Value: string })
Expand All @@ -110,12 +110,12 @@ private void RegisterCodeBlockActions(
// The property access of Request.Form.Files can be replaced by an IFormFile binding.
// Any access to a "Files" property is therefore noncompliant. This is different from the Argument handling above.
var memberAccess = (MemberAccessExpressionSyntax)nodeContext.Node;
var context = new PropertyAccessContext(memberAccess, nodeContext.SemanticModel, memberAccess.Name.Identifier.ValueText);
var context = new PropertyAccessContext(memberAccess, nodeContext.Model, memberAccess.Name.Identifier.ValueText);
if (Language.Tracker.PropertyAccess.MatchProperty(propertyAccessDescriptors)(context)
// form.Files is okay, if "form" is a parameter, because IFormCollection binding is considered appropriate for binding as well
&& nodeContext.SemanticModel.GetSymbolInfo(memberAccess.Expression).Symbol is not IParameterSymbol)
&& nodeContext.Model.GetSymbolInfo(memberAccess.Expression).Symbol is not IParameterSymbol)
{
codeBlockCandidates.Push(new(UseIFormFileBindingMessage, memberAccess.GetLocation(), IsOriginatingFromParameter(nodeContext.SemanticModel, memberAccess)));
codeBlockCandidates.Push(new(UseIFormFileBindingMessage, memberAccess.GetLocation(), IsOriginatingFromParameter(nodeContext.Model, memberAccess)));
}
}, SyntaxKind.SimpleMemberAccessExpression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ protected override void Initialize(SonarAnalysisContext context) =>
{
if (c.Node is InvocationExpressionSyntax { ArgumentList.Arguments.Count: >= 2 } invocation
&& GetParameters(invocation.GetName()) is { } knownAssertParameters
&& c.SemanticModel.GetSymbolInfo(invocation).AllSymbols()
&& c.Model.GetSymbolInfo(invocation).AllSymbols()
.SelectMany(symbol =>
symbol is IMethodSymbol { IsStatic: true, ContainingSymbol: INamedTypeSymbol container } methodSymbol
? knownAssertParameters.Select(knownParameters => FindWrongArguments(c.SemanticModel, container, methodSymbol, invocation, knownParameters))
? knownAssertParameters.Select(knownParameters => FindWrongArguments(c.Model, container, methodSymbol, invocation, knownParameters))
: Enumerable.Empty<WrongArguments?>())
.FirstOrDefault(x => x is not null) is (Expected: var expected, Actual: var actual))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
start.RegisterNodeAction(c =>
CheckInvocation(c, invocation =>
invocation.NameIs("Should")
&& c.SemanticModel.GetSymbolInfo(invocation).AllSymbols().Any(x =>
&& c.Model.GetSymbolInfo(invocation).AllSymbols().Any(x =>
x is IMethodSymbol
{
IsExtensionMethod: true,
Expand All @@ -52,7 +52,7 @@ x is IMethodSymbol
start.RegisterNodeAction(c =>
CheckInvocation(c, invocation =>
invocation.NameIs("That", "ThatEnum", "ThatCode", "ThatAsyncCode", "ThatDynamic")
&& c.SemanticModel.GetSymbolInfo(invocation) is
&& c.Model.GetSymbolInfo(invocation) is
{
Symbol: IMethodSymbol
{
Expand All @@ -69,7 +69,7 @@ x is IMethodSymbol
start.RegisterNodeAction(c =>
CheckInvocation(c, invocation =>
invocation.NameIs("Received", "DidNotReceive", "ReceivedWithAnyArgs", "DidNotReceiveWithAnyArgs", "ReceivedCalls")
&& c.SemanticModel.GetSymbolInfo(invocation) is
&& c.Model.GetSymbolInfo(invocation) is
{
Symbol: IMethodSymbol
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
c =>
{
var methodDeclaration = (MethodDeclarationSyntax)c.Node;
var methodSymbol = c.SemanticModel.GetDeclaredSymbol(methodDeclaration);
var methodSymbol = c.Model.GetDeclaredSymbol(methodDeclaration);

if (IsViolatingRule(methodSymbol) && !IsExceptionToTheRule(methodDeclaration, methodSymbol))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ protected override void Initialize(SonarParametrizedAnalysisContext context) =>
return;
}

var type = c.SemanticModel.GetDeclaredSymbol(typeDeclaration);
var collector = new TypeDependencyCollector(c.SemanticModel, typeDeclaration);
var type = c.Model.GetDeclaredSymbol(typeDeclaration);
var collector = new TypeDependencyCollector(c.Model, typeDeclaration);
collector.SafeVisit(typeDeclaration);
var dependentTypes = collector.DependentTypes
.SelectMany(ExpandGenericTypes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override void Initialize(SonarParametrizedAnalysisContext context) =>
{
return;
}
var objectTypeInfo = new ObjectTypeInfo(c.Node, c.SemanticModel);
var objectTypeInfo = new ObjectTypeInfo(c.Node, c.Model);
if (objectTypeInfo.Symbol is null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
var node = (LambdaExpressionSyntax)c.Node;

if (IsWithinLoopBody(node)
&& IsWithinRenderTreeBuilderInvocation(node, c.SemanticModel))
&& IsWithinRenderTreeBuilderInvocation(node, c.Model))
{
c.ReportIssue(Rule, node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override void Initialize(SonarAnalysisContext context)

private static void ReportOnObjectEqualsMatches(SonarSyntaxNodeReportingContext context, InvocationExpressionSyntax invocation)
{
var methodSymbol = context.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
var methodSymbol = context.Model.GetSymbolInfo(invocation).Symbol as IMethodSymbol;

var operands = GetOperands(invocation, methodSymbol);
if (operands is not null && CSharpEquivalenceChecker.AreEquivalent(RemoveParentheses(operands.Item1), RemoveParentheses(operands.Item2)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ private void CheckConditional(SonarSyntaxNodeReportingContext context)
{
return;
}
var typeLeft = context.SemanticModel.GetTypeInfo(whenTrue).Type;
var typeRight = context.SemanticModel.GetTypeInfo(whenFalse).Type;
var typeLeft = context.Model.GetTypeInfo(whenTrue).Type;
var typeRight = context.Model.GetTypeInfo(whenFalse).Type;
if (typeLeft.IsNullableBoolean()
|| typeRight.IsNullableBoolean()
|| typeLeft == null
Expand Down
Loading

0 comments on commit 7df445f

Please sign in to comment.