BRO1601: Overrides and implementations should inherit their documentation¶
An override or interface implementation without documentation gets /// <inheritdoc/>, which takes the
documentation from the member it overrides or implements. Replaces StyleCop SA1600 for these members.
Example¶
Before¶
using System;
/// <summary>Something that has a size.</summary>
public interface ISized
{
/// <summary>Gets the size in bytes.</summary>
long Size { get; }
}
/// <summary>A file on disk.</summary>
public class DiskFile : ISized, IDisposable
{
public long Size => 0;
public void Dispose()
{
}
public override string ToString() => "file";
}
After¶
using System;
/// <summary>Something that has a size.</summary>
public interface ISized
{
/// <summary>Gets the size in bytes.</summary>
long Size { get; }
}
/// <summary>A file on disk.</summary>
public class DiskFile : ISized, IDisposable
{
/// <inheritdoc/>
public long Size => 0;
/// <inheritdoc/>
public void Dispose()
{
}
/// <inheritdoc/>
public override string ToString() => "file";
}
Why¶
The documentation of an override or implementation is usually the same as its base's. <inheritdoc/> says so
explicitly, and IntelliSense and documentation generators show the inherited text.
Other missing documentation isn't reported. The only automatic fix for a method or class without documentation is placeholder text, which satisfies the rule without documenting anything. StyleBro only reports what it can fix meaningfully.
How the fix works¶
/// <inheritdoc/> is inserted on its own line right before the member, above its attributes, at the member's
indentation. A plain // comment above the member stays above it; blank lines between that comment and the member go too when
BRO1506 is on (it would remove them, but can't see them once the /// is in between). When code sits on the line above, a blank line goes
in between (BRO1513). A single-line property right below the documented one stays there
(BRO1505 lets single-line properties sit together, documented or not).
Checked: methods, properties, indexers and events that override a member or implicitly implement an interface member.
Explicit interface implementations (void IDisposable.Dispose()) aren't: they're only reachable through the interface,
whose documentation tools show (see decisions).
Not reported¶
- Projects that don't generate documentation (no
<GenerateDocumentationFile>true</GenerateDocumentationFile>). The package's build targets pass the property to the analyzer, so a build anddotnet formatagree (dotnet formatparses documentation comments even where the build doesn't). Without the package's targets (the analyzer referenced directly), the compiler's documentation mode decides. - A member with
#if,#elif,#elseor#endifabove it or between its attributes. In a multi-targeted project the member can start with a different token in each target framework ([Obsolete]only under#if NET5_0_OR_GREATER), each copy wanted<inheritdoc/>in another place, and BRO1602 turned the other copy's into//:dotnet formatnever converged. Other directives (#region,#pragma) are fine. - Explicit interface implementations (see How the fix works).
Configuration¶
Which members need documentation. A member counts by its effective accessibility: a public member of an internal class is internal, of a private class private; protected members are exposed.
[*.cs]
# Visible outside the assembly (default true)
stylebro_document_exposed_elements = true
# Visible only inside the assembly (default true)
stylebro_document_internal_elements = true
# Private, or in a private type (default false)
stylebro_document_private_elements = false
How the inserted tag is written (existing tags are never rewritten):
[*.cs]
# compact (default): /// <inheritdoc/>
# spaced: /// <inheritdoc />
stylebro_inheritdoc_style = compact
With spaced:
/// <inheritdoc />
public override string ToString() => "file";