-
Notifications
You must be signed in to change notification settings - Fork 480
New constant expected analyzer #5766
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
1f4234a
Add ConstantExpectedAnalyzer
wzchua f39fc60
Fix floating point string culture issue
wzchua 74d9533
Update tests
wzchua d13a77e
Fix typos
wzchua 8b3d43e
Rework test cases
wzchua d4fe364
run pack command
wzchua 5f8922f
Reorganize and combine some rules.
wzchua 7b883e0
Apply suggestions from code review
wzchua f784732
Merge branch 'main' into new-analyzer/constant-expected
wzchua d12e2fb
Apply suggestion from feedback
wzchua 9d4c1a3
Merge branch 'main' into new-analyzer/constant-expected
wzchua db80589
Apply suggestions from code review
wzchua 68e6e28
Apply suggestions from code review
wzchua 52f5e3f
rebuild
wzchua f991607
Use const variable for Min Max literal
wzchua eab469f
Drop nint nuint support from ConstantExpected
wzchua 5e1b4c3
Add early exit if symbol for attribute is not found
wzchua e7ac792
Add check for enums
wzchua 1196110
Merge branch 'main' into new-analyzer/constant-expected
wzchua 65de245
Fix RS1032
wzchua 04ad5b1
Fix IDE0200: Lambda expression can be removed
wzchua 011d9eb
Rework attribute symbol matching
wzchua ec3f1ae
Update diagnostic Ids
wzchua 59fdd58
Update test to use ReferenceAssemblies.Net70
wzchua 865974c
Add performance tests
wzchua c87ee13
Apply suggestions from code review
buyaa-n 5f07e36
Update localized messages
buyaa-n 7c8b93b
Merge branch 'main' into new-analyzer/constant-expected
wzchua 266fba8
Run pack
wzchua 86dbee4
Merge branch 'main' into new-analyzer/constant-expected
buyaa-n 774008b
Apply suggestions from code review
buyaa-n ca89072
Fix generated file
buyaa-n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...nalyzers/CSharp/Microsoft.NetCore.Analyzers/Performance/CSharpConstantExpectedAnalyzer.cs
This file contains hidden or 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,60 @@ | ||
| // Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.NetCore.Analyzers.Performance; | ||
|
|
||
| namespace Microsoft.NetCore.CSharp.Analyzers.Performance | ||
| { | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| internal sealed class CSharpConstantExpectedAnalyzer : ConstantExpectedAnalyzer | ||
| { | ||
| private static readonly CSharpDiagnosticHelper s_diagnosticHelper = new(); | ||
| private readonly IdentifierNameSyntax _constantExpectedIdentifier = (IdentifierNameSyntax)SyntaxFactory.ParseName(ConstantExpected); | ||
| private readonly IdentifierNameSyntax _constantExpectedAttributeIdentifier = (IdentifierNameSyntax)SyntaxFactory.ParseName(ConstantExpectedAttribute); | ||
|
|
||
| protected override DiagnosticHelper Helper => s_diagnosticHelper; | ||
|
|
||
| protected override void RegisterAttributeSyntax(CompilationStartAnalysisContext context) | ||
| { | ||
| context.RegisterSyntaxNodeAction(context => OnAttributeNode(context), SyntaxKind.Attribute); | ||
| } | ||
wzchua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private void OnAttributeNode(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var attributeSyntax = (AttributeSyntax)context.Node; | ||
| if (!attributeSyntax.Name.IsEquivalentTo(_constantExpectedIdentifier) && !attributeSyntax.Name.IsEquivalentTo(_constantExpectedAttributeIdentifier)) | ||
| { | ||
| return; | ||
| } | ||
| var parameter = (ParameterSyntax)attributeSyntax.Parent.Parent; | ||
| var parameterSymbol = context.SemanticModel.GetDeclaredSymbol(parameter); | ||
|
|
||
| OnParameterWithConstantExpectedAttribute(parameterSymbol, context.ReportDiagnostic); | ||
wzchua marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private sealed class CSharpDiagnosticHelper : DiagnosticHelper | ||
| { | ||
| private readonly IdentifierNameSyntax _constantExpectedMinIdentifier = (IdentifierNameSyntax)SyntaxFactory.ParseName("Min"); | ||
wzchua marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private readonly IdentifierNameSyntax _constantExpectedMaxIdentifier = (IdentifierNameSyntax)SyntaxFactory.ParseName("Max"); | ||
wzchua marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public override Location? GetMaxLocation(SyntaxNode attributeNode) => GetArgumentLocation(attributeNode, _constantExpectedMaxIdentifier); | ||
|
|
||
| public override Location? GetMinLocation(SyntaxNode attributeNode) => GetArgumentLocation(attributeNode, _constantExpectedMinIdentifier); | ||
|
|
||
| private static Location? GetArgumentLocation(SyntaxNode attributeNode, IdentifierNameSyntax targetNameSyntax) | ||
| { | ||
| var attributeSyntax = (AttributeSyntax)attributeNode; | ||
| if (attributeSyntax.ArgumentList is null) | ||
| { | ||
| return null; | ||
| } | ||
| var targetArg = attributeSyntax.ArgumentList.Arguments.FirstOrDefault(arg => arg.NameEquals.Name.IsEquivalentTo(targetNameSyntax, true)); | ||
| return targetArg?.GetLocation(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ; | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.