dotnet/roslyn

Better handling object initializer syntax error

Open

#22,790 opened on Oct 20, 2017

View on GitHub
 (9 comments) (0 reactions) (0 assignees)C# (20,414 stars) (4,257 forks)batch import
Area-CompilersFeature Requesthelp wanted

Description

VS 2017 (15.5.0):

There is an error I actually keep doing (my bad) when using the object initializer syntax that is ending the line with a semicolon instead of a comma, e.g.:

return new OperationResponse
{
   SomeProperty= SomeValue,
   AnotherProperty = AnotherValue;
};

please note the semicolon at the end of line AnotherProperty = AnotherValue;. While this is syntax mistake, looks like roslyn is getting confused (a lot actually) with this type of error and put a lot of squiggles under various csharp constructs and starts suggesting wrong things, e.g.

try
{
   return new OperationResponse
   {
      SomeProperty= SomeValue,
      AnotherProperty = AnotherValue;
   };
}
catch (Exception ex) {}

in this case it puts a red squiggle under the curly brace that closes the try statements, and the popup related to the squiggle is something like: the following image

that is a bit misleading.

While this is definitely a cosmetic issue (IMO), I think it would be very cool if roslyn could better handle these type of syntax error, pointing you to the error instead of giving a misleading message.

Contributor guide