Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added property names to diagnostic title.
added diagnostic property key constant. split analyzers int language-specific analyzers.
- Loading branch information
1 parent
02b30d5
commit 623ca8c
Showing
23 changed files
with
316 additions
and
228 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
...etCore.Analyzers/CSharp/Performance/CSharpUsePropertyInsteadOfCountMethodWhenAvailable.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,54 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
using Microsoft.NetCore.Analyzers.Performance; | ||
|
||
namespace Microsoft.NetCore.CSharp.Analyzers.Performance | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class CSharpUsePropertyInsteadOfCountMethodWhenAvailableAnalyzer | ||
: UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer | ||
{ | ||
/// <summary> | ||
/// Creates the operation actions handler. | ||
/// </summary> | ||
/// <param name="context">The context.</param> | ||
/// <returns>The operation actions handler.</returns> | ||
protected override OperationActionsHandler CreateOperationActionsHandler(OperationActionsContext context) | ||
=> new CSharpOperationActionsHandler(context); | ||
|
||
/// <summary> | ||
/// Handler for operaction actions for C#. This class cannot be inherited. | ||
/// Implements the <see cref="Microsoft.NetCore.Analyzers.Performance.UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer.OperationActionsHandler" /> | ||
/// </summary> | ||
/// <seealso cref="Microsoft.NetCore.Analyzers.Performance.UsePropertyInsteadOfCountMethodWhenAvailableAnalyzer.OperationActionsHandler" /> | ||
private sealed class CSharpOperationActionsHandler : OperationActionsHandler | ||
{ | ||
internal CSharpOperationActionsHandler(OperationActionsContext context) | ||
: base(context) | ||
{ | ||
} | ||
|
||
protected override ITypeSymbol GetEnumerableCountInvocationTargetType(IInvocationOperation invocationOperation) | ||
{ | ||
var method = invocationOperation.TargetMethod; | ||
|
||
if (invocationOperation.Arguments.Length == 1 && | ||
method.Name.Equals(nameof(Enumerable.Count), StringComparison.Ordinal) && | ||
this.Context.IsEnumerableType(method.ContainingSymbol) && | ||
((INamedTypeSymbol)(method.Parameters[0].Type)).TypeArguments[0] is ITypeSymbol methodSourceItemType) | ||
{ | ||
return invocationOperation.Arguments[0].Value is IConversionOperation convertionOperation | ||
? convertionOperation.Operand.Type | ||
: invocationOperation.Arguments[0].Value.Type; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
} |
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.