Skip to content

BRO1145: A class, struct or interface with an empty body should end with ';'

A class, struct or interface with nothing in its body ends with ;: class Marker; (C# 12). Records have their own rule, BRO1140. Not a StyleCop rule: from Meziantou's MA0206. Off by default, also in the preset.

Example

root = true

[*.cs]
dotnet_diagnostic.BRO1145.severity = warning

Before

public class Marker
{
}

public interface ITag { }

public sealed class Point(int x, int y) : Marker, ITag { }

After

public class Marker;

public interface ITag;

public sealed class Point(int x, int y) : Marker, ITag;

Why

The empty braces suggest a body that isn't there. It's a matter of taste, and the short form needs C# 12, which is why the rule is off by default; turn it on with dotnet_diagnostic.BRO1145.severity = warning.

How the fix works

The text from the end of the declaration's header (name, primary constructor, base types or constraints) to the } becomes ;. A ; after the } (which BRO1101 would remove) is part of the replaced text, and an empty body that BRO1509 expands first is replaced the same way, so the rules converge in any order.

Not reported

  • A file compiled as C# 11 or older (class C; doesn't compile there).
  • A project with several target frameworks, unless every framework defaults to C# 12 or later (.NET 8 and later), or the project sets LangVersion (then every framework uses it). Without LangVersion, net48 and netstandard2.0 compile as C# 7.3, and dotnet format writes a fix from the newer framework's compilation into the file every framework shares. The package's build targets pass TargetFrameworks, LangVersion and the SDK's default for the framework (MaxSupportedLangVersion) to the analyzer: when they're equal, the project didn't set LangVersion. A LangVersion that the project sets differently per framework (a condition on TargetFramework) isn't detected; set the rule to none in such a project. Without the package (an analyzer reference), only the file's version counts.
  • Records (BRO1140) and enums (enum E; doesn't exist).
  • A comment or directive inside or around the braces.

Compared with StyleCop

StyleCop has no such rule. stylebro-migrate leaves it off (see Rules beyond StyleCop).