Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reproducers for S1144 FN/FP #7925

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions analyzers/src/SonarAnalyzer.CSharp/Rules/UnusedPrivateMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public sealed class UnusedPrivateMember : SonarDiagnosticAnalyzer

private static readonly DiagnosticDescriptor RuleS1144 = DescriptorFactory.Create(S1144DiagnosticId, S1144MessageFormat);
private static readonly DiagnosticDescriptor RuleS4487 = DescriptorFactory.Create(S4487DiagnosticId, S4487MessageFormat);
private static readonly ImmutableArray<KnownType> IgnoredTypes =
ImmutableArray.Create(
KnownType.UnityEditor_AssetModificationProcessor,
KnownType.UnityEditor_AssetPostprocessor,
KnownType.UnityEngine_MonoBehaviour,
KnownType.UnityEngine_ScriptableObject,
KnownType.Microsoft_EntityFrameworkCore_Migrations_Migration);

private static readonly ImmutableArray<KnownType> IgnoredTypes = ImmutableArray.Create(
KnownType.UnityEditor_AssetModificationProcessor,
KnownType.UnityEditor_AssetPostprocessor,
KnownType.UnityEngine_MonoBehaviour,
KnownType.UnityEngine_ScriptableObject,
KnownType.Microsoft_EntityFrameworkCore_Migrations_Migration);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(RuleS1144, RuleS4487);
protected override bool EnableConcurrentExecution => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,23 @@ private enum Y
B
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/6699
public class Repro_6699
{
public void MethodUsingLocalMethod()
{
void LocalMethod() { } // FN
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/6724
public class Repro_6724
{
public int PrivateGetter { private get; set; } // FN
public int PrivateSetter { get; private set; } // FN

public int ExpressionBodiedPropertyWithPrivateGetter { private get => 1; set => _ = value; } // FN
public int ExpressionBodiedPropertyWithPrivateSetter { get => 1; private set => _ = value; } // FN
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Tests.Diagnostics
Expand Down Expand Up @@ -119,4 +120,15 @@ private record PrivateRecordRef
[DllImport("user32.dll")]
private static extern bool ExternalMethod(ref PrivateRecordRef reference);
}

// https://github.com/SonarSource/sonar-dotnet/issues/7904
public sealed record Repro_7904
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the following test case as well, to make sure when the FP is fixed it won't create new FNs:

public class ClassWithPrintMembersMethod
{
    private bool PrintMembers(StringBuilder builder) // Noncompliant
    {
        return true;
    }
}

{
// Used by the runtime to create a string representation of the record.
// See also https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record#printmembers-formatting-in-derived-records
private bool PrintMembers(StringBuilder builder) // Noncompliant FP
{
return true;
}
}
}