Skip to content

Commit

Permalink
Use ToLowerInvariant Or ToUpperInvariant Analyzer (#2186)
Browse files Browse the repository at this point in the history
  • Loading branch information
TKharaishvili authored Apr 15, 2022
1 parent 5b0b341 commit 334bb06
Show file tree
Hide file tree
Showing 28 changed files with 1,152 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.NetCore.Analyzers.Runtime;

namespace Microsoft.NetCore.CSharp.Analyzers.Runtime
{
[ExportCodeFixProvider(LanguageNames.CSharp), Shared]
public class CSharpSpecifyCultureForToLowerAndToUpperFixer : SpecifyCultureForToLowerAndToUpperFixerBase
{
protected override bool ShouldFix(SyntaxNode node)
{
return node.IsKind(SyntaxKind.IdentifierName) &&
(node.Parent?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true || node.Parent?.IsKind(SyntaxKind.MemberBindingExpression) == true);
}

protected override async Task<Document> SpecifyCurrentCultureAsync(Document document, SyntaxGenerator generator, SyntaxNode root, SyntaxNode node, CancellationToken cancellationToken)
{
if (node.IsKind(SyntaxKind.IdentifierName) && node.Parent?.FirstAncestorOrSelf<InvocationExpressionSyntax>() is InvocationExpressionSyntax invocation)
{
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (model.GetSymbolInfo((IdentifierNameSyntax)node, cancellationToken).Symbol is IMethodSymbol methodSymbol && methodSymbol.Parameters.Length == 0)
{
var newArg = generator.Argument(CreateCurrentCultureMemberAccess(generator, model)).WithAdditionalAnnotations(Formatter.Annotation);
var newInvocation = invocation.AddArgumentListArguments((ArgumentSyntax)newArg).WithAdditionalAnnotations(Formatter.Annotation);
var newRoot = root.ReplaceNode(invocation, newInvocation);
return document.WithSyntaxRoot(newRoot);
}
}

return document;
}

protected override Task<Document> UseInvariantVersionAsync(Document document, SyntaxGenerator generator, SyntaxNode root, SyntaxNode node)
{
if (node.IsKind(SyntaxKind.IdentifierName))
{
if (node.Parent is MemberAccessExpressionSyntax memberAccess)
{
var replacementMethodName = GetReplacementMethodName(memberAccess.Name.Identifier.Text);
var newMemberAccess = memberAccess.WithName((SimpleNameSyntax)generator.IdentifierName(replacementMethodName)).WithAdditionalAnnotations(Formatter.Annotation);
var newRoot = root.ReplaceNode(memberAccess, newMemberAccess);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}

if (node.Parent is MemberBindingExpressionSyntax memberBinding)
{
var replacementMethodName = GetReplacementMethodName(memberBinding.Name.Identifier.Text);
var newMemberBinding = memberBinding.WithName((SimpleNameSyntax)generator.IdentifierName(replacementMethodName)).WithAdditionalAnnotations(Formatter.Annotation);
var newRoot = root.ReplaceNode(memberBinding, newMemberBinding);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
}

return Task.FromResult(document);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.NetCore.Analyzers.Runtime;

namespace Microsoft.NetCore.CSharp.Analyzers.Runtime
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CSharpSpecifyCultureForToLowerAndToUpperAnalyzer : SpecifyCultureForToLowerAndToUpperAnalyzer
{
protected override Location GetMethodNameLocation(SyntaxNode invocationNode)
{
Debug.Assert(invocationNode.IsKind(SyntaxKind.InvocationExpression));

var invocation = (InvocationExpressionSyntax)invocationNode;
if (invocation.Expression is MemberAccessExpressionSyntax memberAccess)
{
return memberAccess.Name.GetLocation();
}
else if (invocation.Expression is MemberBindingExpressionSyntax memberBinding)
{
return memberBinding.Name.GetLocation();
}
return invocation.GetLocation();
}
}
}
1 change: 1 addition & 0 deletions src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
CA1311 | Globalization | Hidden | SpecifyCultureForToLowerAndToUpper, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1311)
CA1420 | Interoperability | Warning | FeatureUnsupportedWhenRuntimeMarshallingDisabled, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1420)
CA1421 | Interoperability | Info | MethodUsesRuntimeMarshallingEvenWhenMarshallingDisabled, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1421)
CA1849 | Performance | Disabled | UseAsyncMethodInAsyncContext, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1849)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,21 @@
<data name="UsesPreviewTypeParameterMessage" xml:space="preserve">
<value>'{0}' uses the preview type '{1}' and needs to opt into preview features. See {2} for more information.</value>
</data>
<data name="SpecifyCultureForToLowerAndToUpperTitle" xml:space="preserve">
<value>Specify a culture or use an invariant version</value>
</data>
<data name="SpecifyCultureForToLowerAndToUpperDescription" xml:space="preserve">
<value>Specify culture to help avoid accidental implicit dependency on current culture. Using an invariant version yields consistent results regardless of the culture of an application.</value>
</data>
<data name="SpecifyCultureForToLowerAndToUpperMessage" xml:space="preserve">
<value>Specify a culture or use an invariant version to avoid implicit dependency on current culture</value>
</data>
<data name="SpecifyCurrentCulture" xml:space="preserve">
<value>Specify current culture</value>
</data>
<data name="UseInvariantVersion" xml:space="preserve">
<value>User 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>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;

