Skip to content

Commit

Permalink
Implement analyzer/fixer: remove unnecessary call
Browse files Browse the repository at this point in the history
  • Loading branch information
tats-u committed Apr 25, 2024
1 parent e461b9b commit 78de9a8
Show file tree
Hide file tree
Showing 24 changed files with 628 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Analyzers/Core/Analyzers/EnforceOnBuildValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ internal static class EnforceOnBuildValues
public const EnforceOnBuild ConvertTypeOfToNameOf = /*IDE0082*/ EnforceOnBuild.Recommended;
public const EnforceOnBuild UseNotPattern = /*IDE0083*/ EnforceOnBuild.Recommended;
public const EnforceOnBuild UseIsNotExpression = /*IDE0084*/ EnforceOnBuild.Recommended;
public const EnforceOnBuild RemoveUnnecessaryCall = /*IDE0085*/ EnforceOnBuild.Recommended;
public const EnforceOnBuild UseImplicitObjectCreation = /*IDE0090*/ EnforceOnBuild.Recommended;
public const EnforceOnBuild RemoveRedundantEquality = /*IDE0100*/ EnforceOnBuild.Recommended;
public const EnforceOnBuild RemoveUnnecessaryDiscardDesignation = /*IDE0110*/ EnforceOnBuild.Recommended;
Expand Down
2 changes: 2 additions & 0 deletions src/Analyzers/Core/Analyzers/IDEDiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ internal static class IDEDiagnosticIds
public const string UseNotPatternDiagnosticId = "IDE0083";
public const string UseIsNotExpressionDiagnosticId = "IDE0084";

public const string RemoveUnnecessaryCallDiagnosticId = "IDE0085";

public const string UseImplicitObjectCreationDiagnosticId = "IDE0090";

public const string RemoveRedundantEqualityDiagnosticId = "IDE0100";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ internal static class PredefinedCodeFixProviderNames
public const string RemoveSharedFromModuleMembers = nameof(RemoveSharedFromModuleMembers);
public const string RemoveUnnecessaryAttributeSuppressions = nameof(RemoveUnnecessaryAttributeSuppressions);
public const string RemoveUnnecessaryByVal = nameof(RemoveUnnecessaryByVal);
public const string RemoveUnnecessaryCall = nameof(RemoveUnnecessaryCall);
public const string RemoveUnnecessaryCast = nameof(RemoveUnnecessaryCast);
public const string RemoveUnnecessaryDiscardDesignation = nameof(RemoveUnnecessaryDiscardDesignation);
public const string RemoveUnnecessaryImports = nameof(RemoveUnnecessaryImports);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
' See the LICENSE file in the project root for more information.

Imports Microsoft.CodeAnalysis.CodeStyle
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax

Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnnecessaryCall
<DiagnosticAnalyzer(LanguageNames.VisualBasic)>
Friend NotInheritable Class VisualBasicRemoveUnnecessaryCallDiagnosticAnalyzer
Inherits AbstractBuiltInUnnecessaryCodeStyleDiagnosticAnalyzer

Public Sub New()
MyBase.New(
diagnosticId:=IDEDiagnosticIds.RemoveUnnecessaryCallDiagnosticId,
enforceOnBuild:=EnforceOnBuildValues.RemoveUnnecessaryCall,
[option]:=Nothing,
fadingOption:=Nothing,
title:=New LocalizableResourceString(NameOf(VisualBasicAnalyzersResources.Remove_Call), VisualBasicAnalyzersResources.ResourceManager, GetType(VisualBasicAnalyzersResources)))
End Sub

