BRO1404: Access modifier should be declared¶
Types and members say their accessibility instead of relying on the default. Replaces StyleCop SA1400.
Example¶
Before¶
class Cache
{
int hits;
void Reset()
{
hits = 0;
}
}
After¶
internal class Cache
{
private int hits;
private void Reset()
{
hits = 0;
}
}
Why¶
The default accessibility differs by place (internal for a top-level type, private for a member, public in an
interface), so a declaration without a modifier makes the reader work it out.
How the fix works¶
The fix writes the accessibility the declaration has now, so nothing changes in what can use it. It goes before the other modifiers (after attributes), where IDE0036's modifier order wants it.
This rule replaces the SDK's IDE0040, whose fix writes merge conflict markers in multi-targeted projects under
dotnet format (reported upstream as dotnet/roslyn#86000; see decisions).
bro init --write carries suppressions of IDE0040 over to this rule and BRO1007 (#pragma warning disable IDE0040,
[SuppressMessage], <NoWarn>), so a declaration you exempted from IDE0040 stays exempt. A suppression written after
init names BRO1404 itself.
Configuration¶
[*.cs]
# The SDK's setting, which BRO1404 and BRO1007 follow:
# for_non_interface_members (or not set): as described here
# always: interface members get one too ('public', C# 8 and later)
# never, omit_if_default: no modifiers are asked for (nothing is reported)
dotnet_style_require_accessibility_modifiers = for_non_interface_members
Not reported¶
- Interface members and types nested in interfaces. With
alwaysthey are, from C# 8; a file also compiled with an older version (a multi-targeted project's net472 build: C# 7.3) keeps the warning but isn't fixed. - Explicit interface implementations, static constructors, operators, finalizers and
filetypes, which can't or needn't have one. - Partial members (another part may declare it); partial types are BRO1007's.
- Declarations whose modifiers depend on
#if: a skipped branch with only modifiers before the declaration (#if !X/file/#endif/static class C), or a directive between its modifiers. Where the branch is on, the declaration has that modifier, and one added here would befile internal(CS9052) or a second accessibility.