Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify LINQ #1384

Merged
merged 2 commits into from
Feb 6, 2024
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
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add analyzer "Use raw string literal" [RCS1266](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1266) ([PR](https://github.com/dotnet/roslynator/pull/1375))
- Add analyzer "Convert 'string.Concat' to interpolated string" [RCS1267](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1267) ([PR](https://github.com/dotnet/roslynator/pull/1379))
- Simplify LINQ query [RCS1077](https://josefpihrt.github.io/docs/roslynator/analyzers/1077) ([PR](https://github.com/dotnet/roslynator/pull/1384))
- `items.Select(selector).Average()` => `items.Average(selector)`
- `items.Select(selector).Sum()` => `items.Sum(selector)`

## [4.10.0] - 2024-01-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ private static void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext contex

break;
}
case "Average":
case "Max":
case "Min":
case "Sum":
{
if (DiagnosticRules.OptimizeLinqMethodCall.IsEffective(context))
OptimizeLinqMethodCallAnalysis.AnalyzeSelectAndMinOrMax(context, invocationInfo);
OptimizeLinqMethodCallAnalysis.AnalyzeSelectAndAverageOrMinOrMaxOrSum(context, invocationInfo);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public static void AnalyzeWhere(SyntaxNodeAnalysisContext context, in SimpleMemb
Properties.SimplifyLinqMethodChain);
}

// items.Select(selector).Min/Max() >>> items.Min/Max(selector)
public static void AnalyzeSelectAndMinOrMax(
// items.Select(selector).Average/Min/Max/Sum() >>> items.Average/Min/Max/Sum(selector)
public static void AnalyzeSelectAndAverageOrMinOrMaxOrSum(
SyntaxNodeAnalysisContext context,
in SimpleMemberInvocationExpressionInfo invocationInfo)
{
Expand Down
52 changes: 52 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,58 @@ void M()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)]
public async Task Test_SelectAndAverage()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Linq;

class C
{
void M()
{
double max = Enumerable.Empty<int>().[|Select(f => f).Average()|];
}
}
", @"
using System.Linq;

class C
{
void M()
{
double max = Enumerable.Empty<int>().Average(f => f);
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)]
public async Task Test_SelectAndSum()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Linq;

class C
{
void M()
{
int max = Enumerable.Empty<int>().[|Select(f => f).Sum()|];
}
}
", @"
using System.Linq;

class C
{
void M()
{
int max = Enumerable.Empty<int>().Sum(f => f);
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)]
public async Task Test_SelectAndMin_ImmutableArray()
{
Expand Down
Loading