Protected Overrides Sub InitializeWorker(context As AnalysisContext)
context.RegisterSyntaxNodeAction(
Sub(syntaxContext As SyntaxNodeAnalysisContext)
If ShouldSkipAnalysis(syntaxContext, notification:=Nothing) Then
Return
End If

Dim callSyntax = DirectCast(syntaxContext.Node, CallStatementSyntax)
Dim root = callSyntax.Invocation
Dim rootAsInvocation = TryCast(root, InvocationExpressionSyntax)
Dim rootAsMemberAccess As MemberAccessExpressionSyntax
Dim rootAsConditionalAccess As ConditionalAccessExpressionSyntax
If rootAsInvocation IsNot Nothing Then
root = rootAsInvocation.Expression
Else
rootAsMemberAccess = TryCast(root, MemberAccessExpressionSyntax)
If rootAsmemberAccess IsNot Nothing Then
root = rootAsMemberAccess.Expression
Else
rootAsConditionalAccess = TryCast(root, ConditionalAccessExpressionSyntax)
If rootAsConditionalAccess IsNot Nothing Then
root = rootAsConditionalAccess.Expression
Else
Debug.Fail(
"Invocation expression node (type: " & root.GetTypeDisplayName() & ") is not " &
NameOf(InvocationExpressionSyntax) & ", " &
NameOf(MemberAccessExpressionSyntax) & ", or " &
NameOf(ConditionalAccessExpressionSyntax) & "."
)
End If
End If
End If
While True
rootAsMemberAccess = TryCast(root, MemberAccessExpressionSyntax)
rootAsInvocation = TryCast(root, InvocationExpressionSyntax)
rootAsConditionalAccess = TryCast(root, ConditionalAccessExpressionSyntax)
If rootAsMemberAccess Is Nothing AndAlso
rootAsInvocation Is Nothing AndAlso
rootAsConditionalAccess Is Nothing Then
Exit While
End If
root = If(
rootAsMemberAccess?.Expression,
If(
rootAsInvocation?.Expression,
rootAsConditionalAccess?.Expression
)
)
End While

' Refer to ParseAssignmentOrInvocationStatement() calls in src\Compilers\VisualBasic\Portable\Parser\Parser.vb
' Adopt syntax kind types that wrap them.
If Not root.IsKind(
SyntaxKind.IdentifierName, ' Appended
SyntaxKind.MyBaseExpression,
SyntaxKind.MyClassExpression,
SyntaxKind.MeExpression,
SyntaxKind.GlobalName,
SyntaxKind.PredefinedType,
SyntaxKind.PredefinedCastExpression,
SyntaxKind.DirectCastExpression,
SyntaxKind.TryCastExpression,
SyntaxKind.CTypeExpression,
SyntaxKind.GetTypeExpression,
SyntaxKind.GetXmlNamespaceExpression
) Then
Return
End If
syntaxContext.ReportDiagnostic(Diagnostic.Create(Descriptor, callSyntax.CallKeyword.GetLocation(), additionalLocations:={callSyntax.GetLocation()}))
End Sub, SyntaxKind.CallStatement)
End Sub

Public Overrides Function GetAnalyzerCategory() As DiagnosticAnalyzerCategory
Return DiagnosticAnalyzerCategory.SemanticSpanAnalysis
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Compile Include="$(MSBuildThisFileDirectory)QualifyMemberAccess\VisualBasicQualifyMemberAccessDiagnosticAnalyzer.vb" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveRedundantEquality\VisualBasicRemoveRedundantEqualityDiagnosticAnalyzer.vb" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessaryByVal\VisualBasicRemoveUnnecessaryByValDiagnosticAnalyzer.vb" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessaryCall\VisualBasicRemoveUnnecessaryCallDiagnosticAnalyzer.vb" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessaryCast\VisualBasicRemoveUnnecessaryCastDiagnosticAnalyzer.vb" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessarySuppressions\VisualBasicRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.vb" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessarySuppressions\VisualBasicRemoveUnnecessaryAttributeSuppressionsDiagnosticAnalyzer.vb" />
Expand Down Expand Up @@ -67,4 +68,7 @@
<ItemGroup Condition="'$(DefaultLanguageSourceExtension)' != '' AND '$(BuildingInsideVisualStudio)' != 'true'">
<ExpectedCompile Include="$(MSBuildThisFileDirectory)**\*$(DefaultLanguageSourceExtension)" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)RemoveUnnecessaryCall\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,8 @@
<data name="Object_creation_can_be_simplified" xml:space="preserve">
<value>Object creation can be simplified</value>
</data>
<data name="Remove_Call" xml:space="preserve">
<value>'Call' keyword is unnecessary here and can be removed.</value>
<comment>{locked: Call}This is a Visual Basic keyword and should not be localized or have its casing changed</comment>
</data>
</root>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 78de9a8

Please sign in to comment.