Skip to content

SA1013 misfires for object initializer in interpolated string #3461

Closed

Description

@Timo-Weike

Code like

var x = 1;
var y = 2;
var z = 3;
                                                v
var formatedString = $"Something {new { x, y, z }}";

triggers "SA1013 Closing brace should be followed by a space", but adding space after the } doesn't look right and also triggers "SA1013 Closing brace should not be preceded by a space" and the description for SA1013 states

A closing brace should always be followed by a single space, unless it is the last character on the line, or unless it is followed by a closing parenthesis, a comma, or a semicolon.

StyleCop Version: 1.2.0-beta.406

Activity

  1. BDisp commented on Feb 22, 2022

    @BDisp

    I think the correct formatting is:

    var formatedString = $"Something { new { x, y, z } }";
  2. Timo-Weike commented on Feb 22, 2022

    @Timo-Weike
    Author

    I think the correct formatting is:

    var formatedString = $"Something { new { x, y, z } }";

    I disagree, you would also not write

    Foo( new { x, y, z} );

    or

    $"Something { x }"

    Also code like

    $"{new object()}"

    does not trigger SA1013, but

                            SA1013
                     SA1009 |
                          | |
                          v v
    var x = $"{new object() }";

    triggers SA1009 and SA1013

  3. BDisp commented on Feb 22, 2022

    @BDisp

    I disagree, you would also not write

    Foo( new { x, y, z} );

    I would write:

    Foo(new { x, y, z } );

    or

    $"Something { x }"

    if I want the space yes, otherwise;

    $"Something{ x }"

    or

    $"Something{x}"

    Also code like

    $"{new object()}"

    Inside a $"" is not mandatory, since you use the same rule on the left and on the right.

    does not trigger SA1013, but

                            SA1013
                     SA1009 |
                          | |
                          v v
    var x = $"{new object() }";

    triggers SA1009 and SA1013

    It must use the same rule on both side.

    var x = $"{new object()}";

    or

    var x = $"{ new object() }";
  4. sharwell commented on Feb 22, 2022

    @sharwell
    Member

    From the perspective of StyleCop Analyzers, the expected formatting here is:

    var formatedString = $"Something {new { x, y, z }}";
  5. self-assigned this
    on Feb 22, 2022
  6. added a commit that references this issue on Feb 22, 2022
    164a00d
  7. added this to the 1.2-beta.next milestone on Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions