-
Notifications
You must be signed in to change notification settings - Fork 231
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
S1186: also inspect empty set and init and empty local functions #8584
Changes from all commits
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 |
---|---|---|
|
@@ -68,41 +68,32 @@ void ReportIfAny(List<Location> nullOrDefaultLiterals) | |
} | ||
} | ||
|
||
private static BlockSyntax GetBody(SyntaxNode node) => | ||
node is PropertyDeclarationSyntax property ? GetAccessor(property)?.Body : node.GetBody(); | ||
|
||
private static bool IsReturningCollection(SonarSyntaxNodeReportingContext context) => | ||
GetType(context) is { } type | ||
&& !type.Is(KnownType.System_String) | ||
&& !type.DerivesFrom(KnownType.System_Xml_XmlNode) | ||
&& type.DerivesOrImplementsAny(CollectionTypes) | ||
&& type.NullableAnnotation() != NullableAnnotation.Annotated; | ||
|
||
private static ITypeSymbol GetType(SonarSyntaxNodeReportingContext context) => | ||
context.SemanticModel.GetDeclaredSymbol(context.Node) switch | ||
{ | ||
IPropertySymbol property => property.Type, | ||
IMethodSymbol method => method.ReturnType, | ||
_ => null, | ||
}; | ||
private static ITypeSymbol GetType(SonarSyntaxNodeReportingContext context) | ||
{ | ||
var symbol = context.SemanticModel.GetDeclaredSymbol(context.Node); | ||
return symbol is IPropertySymbol property ? property.Type : ((IMethodSymbol)symbol).ReturnType; | ||
} | ||
|
||
private static ArrowExpressionClauseSyntax GetExpressionBody(SyntaxNode node) => | ||
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. Wouldn't be better to move this to SyntaxNodeExtensions? 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. Similar with above. |
||
node switch | ||
{ | ||
BaseMethodDeclarationSyntax method => method.ExpressionBody(), | ||
PropertyDeclarationSyntax property => property.ExpressionBody ?? GetAccessor(property)?.ExpressionBody(), | ||
var _ when LocalFunctionStatementSyntaxWrapper.IsInstance(node) => ((LocalFunctionStatementSyntaxWrapper)node).ExpressionBody, | ||
_ => null, | ||
}; | ||
|
||
private static BlockSyntax GetBody(SyntaxNode node) => | ||
node switch | ||
{ | ||
BaseMethodDeclarationSyntax method => method.Body, | ||
PropertyDeclarationSyntax property => GetAccessor(property)?.Body, | ||
var _ when LocalFunctionStatementSyntaxWrapper.IsInstance(node) => ((LocalFunctionStatementSyntaxWrapper)node).Body, | ||
_ => null, | ||
BaseMethodDeclarationSyntax method => method.ExpressionBody(), | ||
PropertyDeclarationSyntax property => property.ExpressionBody ?? GetAccessor(property)?.ExpressionBody(), | ||
_ => ((LocalFunctionStatementSyntaxWrapper)node).ExpressionBody, | ||
}; | ||
|
||
private static AccessorDeclarationSyntax GetAccessor(PropertyDeclarationSyntax property) => | ||
property.AccessorList?.Accessors.FirstOrDefault(a => a.IsKind(SyntaxKind.GetAccessorDeclaration)); | ||
property.AccessorList.Accessors.FirstOrDefault(x => x.IsKind(SyntaxKind.GetAccessorDeclaration)); | ||
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. While the grammar defines the |
||
|
||
private static IEnumerable<SyntaxNode> GetReturnNullOrDefaultExpressions(SyntaxNode methodBlock) => | ||
methodBlock.DescendantNodes(n => | ||
|
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.
Can't we remove this method and use the SyntaxExtensions?
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.
Discussed offline, this always returns the body of the
get
accessor. We would have to return an array instead of a singleBlockSyntax
.