Repository navigation
Bump Roslyn version to 4.2.0 for tests and fix the breaking changes - #5903
Conversation
mary-georgiou
left a comment
There was a problem hiding this comment.
All seem to be good - I just have a question/proposal regarding the new extension method for detecting if a node is part of a top-level statement.
Also, ITs need an update.
c5132da to
ac724e6
Compare
martin-strecker-sonarsource
left a comment
There was a problem hiding this comment.
There are a lot of new issues found. Have you checked if these are all true positives?
| { | ||
| if (c.ContainingSymbol.IsTopLevelMain() | ||
| if ((c.Node.IsInTopLevelMain() | ||
| && !c.Node.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().Any()) |
There was a problem hiding this comment.
What is the use case for that? You registered for LocalFunctionStatement so c.Node must be a MethodDeclarationBaseSyntax. What am I missing here?
There was a problem hiding this comment.
Used to handle cases like this:
public void WithLocalFunction() // Noncompliant
{
LocalFunction();
void LocalFunction()
{
var i = 1; i++;
i++;
i++;
i++;
}
}
Otherwise, we will raise the issue twice, once for the function and a second time for the local one.
| private static bool IsTopLevelMain(SyntaxNodeAnalysisContext context) => | ||
| context.ContainingSymbol.IsTopLevelMain(); | ||
| context.Node.IsInTopLevelMain(); |
There was a problem hiding this comment.
Looks like it can be inlined.
| x = m switch | ||
| { | ||
| string s2 and null => s2, // Compliant - rule ConditionEvaluatesToConstant will raise issue here | ||
| string s2 and null => s2, // Error [CS8502] - Compliant - rule ConditionEvaluatesToConstant will raise issue here |
There was a problem hiding this comment.
Same here
There was a problem hiding this comment.
The other one is the fixed file. It will mirror the changes done in this one.
| } | ||
|
|
||
| public static bool IsInTopLevelMain(this SyntaxNode node) => | ||
| node.SyntaxTree.GetRoot()?.ChildNodes().OfType<GlobalStatementSyntax>().Any() ?? false; |
There was a problem hiding this comment.
Looks valid to me.
Have you considered this:
It is unclear to me why it returns true if wrapped in a method in a top-level main. This is not compatible with context.ContainingSyntax used before. You may consider providing two extensions methods instead:
- One for SyntaxTree
- One for node, this one returns
trueonly if the Ancestors don't have any method declaration or type declaration but aSyntaxKind.GlobalStatement.
If you want to keep it as is, you should document the behavior with a doc comment, and also, ChildNodes() can be avoided. Some optimizations you may consider:
node.SyntaxTree.GetRoot() is { CompilationUnit { Members: { Length: >0 } members } } && members.Any(x => x.IsKind(SyntaxKind.GlobalStatement)
node.SyntaxTree.GetRoot().ChildNodes().Any(x => x.IsKind(SyntaxKind.GlobalStatement)
or a combination of it.
There was a problem hiding this comment.
node is CompilationUnitSyntax compilationUnit && compilationUnit.Members.Any(x => x.IsKind(SyntaxKind.GlobalStatement)); does the trick for all rules but one: MethodsShouldNotHaveTooManyLines. In this case, we subscribe for local functions and we want to handle also the ones from top level main.
There was a problem hiding this comment.
and we want to handle also the ones from top level main.
So you just don't call IsInTopLevelMain then?
There was a problem hiding this comment.
Yes, I'm thinking to change the check a bit but only for MethodsShouldNotHaveTooManyLines
64324c1 to
b7515db
Compare
martin-strecker-sonarsource
left a comment
There was a problem hiding this comment.
Some minor optional comments, but LGTM in general.
| // The tree looks like: CompilationUnit -> GlobalStatement -> LocalFunction and we check the compilation unit to be top-level main | ||
| localFunction.SyntaxNode.Parent.Parent.IsTopLevelMain(); |
There was a problem hiding this comment.
This may throw NREs and you can get rid of the comment, by just testing for it. This also removes the IsTopLevelMain:
| // The tree looks like: CompilationUnit -> GlobalStatement -> LocalFunction and we check the compilation unit to be top-level main | |
| localFunction.SyntaxNode.Parent.Parent.IsTopLevelMain(); | |
| localFunction is { SyntaxNode: { Parent: GlobalStatementSyntax { Parent: CompilationUnitSyntax } } }; |
| public static bool IsTopLevelMain(this SyntaxNode node) => | ||
| node is CompilationUnitSyntax compilationUnit && compilationUnit.Members.Any(x => x.IsKind(SyntaxKind.GlobalStatement)); |
There was a problem hiding this comment.
This should be on CompilationUnitSyntax. Otherwise, it is misleading with the "SyntaxNode".
| public static bool IsTopLevelMain(this SyntaxNode node) => | |
| node is CompilationUnitSyntax compilationUnit && compilationUnit.Members.Any(x => x.IsKind(SyntaxKind.GlobalStatement)); | |
| public static bool IsTopLevelMain(this CompilationUnitSyntax compilationUnit) => | |
| compilationUnit.Members.Any(x => x.IsKind(SyntaxKind.GlobalStatement)); |
There was a problem hiding this comment.
I thought about this but I wanted to avoid all the casts and additional checks. I see your point though.
b7515db to
4287847
Compare
| public static bool IsTopLevelMain(this CompilationUnitSyntax compilationUnit) => | ||
| compilationUnit.Members.Any(x => x.IsKind(SyntaxKind.GlobalStatement)); |
There was a problem hiding this comment.
In case you missed this: IsTopLevelMain is in the wrong file now.
CSharp10: Unexpected build error [CS8983]: A 'struct' with field initializers must include an explicitly declared constructor
- it seems that the dotnet version used for running the ITs was already newer and the top level statements were not correctly checked
4287847 to
746bb2b
Compare
|
Kudos, SonarCloud Quality Gate passed! |
|
Kudos, SonarCloud Quality Gate passed! |










Related to #5661
Between
4.0.0-4.finaland4.2.0there were a couple of breaking changes that we need to handle:BinaryOperatorhas been renamed toBinaryUnaryOperatorhas been renamed to 'Unary'I will add a test with the previous Roslyn version in a separate PR.