-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #751 from polyadic/suppress-use-with-argument-name…
…s-in-expr-trees Disable UseWithArgumentNamesAnalyzer in Expression Trees
- Loading branch information
Showing
9 changed files
with
216 additions
and
127 deletions.
There are no files selected for viewing
33 changes: 0 additions & 33 deletions
33
Funcky.Analyzers/Funcky.Analyzers.Test/TestCode/UseWithArgumentNames.expected
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
Funcky.Analyzers/Funcky.Analyzers.Test/TestCode/UseWithArgumentNames.input
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
Funcky.Analyzers/Funcky.Analyzers.Test/TestCode/ValidUseWithArgumentNames.input
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
Funcky.Analyzers/Funcky.Analyzers.Test/TestCode/ValidUseWithArgumentNamesNoAttribute.input
This file was deleted.
Oops, something went wrong.
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
64 changes: 64 additions & 0 deletions
64
Funcky.Analyzers/Funcky.Analyzers/SyntaxNodeExtensions.ExpressionTree.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,64 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Funcky.Analyzers; | ||
|
||
internal static partial class SyntaxNodeExtensions | ||
{ | ||
// Adapted from Roslyn's source code as this API is not public: | ||
// https://github.com/dotnet/roslyn/blob/232f7afa4966411958759c880de3a1765bdb28a0/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/SyntaxNodeExtensions.cs#L925 | ||
public static bool IsInExpressionTree( | ||
[NotNullWhen(returnValue: true)] this SyntaxNode? node, | ||
SemanticModel semanticModel, | ||
[NotNullWhen(returnValue: true)] INamedTypeSymbol? expressionType, | ||
CancellationToken cancellationToken) | ||
=> expressionType is not null | ||
&& node is not null | ||
&& node | ||
.AncestorsAndSelf() | ||
.Any(current => IsExpressionTree(new(current, expressionType, semanticModel, cancellationToken))); | ||
|
||
private static bool IsExpressionTree(IsExpressionTreeContext context) | ||
=> context.Syntax switch | ||
{ | ||
var node when node.IsAnyLambda() => LambdaIsExpressionTree(context), | ||
SelectOrGroupClauseSyntax or OrderingSyntax => QueryExpressionIsExpressionTree(context), | ||
QueryClauseSyntax queryClause => QueryClauseIsExpressionTree(context, queryClause), | ||
_ => false, | ||
}; | ||
|
||
private static bool LambdaIsExpressionTree(IsExpressionTreeContext context) | ||
{ | ||
var typeInfo = context.SemanticModel.GetTypeInfo(context.Syntax, context.CancellationToken); | ||
return SymbolEqualityComparer.Default.Equals(context.ExpressionType, typeInfo.ConvertedType?.OriginalDefinition); | ||
} | ||
|
||
private static bool QueryExpressionIsExpressionTree(IsExpressionTreeContext context) | ||
{ | ||
var info = context.SemanticModel.GetSymbolInfo(context.Syntax, context.CancellationToken); | ||
return TakesExpressionTree(info, context.ExpressionType); | ||
} | ||
|
||
private static bool QueryClauseIsExpressionTree(IsExpressionTreeContext context, QueryClauseSyntax queryClause) | ||
{ | ||
var info = context.SemanticModel.GetQueryClauseInfo(queryClause, context.CancellationToken); | ||
return TakesExpressionTree(info.CastInfo, context.ExpressionType) | ||
|| TakesExpressionTree(info.OperationInfo, context.ExpressionType); | ||
} | ||
|
||
private static bool TakesExpressionTree(SymbolInfo info, INamedTypeSymbol expressionType) | ||
=> GetAllSymbols(info).Any(symbol => TakesExpressionTreeAsFirstArgument(symbol, expressionType)); | ||
|
||
private static bool TakesExpressionTreeAsFirstArgument(ISymbol symbol, INamedTypeSymbol expressionType) | ||
=> symbol is IMethodSymbol method | ||
&& method.Parameters.Length > 0 | ||
&& SymbolEqualityComparer.Default.Equals(expressionType, method.Parameters[0].Type?.OriginalDefinition); | ||
|
||
private sealed record IsExpressionTreeContext( | ||
SyntaxNode Syntax, | ||
INamedTypeSymbol ExpressionType, | ||
SemanticModel SemanticModel, | ||
CancellationToken CancellationToken); | ||
} |
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,19 @@ | ||
using System.Collections.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
namespace Funcky.Analyzers; | ||
|
||
internal static partial class SyntaxNodeExtensions | ||
{ | ||
internal static ImmutableArray<ISymbol> GetAllSymbols(SymbolInfo info) | ||
=> info.Symbol == null | ||
? info.CandidateSymbols | ||
: ImmutableArray.Create(info.Symbol); | ||
|
||
// Copied from Roslyn's source code as this API is not public: | ||
// https://github.com/dotnet/roslyn/blob/232f7afa4966411958759c880de3a1765bdb28a0/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/SyntaxNodeExtensions.cs#L925 | ||
internal static bool IsAnyLambda([NotNullWhen(returnValue: true)] this SyntaxNode? node) | ||
=> node?.Kind() is SyntaxKind.ParenthesizedLambdaExpression or SyntaxKind.SimpleLambdaExpression; | ||
} |
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