Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA1311 various fixes #6010

Merged
merged 3 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@
<value>Specify current culture</value>
</data>
<data name="UseInvariantVersion" xml:space="preserve">
<value>User an invariant version</value>
<value>Use an invariant version</value>
</data>
<data name="FeatureUnsupportedWhenRuntimeMarshallingDisabledDescription" xml:space="preserve">
<value>Using features that require runtime marshalling when runtime marshalling is disabled will result in runtime exceptions.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Uživatel s neutrální verzí</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Uživatel s neutrální verzí</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Verwenden Sie eine unveränderliche Version</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Verwenden Sie eine unveränderliche Version</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Usuario de una versión invariable</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Usuario de una versión invariable</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Utilisateur avec une version invariante</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Utilisateur avec une version invariante</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Utente di una versione invariante</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Utente di una versione invariante</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">インバリアント バージョンを使用する</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">インバリアント バージョンを使用する</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">사용자 불변 버전</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">사용자 불변 버전</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Użyj niezmiennej wersji</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Użyj niezmiennej wersji</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Usar uma versão invariável</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Usar uma versão invariável</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Использование инвариантной версии</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Использование инвариантной версии</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">Sabit bir sürüm kullanın</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">Sabit bir sürüm kullanın</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">用户固定版本</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">用户固定版本</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,8 @@
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="translated">使用者固定版本</target>
<source>Use an invariant version</source>
<target state="needs-review-translation">使用者固定版本</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,42 @@ End Class
}.RunAsync();
}

[Fact]
public async Task CA1311_FixToLowerBasicAsync_SpecifyCurrentCulture_MemberAccessSyntax()
{
var source = @"
Imports System.Globalization
Class C
Sub M()
Dim a = ""test""
a.[|ToLower|]
a?.[|ToLower|]
Dim b = a.[|ToLower|]
Dim c = a?.[|ToLower|]
End Sub
End Class
";

var fixedSource = @"
Imports System.Globalization
Class C
Sub M()
Dim a = ""test""
a.ToLower(CultureInfo.CurrentCulture)
a?.ToLower(CultureInfo.CurrentCulture)
Dim b = a.ToLower(CultureInfo.CurrentCulture)
Dim c = a?.ToLower(CultureInfo.CurrentCulture)
End Sub
End Class
";
await new VerifyVB.Test
{
TestState = { Sources = { source } },
FixedState = { Sources = { fixedSource } },
CodeActionIndex = 0,
CodeActionEquivalenceKey = nameof(MicrosoftNetCoreAnalyzersResources.SpecifyCurrentCulture),
}.RunAsync();
}

[Fact]
public async Task CA1311_FixToLowerBasicAsync_UseInvariantVersion()
Expand Down Expand Up @@ -157,6 +193,42 @@ End Class
}.RunAsync();
}

[Fact]
public async Task CA1311_FixToLowerBasicAsync_UseInvariantVersion_MemberAccessSyntax()
{
const string source = @"
Class C
Sub M()
Dim a = ""test""
a.[|ToLower|]
a?.[|ToLower|]
Dim b = a.[|ToLower|]
Dim c = a?.[|ToLower|]
End Sub
End Class
";

const string fixedSource = @"
Class C
Sub M()
Dim a = ""test""
a.ToLowerInvariant
a?.ToLowerInvariant
Dim b = a.ToLowerInvariant
Dim c = a?.ToLowerInvariant
End Sub
End Class
";

await new VerifyVB.Test
{
TestState = { Sources = { source } },
FixedState = { Sources = { fixedSource } },
CodeActionIndex = 1,
CodeActionEquivalenceKey = nameof(MicrosoftNetCoreAnalyzersResources.UseInvariantVersion),
}.RunAsync();
}

[Fact]
public async Task CA1311_FixToUpperCSharpAsync_SpecifyCurrentCulture()
{
Expand Down Expand Up @@ -268,6 +340,42 @@ End Class
}.RunAsync();
}

[Fact]
public async Task CA1311_FixToUpperBasicAsync_SpecifyCurrentCulture_MemberAccessSyntax()
{
var source = @"
Imports System.Globalization
Class C
Sub M()
Dim a = ""test""
a.[|ToUpper|]
a?.[|ToUpper|]
Dim b = a.[|ToUpper|]
Dim c = a?.[|ToUpper|]
End Sub
End Class
";

var fixedSource = @"
Imports System.Globalization
Class C
Sub M()
Dim a = ""test""
a.ToUpper(CultureInfo.CurrentCulture)
a?.ToUpper(CultureInfo.CurrentCulture)
Dim b = a.ToUpper(CultureInfo.CurrentCulture)
Dim c = a?.ToUpper(CultureInfo.CurrentCulture)
End Sub
End Class
";
await new VerifyVB.Test
{
TestState = { Sources = { source } },
FixedState = { Sources = { fixedSource } },
CodeActionIndex = 0,
CodeActionEquivalenceKey = nameof(MicrosoftNetCoreAnalyzersResources.SpecifyCurrentCulture),
}.RunAsync();
}

