-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d581cc4
commit 5a69115
Showing
20 changed files
with
366 additions
and
1 deletion.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpUseToLowerInvariantOrToUpperInvariant.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...alyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseToLowerInvariantOrToUpperInvariant.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.