-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule IDE0159 - Use 'Order' instead of 'OrderBy'
- Loading branch information
Showing
8 changed files
with
226 additions
and
5 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
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,13 @@ | ||
# MA0159 - Use 'Order' instead of 'OrderBy' | ||
|
||
Use `Order` instead of `OrderBy(x => x)` to order a collection. | ||
|
||
````c# | ||
IEnumerable<int> numbers = [1, 2]; | ||
var orderedNumbers = numbers.OrderBy(n => n); // non-compliant | ||
```` | ||
|
||
````c# | ||
IEnumerable<int> numbers = [1, 2]; | ||
var orderedNumbers = numbers.Order(); // compliant | ||
```` |
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
139 changes: 139 additions & 0 deletions
139
tests/Meziantou.Analyzer.Test/Rules/OptimizeLinqUsageAnalyzerOrderTests.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,139 @@ | ||
using System.Threading.Tasks; | ||
using Meziantou.Analyzer.Rules; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Meziantou.Analyzer.Test.Rules; | ||
|
||
public sealed class OptimizeLinqUsageAnalyzerOrderTests | ||
{ | ||
private static ProjectBuilder CreateProjectBuilder() | ||
{ | ||
return new ProjectBuilder() | ||
.WithTargetFramework(TargetFramework.Net8_0) | ||
.WithAnalyzer<OptimizeLinqUsageAnalyzer>(id: RuleIdentifiers.OptimizeEnumerable_UseOrder) | ||
.WithCodeFixProvider<OptimizeLinqUsageFixer>(); | ||
} | ||
|
||
[Fact] | ||
public async Task IEnumerable_Order_net5() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithTargetFramework(TargetFramework.Net5_0) | ||
.WithSourceCode(""" | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
class Test | ||
{ | ||
public Test() | ||
{ | ||
IEnumerable<string> query = null; | ||
query.OrderBy(x => x); | ||
} | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task IEnumerable_Order_LambdaNotValid() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithTargetFramework(TargetFramework.Net5_0) | ||
.WithSourceCode(""" | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
class Test | ||
{ | ||
public Test() | ||
{ | ||
IEnumerable<string> query = null; | ||
query.OrderBy(x => true); | ||
} | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task IEnumerable_Order_LambdaReferenceAnotherParameter() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithTargetFramework(TargetFramework.Net5_0) | ||
.WithSourceCode(""" | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
class Test | ||
{ | ||
public Test(int a) | ||
{ | ||
IEnumerable<string> query = null; | ||
query.OrderBy(x => a); | ||
} | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task IEnumerable_Order() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
class Test | ||
{ | ||
public Test() | ||
{ | ||
IEnumerable<string> query = null; | ||
query.[|OrderBy|](x => x); | ||
} | ||
} | ||
""") | ||
.ShouldFixCodeWith(""" | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
class Test | ||
{ | ||
public Test() | ||
{ | ||
IEnumerable<string> query = null; | ||
query.Order(); | ||
} | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task IEnumerable_OrderDescending() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
class Test | ||
{ | ||
public Test() | ||
{ | ||
IEnumerable<string> query = null; | ||
query.[|OrderByDescending|](x => x); | ||
} | ||
} | ||
""") | ||
.ShouldFixCodeWith(""" | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
class Test | ||
{ | ||
public Test() | ||
{ | ||
IEnumerable<string> query = null; | ||
query.OrderDescending(); | ||
} | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
} |