-
-
Notifications
You must be signed in to change notification settings - Fork 259
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
More cases to optimize linq #1157
base: main
Are you sure you want to change the base?
Changes from 9 commits
8b46a41
04180e7
60803c5
e0f6014
467d1f1
3653b31
689b19d
7b2d465
29cd3ee
7b440c7
4ce6957
1d45ae3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,6 +185,20 @@ private static void SimplifyLinqMethodChain( | |
return; | ||
} | ||
|
||
break; | ||
} | ||
case "OrderBy": | ||
{ | ||
if (!SymbolUtility.IsLinqOrderBy(methodSymbol2, allowImmutableArrayExtension: true)) | ||
return; | ||
|
||
break; | ||
} | ||
case "OrderByDescending": | ||
{ | ||
if (!SymbolUtility.IsLinqOrderByDescending(methodSymbol2, allowImmutableArrayExtension: true)) | ||
return; | ||
|
||
break; | ||
} | ||
default: | ||
|
@@ -199,6 +213,48 @@ private static void SimplifyLinqMethodChain( | |
Report(context, invocation, span, checkDirectives: true, properties: properties); | ||
} | ||
|
||
private static bool IsNet6OrGreater(Compilation compilation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not maintainable as .NET version will grow. I suggest to check if the enumerableSymbol.FindMember<IMethodSymbol>("MinBy") is not null There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree thats much better :) |
||
{ | ||
var targetFrameworkAttribute = compilation.GetTypeByMetadataName("System.Runtime.Versioning.TargetFrameworkAttribute"); | ||
|
||
if (targetFrameworkAttribute == null) | ||
return false; | ||
|
||
foreach (var attr in compilation.Assembly.GetAttributes()) | ||
{ | ||
if (!SymbolEqualityComparer.Default.Equals(targetFrameworkAttribute, attr.AttributeClass)) | ||
continue; | ||
|
||
if (attr.ConstructorArguments.FirstOrDefault().Value is not string targetFramework) | ||
continue; | ||
|
||
if (targetFramework is ".NETCoreApp,Version=v6.0" or ".NETCoreApp,Version=v7.0") | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// items.OrderBy(selector).FirstOrDefault() >>> items.MaxBy(selector) | ||
// items.OrderByDescending(selector).FirstOrDefault() >>> items.MaxBy(selector) | ||
public static void AnalyzerOrderByAndFirstOrDefault(SyntaxNodeAnalysisContext context, in SimpleMemberInvocationExpressionInfo invocationInfo) | ||
{ | ||
if (!IsNet6OrGreater(context.Compilation)) | ||
return; | ||
|
||
SimplifyLinqMethodChain( | ||
context, | ||
invocationInfo, | ||
"OrderBy", | ||
Properties.SimplifyLinqMethodChain); | ||
|
||
SimplifyLinqMethodChain( | ||
context, | ||
invocationInfo, | ||
"OrderByDescending", | ||
Properties.SimplifyLinqMethodChain); | ||
} | ||
|
||
public static void AnalyzeFirstOrDefault(SyntaxNodeAnalysisContext context, in SimpleMemberInvocationExpressionInfo invocationInfo) | ||
{ | ||
InvocationExpressionSyntax invocation = invocationInfo.InvocationExpression; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to
First
case.