KirillOsenkov / RoslynQuoter

Roslyn tool that for a given C# program shows syntax tree API calls to construct its syntax tree
http://roslynquoter.azurewebsites.net
Apache License 2.0
909 stars 120 forks source link

`not` patterns incorrectly parsed #71

Closed AArnott closed 2 years ago

AArnott commented 2 years ago

Parse o is not null as an expression, and you get:

IsPatternExpression(
    IdentifierName("o"),
    UnaryPattern(
        ConstantPattern(
            LiteralExpression(
                SyntaxKind.NullLiteralExpression))))
.NormalizeWhitespace()

Note that the not in the expression is nowhere represented.

KirillOsenkov commented 2 years ago

This appears to be correct: the UnaryPattern only allows NotKeyword: https://github.com/dotnet/roslyn/blob/badcd003f616de451aa1ce04191f27c0be835c4d/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Main.Generated.cs#L3720

If you check Keep redundant API calls it will show the NotKeyword: image

Quoter has detected that setting it explicitly is redundant and has eliminated it.

P.S. I'm not quite sure why the syntax tree shown by SharpLab looks like this though: image

Maybe @ashmind knows?

When I parse myself and look at the syntax tree, it looks just like what Quoter does: image

AArnott commented 2 years ago

When I use the code the quoter gives me, no not keyword appears in the generated C# code.

This is what I had to use to actually get not to show up: https://github.com/AArnott/CSharpIsNull/blob/09d8a209b3e150cc79ba4eeaf2dedd69a6a76999/src/CSharpIsNullAnalyzer.CodeFixes/CSIsNull002Fixer.cs#L36

AArnott commented 2 years ago

Ok, never mind. It works. I guess I had some other bug at the time when it appeared to not work.