Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Jun 27, 2024
1 parent 61d3000 commit 0fc3570
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,65 +44,47 @@ protected override void Initialize(SonarAnalysisContext context)
private static void CasePatternSwitchLabel(SonarSyntaxNodeReportingContext analysisContext)
{
var casePatternSwitch = (CasePatternSwitchLabelSyntaxWrapper)analysisContext.Node;
if (casePatternSwitch.SyntaxNode.GetFirstNonParenthesizedParent().GetFirstNonParenthesizedParent() is not SwitchStatementSyntax parentSwitchStatement)
if (casePatternSwitch.SyntaxNode.GetFirstNonParenthesizedParent().GetFirstNonParenthesizedParent() is SwitchStatementSyntax parentSwitchStatement)
{
return;
ProcessPatternExpression(analysisContext, casePatternSwitch.Pattern, parentSwitchStatement.Expression, parentSwitchStatement);
}
ProcessPatternExpression(analysisContext,
casePatternSwitch.Pattern,
parentSwitchStatement.Expression,
parentSwitchStatement);
}

private static void SwitchExpressionArm(SonarSyntaxNodeReportingContext analysisContext)
{
var isSwitchExpression = (SwitchExpressionArmSyntaxWrapper)analysisContext.Node;
var parent = isSwitchExpression.SyntaxNode.GetFirstNonParenthesizedParent();

if (!parent.IsKind(SyntaxKindEx.SwitchExpression))
if (parent.IsKind(SyntaxKindEx.SwitchExpression))
{
return;
var switchExpression = (SwitchExpressionSyntaxWrapper)parent;
ProcessPatternExpression(analysisContext, isSwitchExpression.Pattern, switchExpression.GoverningExpression, isSwitchExpression);
}
var switchExpression = (SwitchExpressionSyntaxWrapper)parent;
ProcessPatternExpression(analysisContext,
isSwitchExpression.Pattern,
switchExpression.GoverningExpression,
isSwitchExpression);
}

private static void IsPatternExpression(SonarSyntaxNodeReportingContext analysisContext)
{
var isPatternExpression = (IsPatternExpressionSyntaxWrapper)analysisContext.Node;
if (isPatternExpression.SyntaxNode.GetFirstNonParenthesizedParent() is not IfStatementSyntax parentIfStatement)
if (isPatternExpression.SyntaxNode.GetFirstNonParenthesizedParent() is IfStatementSyntax parentIfStatement)
{
return;
ProcessPatternExpression(analysisContext, isPatternExpression.Pattern, isPatternExpression.Expression, parentIfStatement.Statement);
}
ProcessPatternExpression(analysisContext,
isPatternExpression.Pattern,
isPatternExpression.Expression,
parentIfStatement.Statement);
}

private static void IsExpression(SonarSyntaxNodeReportingContext analysisContext)
{
var isExpression = (BinaryExpressionSyntax)analysisContext.Node;

if (isExpression.Right is not TypeSyntax castType
|| isExpression.GetFirstNonParenthesizedParent() is not IfStatementSyntax parentIfStatement
|| analysisContext.SemanticModel.GetSymbolInfo(castType).Symbol is not INamedTypeSymbol castTypeSymbol
|| castTypeSymbol.TypeKind == TypeKind.Struct)
if (isExpression.Right is TypeSyntax castType
&& isExpression.GetFirstNonParenthesizedParent() is IfStatementSyntax parentIfStatement
&& analysisContext.SemanticModel.GetSymbolInfo(castType).Symbol is INamedTypeSymbol castTypeSymbol
&& castTypeSymbol.TypeKind != TypeKind.Struct)
{
return;
ReportPatternAtMainVariable(analysisContext, isExpression.Left, isExpression.GetLocation(), parentIfStatement.Statement, castType, ReplaceWithAsAndNullCheckMessage);
}

ReportPatternAtMainVariable(analysisContext, isExpression.Left, isExpression.GetLocation(), parentIfStatement.Statement, castType, ReplaceWithAsAndNullCheckMessage);
}

