-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add 'Make anonymous function static' feature #73012
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
Changes from 7 commits
246cbeb
2d31ad5
da06c48
d6c6873
ad6845f
ebc7c01
622c8b8
7757059
73171b5
feb8c29
8668c6d
41bfda6
7c64a35
412b744
1bf9c2d
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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.CodeAnalysis.CodeStyle; | ||
| using Microsoft.CodeAnalysis.CSharp.CodeStyle; | ||
| using Microsoft.CodeAnalysis.CSharp.Extensions; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.MakeAnonymousFunctionStatic; | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| internal sealed class MakeAnonymousFunctionStaticDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer | ||
| { | ||
| public MakeAnonymousFunctionStaticDiagnosticAnalyzer() | ||
| : base(IDEDiagnosticIds.MakeAnonymousFunctionStaticDiagnosticId, | ||
| EnforceOnBuildValues.MakeAnonymousFunctionStatic, | ||
| CSharpCodeStyleOptions.PreferStaticAnonymousFunction, | ||
| new LocalizableResourceString(nameof(CSharpAnalyzersResources.Make_anonymous_function_static), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)), | ||
| new LocalizableResourceString(nameof(CSharpAnalyzersResources.Anonymous_function_can_be_made_static), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources))) | ||
| { | ||
| } | ||
|
|
||
| public override DiagnosticAnalyzerCategory GetAnalyzerCategory() | ||
| => DiagnosticAnalyzerCategory.SemanticSpanAnalysis; | ||
|
|
||
| protected override void InitializeWorker(AnalysisContext context) | ||
| { | ||
| context.RegisterCompilationStartAction(context => | ||
| { | ||
| if (context.Compilation.LanguageVersion() < LanguageVersion.CSharp9) | ||
| return; | ||
|
|
||
| context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.SimpleLambdaExpression, SyntaxKind.ParenthesizedLambdaExpression, SyntaxKind.AnonymousMethodExpression); | ||
| }); | ||
| } | ||
|
|
||
| private void AnalyzeSyntax(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var option = context.GetCSharpAnalyzerOptions().PreferStaticAnonymousFunction; | ||
| if (!option.Value || ShouldSkipAnalysis(context, option.Notification)) | ||
| return; | ||
|
|
||
| var anonymousFunction = (AnonymousFunctionExpressionSyntax)context.Node; | ||
| if (anonymousFunction.Modifiers.Any(SyntaxKind.StaticKeyword)) | ||
| return; | ||
|
|
||
| if (CanAnonymousFunctionByMadeStatic(anonymousFunction, context.SemanticModel)) | ||
| { | ||
| context.ReportDiagnostic( | ||
| Diagnostic.Create( | ||
| Descriptor, | ||
| anonymousFunction.GetLocation())); | ||
| } | ||
| } | ||
|
|
||
| private static bool CanAnonymousFunctionByMadeStatic(AnonymousFunctionExpressionSyntax anonymousFunction, SemanticModel semanticModel) | ||
| { | ||
| var dataFlow = semanticModel.AnalyzeDataFlow(anonymousFunction); | ||
| if (dataFlow is not { Succeeded: true }) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return dataFlow.Captured.IsEmpty; | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.