Skip to content

RS1034 code fix produces incorrect code with null conditional #5740

Open

Description

Analyzer

Diagnostic ID: RS1034: Prefer 'IsKind' for checking syntax kinds

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.Analyzers

Version: 3.3.3 (Latest)

Describe the bug

The code fix for RS1034 applies a code change that leads to malfunctions at runtime.

Steps To Reproduce

git clone https://github.com/microsoft/vs-threading.git
cd vs-threading
git checkout 07ca567041a077f2814f6b096237a9d76884c5b0

Change FixUtils.cs L104 to:

hasReturnValue = (method.ReturnType as PredefinedTypeSyntax)?.Keyword.Kind() != SyntaxKind.VoidKeyword;

Expected behavior

-hasReturnValue = (method.ReturnType as PredefinedTypeSyntax)?.Keyword.Kind() != SyntaxKind.VoidKeyword;
+hasReturnValue = (method.ReturnType as PredefinedTypeSyntax)?.Keyword.IsKind(SyntaxKind.VoidKeyword) is not true;

(or some other equivalent transaction.

Actual behavior

-hasReturnValue = (method.ReturnType as PredefinedTypeSyntax)?.Keyword.Kind() != SyntaxKind.VoidKeyword;
+hasReturnValue = !(method.ReturnType as PredefinedTypeSyntax).IsKind(SyntaxKind.VoidKeyword);

The transaction here is not equivalent in behavior. Note the drop of the .Keyword in the expression, which leads to the IsKind to always return false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Area-Microsoft.CodeAnalysis.AnalyzersBugThe product is not behaving according to its current intended designhelp wantedThe issue is up-for-grabs, and can be claimed by commenting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions