Why is this an issue?

The ExcludeFromCodeCoverageAttribute is used to exclude portions of code from code coverage reporting. It is a bad practice to retain code that is not covered by unit tests. In .Net 5, the Justification property was added to the ExcludeFromCodeCoverageAttribute as an opportunity to document the rationale for the exclusion. This rule raises an issue when no such justification is given.

Noncompliant code example

Public Structure Coordinates

    Public ReadOnly Property X As Integer
    Public ReadOnly Property Y As Integer

    <ExcludeFromCodeCoverage> ' Noncompliant
    Public Overrides Function Equals(obj As Object) As Boolean
        If Not (TypeOf obj Is Coordinates) Then
            Return False
        End If

        Dim coordinates = DirectCast(obj, Coordinates)
        Return X = coordinates.X AndAlso
               Y = coordinates.Y
    End Function

    <ExcludeFromCodeCoverage> ' Noncompliant
    Public Overrides Function GetHashCode() As Integer
        Dim hashCode As Long = 1861411795
        hashCode = (hashCode * -1521134295 + X.GetHashCode()).GetHashCode()
        hashCode = (hashCode * -1521134295 + Y.GetHashCode()).GetHashCode()
        Return hashCode
    End Function
End Structure

Compliant solution

Public Structure Coordinates

    Public ReadOnly Property X As Integer
    Public ReadOnly Property Y As Integer

    <ExcludeFromCodeCoverage(Justification:="Code generated by Visual Studio refactoring")> ' Compliant
    Public Overrides Function Equals(obj As Object) As Boolean
        If Not (TypeOf obj Is Coordinates) Then
            Return False
        End If

        Dim coordinates = DirectCast(obj, Coordinates)
        Return X = coordinates.X AndAlso
               Y = coordinates.Y
    End Function

    <ExcludeFromCodeCoverage(Justification:="Code generated by Visual Studio refactoring")> ' Compliant
    Public Overrides Function GetHashCode() As Integer
        Dim hashCode As Long = 1861411795
        hashCode = (hashCode * -1521134295 + X.GetHashCode()).GetHashCode()
        hashCode = (hashCode * -1521134295 + Y.GetHashCode()).GetHashCode()
        Return hashCode
    End Function
End Structure

Resources