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
18.92k stars 4.02k forks source link

VS 2019 suggests turning single-line method statements to expression body methods, even if the statement is wrapped in an #if directive. #41596

Open vsfeedback opened 4 years ago

vsfeedback commented 4 years ago

This issue has been moved from a ticket on Developer Community.


VS 2019 suggests turning single-line statements to expression body methods, even if the statement is wrapped in an #if directive. It also does this automatically if I select "For the whole project" on another suggestion (I'm including a picture on what I mean)

Some code you can use to reproduce this:

// Will show a suggestion to turn this in an expression body method, if that's the prefered code-style on the settings.
protected static string GetPluginPath() {
#if FALSE_STATEMENT1 || FALSE_STATEMENT2 || FALSE_STATEMENT3
        string x = "This doesn't matter";
        return x + ".";
#elif FALSE_STATEMENT4
                string x = "This doesn't matter either";
        return x + ".";
#else
        return null;
#endif
}

Original Comments

Visual Studio Feedback System on 2/10/2020, 02:37 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

MrFelna commented 3 years ago

Still an issue with VS2019 16.8.4 Simple code example - if the suggestion is used the code won't compile when targeting Release

// VS2019 will prompt to use expression body
private static void SampleMethod()
{
#if DEBUG
    Console.WriteLine("Invoked SampleMethod!");
#endif
}

image

CyrusNajmabadi commented 3 years ago

@MrFelna would you like to contribute a fix here?

MrFelna commented 3 years ago

@CyrusNajmabadi - I can have a look, but I have not reviewed the Roslyn code before, nor any compiler code. Do you think this is something someone new to the codebase can work on? Having briefly looked at the Contributing-Code .md file, I think it can be worked on as a bug fix.

I found this item from the Developer community and wanted to avoid reporting a duplicate issue.