Skip to content

Commit

Permalink
Updated implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TKharaishvili committed Oct 20, 2021
1 parent d581cc4 commit 5a69115
Show file tree
Hide file tree
Showing 20 changed files with 366 additions and 1 deletion.
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 CSharpUseToLowerInvariantOrToUpperInvariantAnalyzer : UseToLowerInvariantOrToUpperInvariantAnalyzer
{
protected override Location GetMethodNameLocation(SyntaxNode invocationNode)
{
Debug.Assert(invocationNode.IsKind(SyntaxKind.InvocationExpression));

var invocation = (InvocationExpressionSyntax)invocationNode;
if (invocation.Expression.IsKind(SyntaxKind.SimpleMemberAccessExpression))
{
return ((MemberAccessExpressionSyntax)invocation.Expression).Name.GetLocation();
}
else if (invocation.Expression.IsKind(SyntaxKind.ConditionalAccessExpression))
{
return ((ConditionalAccessExpressionSyntax)invocation.Expression).WhenNotNull.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 | UseToLowerInvariantOrToUpperInvariant, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1311)
CA1849 | Performance | Disabled | UseAsyncMethodInAsyncContext, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1849)
CA5404 | Security | Disabled | DoNotDisableTokenValidationChecks, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca5404)
CA5405 | Security | Disabled | DoNotAlwaysSkipTokenValidationInDelegates, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca5405)
Original file line number Diff line number Diff line change
Expand Up @@ -1822,4 +1822,10 @@
<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="UseToLowerInvariantOrToUpperInvariantTitle" xml:space="preserve">
<value>Use an invariant version</value>
</data>
<data name="UseToLowerInvariantOrToUpperInvariantDescription" xml:space="preserve">
<value>Using an invariant version yields consistent results regardless of the culture of an application.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
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 UseToLowerInvariantOrToUpperInvariantAnalyzer : AbstractGlobalizationDiagnosticAnalyzer
{
internal const string RuleId = "CA1311";

private static readonly LocalizableString s_localizableMessageAndTitle = CreateLocalizableResourceString(nameof(UseToLowerInvariantOrToUpperInvariantTitle));

internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
RuleId,
s_localizableMessageAndTitle,
s_localizableMessageAndTitle,
DiagnosticCategory.Globalization,
RuleLevel.IdeHidden_BulkConfigurable,
description: CreateLocalizableResourceString(nameof(UseToLowerInvariantOrToUpperInvariantDescription)),
isPortedFxCopRule: true,
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 string.Equals(methodName, ToLowerMethodName, StringComparison.Ordinal) ||
string.Equals(methodName, ToUpperMethodName, StringComparison.Ordinal);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Použít znakový literál pro vyhledávání s jedním znakem</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">Analyzátor kompatibility platformy vyžaduje platný název a verzi platformy.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Zeichenliteral für die Suche nach einem einzelnen Zeichen verwenden</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">Das Analysetool für Plattformkompatibilität erfordert einen gültigen Plattformnamen und eine gültige Version.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Usar literal de carácter para una búsqueda de caracteres individuales</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">El analizador de compatibilidad de plataforma requiere un nombre de plataforma y una versión válidos.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Utiliser le littéral char pour une recherche à caractère unique</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">Platform Analyzer requiert un nom de plateforme et une version valides.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Usare il valore letterale char per la ricerca di un singolo carattere</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">Con l'analizzatore della compatibilità della piattaforma sono richiesti un nome e una versione di piattaforma validi.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">1 つの文字参照に単一文字検索を使用する</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">プラットフォーム互換性アナライザーには、有効なプラットフォーム名とバージョンが必要です。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">단일 문자 조회에 char 리터럴 사용</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">플랫폼 호환성 분석기에는 유효한 플랫폼 이름과 버전이 필요합니다.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Użyj literału char do wyszukiwania pojedynczego znaku</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">Analizator zgodności platformy wymaga prawidłowej nazwy i wersji platformy.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Usar o literal char para uma pesquisa de caractere único</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">O analisador de compatibilidade de plataforma requer um nome de plataforma e uma versão válidos.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Использовать знаковый литерал для поиска одного знака</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">Анализатору совместимости платформы требуется допустимое имя и версия платформы.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">Tek bir karakter araması için sabit değerli karakter kullanın</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">Platform uyumluluğu çözümleyicisi geçerli bir platform adı ve sürümü gerektiriyor.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">将字符型文本用于单个字符查找</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">平台兼容性分析器需要有效的平台名称和版本。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,16 @@
<target state="translated">請為單一字元查閱使用字元常值</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantDescription">
<source>Using an invariant version yields consistent results regardless of the culture of an application.</source>
<target state="new">Using an invariant version yields consistent results regardless of the culture of an application.</target>
<note />
</trans-unit>
<trans-unit id="UseToLowerInvariantOrToUpperInvariantTitle">
<source>Use an invariant version</source>
<target state="new">Use an invariant version</target>
<note />
</trans-unit>
<trans-unit id="UseValidPlatformStringDescription">
<source>Platform compatibility analyzer requires a valid platform name and version.</source>
<target state="translated">平台相容性分析器需要有效的平台名稱和版本。</target>
Expand Down
Loading

0 comments on commit 5a69115

Please sign in to comment.