namespace Microsoft.NetCore.Analyzers.Runtime
{
public abstract class SpecifyCultureForToLowerAndToUpperFixerBase : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(SpecifyCultureForToLowerAndToUpperAnalyzer.RuleId);

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span);

if (ShouldFix(node))
{
var generator = SyntaxGenerator.GetGenerator(context.Document);

var title = MicrosoftNetCoreAnalyzersResources.SpecifyCurrentCulture;
context.RegisterCodeFix(CodeAction.Create(title,
async ct => await SpecifyCurrentCultureAsync(context.Document, generator, root, node, ct).ConfigureAwait(false),
equivalenceKey: nameof(MicrosoftNetCoreAnalyzersResources.SpecifyCurrentCulture)),
context.Diagnostics);

title = MicrosoftNetCoreAnalyzersResources.UseInvariantVersion;
context.RegisterCodeFix(CodeAction.Create(title,
async ct => await UseInvariantVersionAsync(context.Document, generator, root, node).ConfigureAwait(false),
equivalenceKey: nameof(MicrosoftNetCoreAnalyzersResources.UseInvariantVersion)),
context.Diagnostics);
}
}

protected abstract bool ShouldFix(SyntaxNode node);

protected abstract Task<Document> SpecifyCurrentCultureAsync(Document document, SyntaxGenerator generator, SyntaxNode root, SyntaxNode node, CancellationToken cancellationToken);

protected static SyntaxNode CreateCurrentCultureMemberAccess(SyntaxGenerator generator, SemanticModel model)
{
var cultureInfoType = model.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemGlobalizationCultureInfo)!;
return generator.MemberAccessExpression(
generator.TypeExpressionForStaticMemberAccess(cultureInfoType),
generator.IdentifierName("CurrentCulture"));
}

protected abstract Task<Document> UseInvariantVersionAsync(Document document, SyntaxGenerator syntaxGenerator, SyntaxNode root, SyntaxNode node);

protected static string GetReplacementMethodName(string currentMethodName) => currentMethodName switch
{
SpecifyCultureForToLowerAndToUpperAnalyzer.ToLowerMethodName => "ToLowerInvariant",
SpecifyCultureForToLowerAndToUpperAnalyzer.ToUpperMethodName => "ToUpperInvariant",
_ => currentMethodName,
};

public sealed override FixAllProvider GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Immutable;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.NetAnalyzers;
using Microsoft.CodeAnalysis.Operations;

namespace Microsoft.NetCore.Analyzers.Runtime
{
using static MicrosoftNetCoreAnalyzersResources;

public abstract class SpecifyCultureForToLowerAndToUpperAnalyzer : AbstractGlobalizationDiagnosticAnalyzer
{
internal const string RuleId = "CA1311";

internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
RuleId,
CreateLocalizableResourceString(nameof(SpecifyCultureForToLowerAndToUpperTitle)),
CreateLocalizableResourceString(nameof(SpecifyCultureForToLowerAndToUpperMessage)),
DiagnosticCategory.Globalization,
RuleLevel.IdeHidden_BulkConfigurable,
description: CreateLocalizableResourceString(nameof(SpecifyCultureForToLowerAndToUpperDescription)),
isPortedFxCopRule: false,
isDataflowRule: false);

internal const string ToLowerMethodName = "ToLower";
internal const string ToUpperMethodName = "ToUpper";
protected abstract Location GetMethodNameLocation(SyntaxNode invocationNode);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

protected override void InitializeWorker(CompilationStartAnalysisContext context)
{
context.RegisterOperationAction(operationContext =>
{
var operation = (IInvocationOperation)operationContext.Operation;
IMethodSymbol methodSymbol = operation.TargetMethod;

if (methodSymbol.ContainingType.SpecialType == SpecialType.System_String &&
!methodSymbol.IsStatic &&
IsToLowerOrToUpper(methodSymbol.Name) &&
//picking the correct overload
methodSymbol.Parameters.Length == 0)
{
operationContext.ReportDiagnostic(Diagnostic.Create(Rule, GetMethodNameLocation(operation.Syntax)));
}
}, OperationKind.Invocation);
}

private static bool IsToLowerOrToUpper(string methodName)
{
return methodName == ToLowerMethodName || methodName == ToUpperMethodName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,21 @@
<target state="translated">Třída {0}, která je odvozená ze System.Web.UI.Page, nenastavuje vlastnost ViewStateUserKey v metodě OnInit nebo Page_Init.</target>
<note />
</trans-unit>
<trans-unit id="SpecifyCultureForToLowerAndToUpperDescription">
<source>Specify culture to help avoid accidental implicit dependency on current culture. Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Specify culture to help avoid accidental implicit dependency on current culture. Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="SpecifyCultureForToLowerAndToUpperMessage">
<source>Specify a culture or use an invariant version to avoid implicit dependency on current culture</source>
<target state="new">Specify a culture or use an invariant version to avoid implicit dependency on current culture</target>
<note />
</trans-unit>
<trans-unit id="SpecifyCultureForToLowerAndToUpperTitle">
<source>Specify a culture or use an invariant version</source>
<target state="new">Specify a culture or use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="SpecifyCultureInfoDescription">
<source>A method or constructor calls a member that has an overload that accepts a System.Globalization.CultureInfo parameter, and the method or constructor does not call the overload that takes the CultureInfo parameter. When a CultureInfo or System.IFormatProvider object is not supplied, the default value that is supplied by the overloaded member might not have the effect that you want in all locales. If the result will be displayed to the user, specify 'CultureInfo.CurrentCulture' as the 'CultureInfo' parameter. Otherwise, if the result will be stored and accessed by software, such as when it is persisted to disk or to a database, specify 'CultureInfo.InvariantCulture'.</source>
<target state="translated">Metoda nebo konstruktor volá člen, který je přetížením přijímajícím parametr System.Globalization.CultureInfo, ale metoda nebo konstruktor nevolá přetížení, které přijímá parametr CultureInfo. Když se nezadá CultureInfo ani objekt System.IFormatProvider, výchozí hodnota, kterou poskytuje přetížený člen, nemusí mít ve všech národních prostředích požadovaný účinek. Pokud se výsledek zobrazí uživateli, zadejte jako parametr CultureInfo hodnotu CultureInfo.CurrentCulture. V opačném případě, pokud software výsledek uloží a přistupuje k němu, třeba při jeho trvalém uložení na disk nebo do databáze, zadejte CultureInfo.InvariantCulture.</target>
Expand All @@ -2342,6 +2357,11 @@
<target state="translated">Zadejte CultureInfo</target>
<note />
</trans-unit>
<trans-unit id="SpecifyCurrentCulture">
<source>Specify current culture</source>
<target state="new">Specify current culture</target>
<note />
</trans-unit>
<trans-unit id="SpecifyIFormatProviderDescription">
<source>A method or constructor calls one or more members that have overloads that accept a System.IFormatProvider parameter, and the method or constructor does not call the overload that takes the IFormatProvider parameter. When a System.Globalization.CultureInfo or IFormatProvider object is not supplied, the default value that is supplied by the overloaded member might not have the effect that you want in all locales. If the result will be based on the input from/output displayed to the user, specify 'CultureInfo.CurrentCulture' as the 'IFormatProvider'. Otherwise, if the result will be stored and accessed by software, such as when it is loaded from disk/database and when it is persisted to disk/database, specify 'CultureInfo.InvariantCulture'.</source>
<target state="translated">Metoda nebo konstruktor volá nejméně jeden člen, který má přetížení přijímající parametr System.IFormatProvider, ale metoda nebo konstruktor nevolá přetížení, které přijímá parametr IFormatProvider. Když se nezadá System.Globalization.CultureInfo ani objekt IFormatProvider, výchozí hodnota, kterou poskytuje přetížený člen, nemusí mít ve všech národních prostředích požadovaný účinek. Pokud se výsledek zakládá na vstupu od uživatele nebo na výstupu, který se mu zobrazí, zadejte jako parametr IFormatProvider hodnotu CultureInfo.CurrentCulture. V opačném případě, pokud software výsledek uloží a přistupuje k němu, třeba při jeho trvalém uložení na disk nebo do databáze nebo při jeho načtení z nich, zadejte CultureInfo.InvariantCulture.</target>
Expand Down Expand Up @@ -2692,6 +2712,11 @@
<target state="translated">Použít indexer</target>
<note />
</trans-unit>
<trans-unit id="UseInvariantVersion">
<source>User an invariant version</source>
<target state="new">User an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseManagedEquivalentsOfWin32ApiDescription">
<source>An operating system invoke method is defined and a method that has the equivalent functionality is located in the .NET Framework class library.</source>
<target state="translated">Metoda invoke operačního systému je definována a metoda, která má ekvivalentní funkci, se nachází v knihovně tříd .NET Framework.</target>
Expand Down
Loading

0 comments on commit 334bb06

Please sign in to comment.