NoSpaceBeforeBrace

NoSpaceBeforeBrace

Summary

A brace (‘{’ or ‘}’) must be preceded by a white space.

Default severity

Warning

Description

An opening brace (‘{’) that is not the first character on the line must be preceded by a single space or an opening parenthesis (‘(’).

A closing brace (‘}’) that is not the first character on the line must be preceded by a single space or an opening brace (‘{’).

Note that this analyzer and the NoSpaceAfterBrace analyzer are intended to be used together and replace SA1012 and SA1013 with them, allowing us to write empty braces ({}) as follows:

Action doNothing = () => {};

if (maybeString is {} s)
{
    ⋮
}

Code fix

The code fix provides an option inserting a space before the brace.

Example

Diagnostic

string[] array ={ ""};

Action doNothing = () =>{ return;};

if (array is{ Length: 0} z)
{
}

Code fix

string[] array = { "" };

Action doNothing = () => { return; };

if (array is { Length: 0 } z)
{
}