dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.11k stars 4.04k forks source link

Incorrectly correcting a lambda expression, changing the behaviour of a delegate #70268

Open Casperfrc opened 1 year ago

Casperfrc commented 1 year ago

Version Used:

Microsoft Visual Studio Professional 2022 (64-bit) - Current Version 17.7.4 Microsoft .NET Framework Version 4.8.04084 Steps to Reproduce:

Add the following lin to .editorconfig: "dotnet_diagnostic.IDE0052.severity = suggestion" Setup Visual Studio such that on save a code cleanup is run. Tools -> Options -> Text Editor -> Code Cleanup -> Tick box that says "Runc Dode Cleanup profile on Save" Now setup Code Cleanup to "Fix all warnings and errors set in EditorConfig". Analyze -> Code Cleanup -> Configure Code Cleanup -> Move the fixer up such that it is active Create a C# file that has the following source code:

using System;

internal static class C {
    private static void Main() {

        // Accepting suggestion "IDE0053 Use expression body"
        // leads to changed behavior:

        P(() => { DoWork(); });

    }

    private static void P(Action a) {
        Console.WriteLine("P: action");
        a();
    }
    private static void P(Func<bool> f) {
        Console.WriteLine("P: func");
        bool r = f();
        Console.WriteLine("f returned " + r);
        throw new Exception();
    }

    private static bool DoWork() {
        Console.WriteLine("Working");
        return true;
    }
}

Diagnostic Id: "dotnet_diagnostic.IDE0052

Expected Behavior: I would not expect it to change the behaviour of the delegate

Actual Behavior: It changed the behaviour of the delegate such that it was no longer a delegate returning "void" but instead it returns the actual value.

CyrusNajmabadi commented 3 days ago

Unclear why something set to 'suggestion' is running when the features say to only run error/warning fixers.