Skip to content

Bump Roslyn version to 4.2.0 for tests and fix the breaking changes - #5903

Merged
costin-zaharia-sonarsource merged 14 commits into
masterfrom
costin/tests-roslyn-4.2.0
Jul 26, 2022
Merged

costin-zaharia-sonarsource merged 14 commits into
masterfrom
costin/tests-roslyn-4.2.0

Conversation

@costin-zaharia-sonarsource

@costin-zaharia-sonarsource costin-zaharia-sonarsource commented Jul 25, 2022 •

Copy link
Copy Markdown
Member

Related to #5661

Between 4.0.0-4.final and 4.2.0 there were a couple of breaking changes that we need to handle:

  • Operation BinaryOperator has been renamed to Binary
  • Operation UnaryOperator has been renamed to 'Unary'
  • Roslyn team has fixed Duplicate syntax node callbacks for top-level statements dotnet/roslyn#58561 and the check for top-level statements is no longer correct
  • I had to fix a few tests cases since some pattern matching flows are checked now at compile time and can trigger compilation errors
  • I had to fix a couple of test cases since the compiler no longer generated the record struct default constructor.
  • Record struct properties need to be initialized.

I will add a test with the previous Roslyn version in a separate PR.

@costin-zaharia-sonarsource costin-zaharia-sonarsource changed the title Bump Roslyn version to 4.2.0 for tests and fix broken chnages Bump Roslyn version to 4.2.0 for tests and fix broken changes Jul 25, 2022

@mary-georgiou mary-georgiou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mary-georgiou mary-georgiou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case for that? You registered for LocalFunctionStatement so c.Node must be a MethodDeclarationBaseSyntax. What am I missing here?

@costin-zaharia-sonarsource costin-zaharia-sonarsource Jul 25, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +78 to +79
private static bool IsTopLevelMain(SyntaxNodeAnalysisContext context) =>
context.ContainingSymbol.IsTopLevelMain();
context.Node.IsInTopLevelMain();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 true only if the Ancestors don't have any method declaration or type declaration but a SyntaxKind.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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and we want to handle also the ones from top level main.

So you just don't call IsInTopLevelMain then?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm thinking to change the check a bit but only for MethodsShouldNotHaveTooManyLines

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor optional comments, but LGTM in general.

Comment on lines +28 to +29
// The tree looks like: CompilationUnit -> GlobalStatement -> LocalFunction and we check the compilation unit to be top-level main
localFunction.SyntaxNode.Parent.Parent.IsTopLevelMain();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may throw NREs and you can get rid of the comment, by just testing for it. This also removes the IsTopLevelMain:

Suggested change
// 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 } } };

Comment on lines +258 to +259
public static bool IsTopLevelMain(this SyntaxNode node) =>
node is CompilationUnitSyntax compilationUnit && compilationUnit.Members.Any(x => x.IsKind(SyntaxKind.GlobalStatement));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be on CompilationUnitSyntax. Otherwise, it is misleading with the "SyntaxNode".

Suggested change
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));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this but I wanted to avoid all the casts and additional checks. I see your point though.

Comment on lines +258 to +259
public static bool IsTopLevelMain(this CompilationUnitSyntax compilationUnit) =>
compilationUnit.Members.Any(x => x.IsKind(SyntaxKind.GlobalStatement));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@costin-zaharia-sonarsource costin-zaharia-sonarsource changed the title Bump Roslyn version to 4.2.0 for tests and fix broken changes Bump Roslyn version to 4.2.0 for tests and fix the breaking changes Jul 26, 2022
@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

92.3% 92.3% Coverage
0.0% 0.0% Duplication

@costin-zaharia-sonarsource
costin-zaharia-sonarsource merged commit 15829d3 into master Jul 26, 2022
@costin-zaharia-sonarsource
costin-zaharia-sonarsource deleted the costin/tests-roslyn-4.2.0 branch July 26, 2022 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate syntax node callbacks for top-level statements

3 participants