private static List<Location> GetDuplicatedCastLocations(SonarSyntaxNodeReportingContext context, SyntaxNode parentStatement, TypeSyntax castType, SyntaxNode typedVariable)
{
var typeExpressionSymbol = context.SemanticModel.GetSymbolInfo(typedVariable).Symbol
?? context.SemanticModel.GetDeclaredSymbol(typedVariable);

var typeExpressionSymbol = context.SemanticModel.GetSymbolInfo(typedVariable).Symbol ?? context.SemanticModel.GetDeclaredSymbol(typedVariable);
return typeExpressionSymbol is null
? []
: parentStatement
Expand All @@ -117,10 +99,7 @@ bool IsCastOnSameSymbol(CastExpressionSyntax castExpression) =>
Equals(context.SemanticModel.GetSymbolInfo(castExpression.Expression).Symbol, typeExpressionSymbol);
}

private static void ProcessPatternExpression(SonarSyntaxNodeReportingContext analysisContext,
SyntaxNode isPattern,
SyntaxNode mainVariableExpression,
SyntaxNode parentStatement)
private static void ProcessPatternExpression(SonarSyntaxNodeReportingContext analysisContext, SyntaxNode isPattern, SyntaxNode mainVariableExpression, SyntaxNode parentStatement)
{
var objectToPattern = new Dictionary<ExpressionSyntax, SyntaxNode>();
PatternExpressionObjectToPatternMapping.MapObjectToPattern((ExpressionSyntax)mainVariableExpression.RemoveParentheses(), isPattern.RemoveParentheses(), objectToPattern);
Expand All @@ -136,15 +115,15 @@ private static void ProcessPatternExpression(SonarSyntaxNodeReportingContext ana
{
rightPartsToCheck.Add(declarationPattern.Designation.SyntaxNode, new Tuple<TypeSyntax, Location>(declarationPattern.Type, subPattern.GetLocation()));
}
else if ((RecursivePatternSyntaxWrapper)subPattern is { Designation.SyntaxNode: { }, Type: { }} recursivePattern)
else if ((RecursivePatternSyntaxWrapper)subPattern is { Designation.SyntaxNode: { }, Type: { } } recursivePattern)
{
rightPartsToCheck.Add(recursivePattern.Designation.SyntaxNode, new Tuple<TypeSyntax, Location>(recursivePattern.Type, subPattern.GetLocation()));
}
}

var mainVarMsg = rightPartsToCheck.Any()
? RemoveRedundantCastAnotherVariableMessage
: RemoveRedundantCastMessage;
? RemoveRedundantCastAnotherVariableMessage
: RemoveRedundantCastMessage;
foreach (var targetType in targetTypes)
{
ReportPatternAtMainVariable(analysisContext, leftVariable, leftVariable.GetLocation(), parentStatement, targetType, mainVarMsg);
Expand Down Expand Up @@ -232,8 +211,7 @@ private static void ReportPatternAtCastLocation(SonarSyntaxNodeReportingContext
TypeSyntax castType,
string message)
{
if (context.SemanticModel.GetSymbolInfo(castType).Symbol is INamedTypeSymbol castTypeSymbol
&& castTypeSymbol.TypeKind != TypeKind.Struct)
if (context.SemanticModel.GetSymbolInfo(castType).Symbol is INamedTypeSymbol castTypeSymbol && castTypeSymbol.TypeKind != TypeKind.Struct)
{
var duplicatedCastLocations = GetDuplicatedCastLocations(context, parentStatement, castType, variableExpression);
foreach (var castLocation in duplicatedCastLocations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace SonarAnalyzer.Test.Rules;
public class CastShouldNotBeDuplicatedTest
{
private static readonly VerifierBuilder Builder = new VerifierBuilder<CastShouldNotBeDuplicated>();

public TestContext TestContext { get; set; }

[TestMethod]
Expand Down

0 comments on commit 0fc3570

Please sign in to comment.