Skip to content
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 @@ -12,7 +12,12 @@
namespace Microsoft.CodeAnalysis.CSharp.SimplifyLinqExpression;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpSimplifyLinqExpressionDiagnosticAnalyzer : AbstractSimplifyLinqExpressionDiagnosticAnalyzer<InvocationExpressionSyntax, MemberAccessExpressionSyntax>
internal sealed class CSharpSimplifyLinqExpressionDiagnosticAnalyzer
: AbstractSimplifyLinqExpressionDiagnosticAnalyzer<
ExpressionSyntax,
SimpleNameSyntax,
InvocationExpressionSyntax,
MemberAccessExpressionSyntax>
{
protected override ISyntaxFacts SyntaxFacts => CSharpSyntaxFacts.Instance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,45 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.SimplifyLinqExpression;

internal abstract class AbstractSimplifyLinqExpressionDiagnosticAnalyzer<TInvocationExpressionSyntax, TMemberAccessExpressionSyntax>()
: AbstractBuiltInCodeStyleDiagnosticAnalyzer(
internal abstract class AbstractSimplifyLinqExpressionDiagnosticAnalyzer<
TExpressionSyntax,
TSimpleNameSyntax,
TInvocationExpressionSyntax,
TMemberAccessExpressionSyntax>()
: AbstractBuiltInUnnecessaryCodeStyleDiagnosticAnalyzer(
IDEDiagnosticIds.SimplifyLinqExpressionDiagnosticId,
EnforceOnBuildValues.SimplifyLinqExpression,
option: null,
title: new LocalizableResourceString(nameof(AnalyzersResources.Simplify_LINQ_expression), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
where TInvocationExpressionSyntax : SyntaxNode
where TMemberAccessExpressionSyntax : SyntaxNode
where TExpressionSyntax : SyntaxNode
where TSimpleNameSyntax : TExpressionSyntax
where TInvocationExpressionSyntax : TExpressionSyntax
where TMemberAccessExpressionSyntax : TExpressionSyntax
{
private static readonly ImmutableHashSet<string> s_nonEnumerableReturningLinqPredicateMethodNames =
[
nameof(Enumerable.First),
nameof(Enumerable.Last),
nameof(Enumerable.Single),
nameof(Enumerable.Any),
nameof(Enumerable.Count),
nameof(Enumerable.SingleOrDefault),
nameof(Enumerable.FirstOrDefault),
nameof(Enumerable.LastOrDefault),
];
private static readonly ImmutableHashSet<string> s_nonEnumerableReturningLinqSelectorMethodNames =
[
nameof(Enumerable.Average),
nameof(Enumerable.Sum),
nameof(Enumerable.Min),
nameof(Enumerable.Max),
];
private static readonly ImmutableHashSet<string> s_nonEnumerableReturningLinqPredicateMethodNames = [
nameof(Enumerable.First),
nameof(Enumerable.Last),
nameof(Enumerable.Single),
nameof(Enumerable.Any),
nameof(Enumerable.Count),
nameof(Enumerable.SingleOrDefault),
nameof(Enumerable.FirstOrDefault),
nameof(Enumerable.LastOrDefault)];

private static readonly ImmutableHashSet<string> s_nonEnumerableReturningLinqSelectorMethodNames = [
nameof(Enumerable.Average),
nameof(Enumerable.Sum),
nameof(Enumerable.Min),
nameof(Enumerable.Max)];

protected abstract ISyntaxFacts SyntaxFacts { get; }

Expand Down Expand Up @@ -140,12 +145,16 @@ void AnalyzeInvocationOperation(OperationAnalysisContext context)
return;
}

if (TryGetSymbolOfMemberAccess(invocation) is not ITypeSymbol targetTypeSymbol ||
TryGetMethodName(nextInvocation) is not string name)
if (TryGetSymbolOfMemberAccess(invocation) is not ITypeSymbol targetTypeSymbol)
return;

if (nextInvocation.Syntax is not TInvocationExpressionSyntax nextInvocationNode ||
TryGetMethodName(nextInvocationNode) is not TSimpleNameSyntax memberName)
{
return;
}

var name = SyntaxFacts.GetIdentifierOfSimpleName(memberName).ValueText;
if (isWhereMethod && !s_nonEnumerableReturningLinqPredicateMethodNames.Contains(name))
return;

Expand All @@ -170,7 +179,19 @@ void AnalyzeInvocationOperation(OperationAnalysisContext context)
}
}

context.ReportDiagnostic(Diagnostic.Create(Descriptor, nextInvocation.Syntax.GetLocation()));
// For `Where(x => x is not null).First()` fade out the `First()` portion as we're effectively trimming off
// the last unnecessary chunk.
using var additionalUnnecessaryLocations = TemporaryArray<Location>.Empty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it easy to add a test for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving to a different test harness, yes :'(

additionalUnnecessaryLocations.Add(nextInvocationNode.SyntaxTree.GetLocation(
TextSpan.FromBounds(memberName.SpanStart, nextInvocationNode.Span.End)));

context.ReportDiagnostic(DiagnosticHelper.CreateWithLocationTags(
Descriptor,
nextInvocation.Syntax.GetLocation(),
NotificationOption2.ForSeverity(Descriptor.DefaultSeverity),
context.Options,
additionalLocations: [],
additionalUnnecessaryLocations.ToImmutableAndClear()));
}

bool IsWhereLinqMethod(IInvocationOperation invocation)
Expand All @@ -194,17 +215,13 @@ bool IsInvocationNonEnumerableReturningLinqMethod(IInvocationOperation invocatio
return invocation.SemanticModel?.GetTypeInfo(expression).Type;
}

string? TryGetMethodName(IInvocationOperation invocation)
TSimpleNameSyntax? TryGetMethodName(TInvocationExpressionSyntax invocationNode)
{
if (invocation.Syntax is not TInvocationExpressionSyntax invocationNode ||
SyntaxFacts.GetExpressionOfInvocationExpression(invocationNode) is not TMemberAccessExpressionSyntax memberAccess)
{
if (SyntaxFacts.GetExpressionOfInvocationExpression(invocationNode) is not TMemberAccessExpressionSyntax memberAccess)
return null;
}

var memberName = SyntaxFacts.GetNameOfMemberAccessExpression(memberAccess);
var identifier = SyntaxFacts.GetIdentifierOfSimpleName(memberName);
return identifier.ValueText;
return memberName as TSimpleNameSyntax;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.SimplifyLinqExpression
<DiagnosticAnalyzer(LanguageNames.VisualBasic)>
Friend Class VisualBasicSimplifyLinqExpressionDiagnosticAnalyzer
Inherits AbstractSimplifyLinqExpressionDiagnosticAnalyzer(Of InvocationExpressionSyntax, MemberAccessExpressionSyntax)
Inherits AbstractSimplifyLinqExpressionDiagnosticAnalyzer(Of
ExpressionSyntax,
SimpleNameSyntax,
InvocationExpressionSyntax,
MemberAccessExpressionSyntax)

Protected Overrides ReadOnly Property SyntaxFacts As ISyntaxFacts = VisualBasicSyntaxFacts.Instance

Expand Down
Loading