-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid reporting SA1141 (Use tuple syntax) in expression trees
Fixes #3305
- Loading branch information
Showing
5 changed files
with
211 additions
and
36 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
92 changes: 92 additions & 0 deletions
92
StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SyntaxNodeExtensions.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,92 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
#nullable enable | ||
|
||
namespace StyleCop.Analyzers.Helpers | ||
{ | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
internal static class SyntaxNodeExtensions | ||
{ | ||
public static bool IsInExpressionTree( | ||
this SyntaxNode? node, | ||
SemanticModel semanticModel, | ||
INamedTypeSymbol? expressionType, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (expressionType != null) | ||
{ | ||
for (var current = node; current != null; current = current.Parent) | ||
{ | ||
if (current.IsAnyLambda()) | ||
{ | ||
var typeInfo = semanticModel.GetTypeInfo(current, cancellationToken); | ||
if (expressionType.Equals(typeInfo.ConvertedType?.OriginalDefinition)) | ||
{ | ||
return true; | ||
} | ||
} | ||
else if (current is SelectOrGroupClauseSyntax or OrderingSyntax) | ||
{ | ||
var info = semanticModel.GetSymbolInfo(current, cancellationToken); | ||
if (AnyTakesExpressionTree(info, expressionType)) | ||
{ | ||
return true; | ||
} | ||
} | ||
else if (current is QueryClauseSyntax queryClause) | ||
{ | ||
var info = semanticModel.GetQueryClauseInfo(queryClause, cancellationToken); | ||
if (AnyTakesExpressionTree(info.CastInfo, expressionType) | ||
|| AnyTakesExpressionTree(info.OperationInfo, expressionType)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
|
||
static bool AnyTakesExpressionTree(SymbolInfo info, INamedTypeSymbol expressionType) | ||
{ | ||
if (TakesExpressionTree(info.Symbol, expressionType)) | ||
{ | ||
return true; | ||
} | ||
|
||
foreach (var symbol in info.CandidateSymbols) | ||
{ | ||
if (TakesExpressionTree(symbol, expressionType)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
static bool TakesExpressionTree(ISymbol symbol, INamedTypeSymbol expressionType) | ||
{ | ||
if (symbol is IMethodSymbol method | ||
&& method.Parameters.Length > 0 | ||
&& expressionType.Equals(method.Parameters[0].Type?.OriginalDefinition)) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
public static bool IsAnyLambda(this SyntaxNode? node) | ||
{ | ||
return node.IsKind(SyntaxKind.ParenthesizedLambdaExpression) | ||
|| node.IsKind(SyntaxKind.SimpleLambdaExpression); | ||
} | ||
} | ||
} |
Oops, something went wrong.