[Fact]
public async Task CA1311_FixToUpperBasicAsync_UseInvariantVersion()
Expand All @@ -290,6 +398,42 @@ Sub M()
a?.ToUpperInvariant()
End Sub
End Class
";

await new VerifyVB.Test
{
TestState = { Sources = { source } },
FixedState = { Sources = { fixedSource } },
CodeActionIndex = 1,
CodeActionEquivalenceKey = nameof(MicrosoftNetCoreAnalyzersResources.UseInvariantVersion),
}.RunAsync();
}

[Fact]
public async Task CA1311_FixToUpperBasicAsync_UseInvariantVersion_MemberAccessSyntax()
{
const string source = @"
Class C
Sub M()
Dim a = ""test""
a.[|ToUpper|]
a?.[|ToUpper|]
Dim b = a.[|ToUpper|]
Dim c = a?.[|ToUpper|]
End Sub
End Class
";

const string fixedSource = @"
Class C
Sub M()
Dim a = ""test""
a.ToUpperInvariant
a?.ToUpperInvariant
Dim b = a.ToUpperInvariant
Dim c = a?.ToUpperInvariant
End Sub
End Class
";

await new VerifyVB.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,41 @@ Namespace Microsoft.NetCore.VisualBasic.Analyzers.Runtime
End Function

Protected Overrides Async Function SpecifyCurrentCultureAsync(document As Document, generator As SyntaxGenerator, root As SyntaxNode, node As SyntaxNode, cancellationToken As CancellationToken) As Task(Of Document)
If node.IsKind(SyntaxKind.IdentifierName) Then
Dim invocation = node.Parent?.FirstAncestorOrSelf(Of InvocationExpressionSyntax)
If invocation IsNot Nothing Then
Dim model = Await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(False)
Dim symbolInfo = model.GetSymbolInfo(node, cancellationToken).Symbol
Dim methodSymbol = TryCast(symbolInfo, IMethodSymbol)

If methodSymbol IsNot Nothing And methodSymbol.Parameters.Length = 0 Then
Dim newArg = generator.Argument(CreateCurrentCultureMemberAccess(generator, model)).WithAdditionalAnnotations(Formatter.Annotation)
Dim newInvocation = invocation.AddArgumentListArguments(DirectCast(newArg, ArgumentSyntax)).WithAdditionalAnnotations(Formatter.Annotation)
Dim newRoot = root.ReplaceNode(invocation, newInvocation)
Return document.WithSyntaxRoot(newRoot)
End If
If ShouldFix(node) Then
Dim memberAccess = DirectCast(node.Parent, MemberAccessExpressionSyntax)

If memberAccess.Parent Is Nothing Or Not memberAccess.Parent.IsKind(SyntaxKind.InvocationExpression) Then
Return Await SpecifyCurrentCultureWhenTheresNoArgumentListAsync(document, generator, root, memberAccess, memberAccess, cancellationToken)
End If

Dim invocation = DirectCast(memberAccess.Parent, InvocationExpressionSyntax)
If invocation.ArgumentList Is Nothing Then
Return Await SpecifyCurrentCultureWhenTheresNoArgumentListAsync(document, generator, root, memberAccess, invocation, cancellationToken)
End If

Dim model = Await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(False)
Dim symbolInfo = model.GetSymbolInfo(node, cancellationToken).Symbol
Dim methodSymbol = TryCast(symbolInfo, IMethodSymbol)

If methodSymbol IsNot Nothing And methodSymbol.Parameters.Length = 0 Then
Dim newArg = generator.Argument(CreateCurrentCultureMemberAccess(generator, model)).WithAdditionalAnnotations(Formatter.Annotation)
Dim newInvocation = invocation.AddArgumentListArguments(DirectCast(newArg, ArgumentSyntax)).WithAdditionalAnnotations(Formatter.Annotation)
Dim newRoot = root.ReplaceNode(invocation, newInvocation)
Return document.WithSyntaxRoot(newRoot)
End If
End If

Return document
End Function

Private Async Function SpecifyCurrentCultureWhenTheresNoArgumentListAsync(document As Document, generator As SyntaxGenerator, root As SyntaxNode, memberAccess As MemberAccessExpressionSyntax, nodeToReplace As SyntaxNode, cancellationToken As CancellationToken) As Task(Of Document)
Dim model = Await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(False)
Dim newArg = generator.Argument(CreateCurrentCultureMemberAccess(generator, model)).WithAdditionalAnnotations(Formatter.Annotation)
Dim invocation = generator.InvocationExpression(memberAccess.WithoutTrailingTrivia(), newArg).WithAdditionalAnnotations(Formatter.Annotation)
Dim newRoot = root.ReplaceNode(nodeToReplace, invocation)
Return document.WithSyntaxRoot(newRoot)
End Function

Protected Overrides Function UseInvariantVersionAsync(document As Document, generator As SyntaxGenerator, root As SyntaxNode, node As SyntaxNode) As Task(Of Document)
If ShouldFix(node) Then
Dim memberAccess = DirectCast(node.Parent, MemberAccessExpressionSyntax)
Expand Down
Loading