protected static DiagnosticResult ExpectSuppressor(SuppressionDescriptor descriptor)
    {
        var result = new DiagnosticResult(descriptor.Id, DiagnosticSeverity.Hidden)
                     .WithMessageFormat(descriptor.Justification)
                     .WithSuppressedId(descriptor.SuppressedDiagnosticId);

        return(result);
    }
            private SuppressorInfo(DiagnosticSuppressor suppressor, SuppressionDescriptor descriptor)
                : base(suppressor.GetType(), descriptor.Id)
            {
                this.Suppressor = suppressor;
                this.Descriptor = descriptor;
                var name = descriptor.Id + ".md";

                this.HelpLinkUri = $"https://github.com/nunit/nunit.analyzers/tree/master/documentation/{name}";
                this.Stub        = CreateStub(suppressor, descriptor);
            }
Example #3
0
        private Suppression(SuppressionDescriptor descriptor, Diagnostic suppressedDiagnostic)
        {
            Descriptor           = descriptor ?? throw new ArgumentNullException(nameof(descriptor));
            SuppressedDiagnostic = suppressedDiagnostic ?? throw new ArgumentNullException(nameof(suppressedDiagnostic));
            Debug.Assert(suppressedDiagnostic.ProgrammaticSuppressionInfo == null);

            if (descriptor.SuppressedDiagnosticId != suppressedDiagnostic.Id)
            {
                // Suppressed diagnostic ID '{0}' does not match suppressable ID '{1}' for the given suppression descriptor.
                var message = string.Format(CodeAnalysisResources.InvalidDiagnosticSuppressionReported, suppressedDiagnostic.Id, descriptor.SuppressedDiagnosticId);
                throw new ArgumentException(message);
            }
        }
Example #4
0
        private static void AssertProgrammaticSuppression(Diagnostic error, SuppressionDescriptor suppressionDescriptor)
        {
            // We cannot check for the correct suppression nicely.
            // DiagnosticWithProgrammaticSuppression is a private class.
            // ProgrammaticSuppressionInfo is an internal class.
            // Resorting to reflection ...
            Type errorType = error.GetType();

            Assert.That(errorType.Name, Is.EqualTo("DiagnosticWithProgrammaticSuppression"));

            var programmaticSuppressionInfoProperty = errorType.GetProperty("ProgrammaticSuppressionInfo",
                                                                            BindingFlags.Instance | BindingFlags.NonPublic);

            if (programmaticSuppressionInfoProperty == null)
            {
                Assert.Fail("Expected a property with the name 'ProgrammaticSuppressionInfo'");
                return;
            }

            var programmaticSuppressionInfo = programmaticSuppressionInfoProperty.GetValue(error);

            if (programmaticSuppressionInfo != null)
            {
                Type programmaticSuppressionInfoType = programmaticSuppressionInfo.GetType();
                Assert.That(programmaticSuppressionInfoType.Name, Is.EqualTo("ProgrammaticSuppressionInfo"));

                var suppressionsProperty = programmaticSuppressionInfoType.GetProperty("Suppressions");
                if (suppressionsProperty != null)
                {
                    var suppressions = suppressionsProperty.GetValue(programmaticSuppressionInfo);

                    if (suppressions is ImmutableHashSet <(string Id, LocalizableString Justification)> suppressionsHashSet)
                    {
                        Assert.That(suppressionsHashSet, Does.Contain((suppressionDescriptor.Id, suppressionDescriptor.Justification)));
                    }
                }
            }
        }
            private static string CreateStub(DiagnosticSuppressor suppressor, SuppressionDescriptor descriptor)
            {
                var builder = new StringBuilder();

                builder.Append("| Code     | ");
                builder.Append($"[{suppressor.GetType().Name}]({CodeFile.Find(suppressor.GetType()).Uri})");

                var text = builder.ToString();
                var stub = $@"# {descriptor.Id}

## {EscapeTags(descriptor.Justification)}

| Topic    | Value
| :--      | :--
| Id       | {descriptor.Id}
| Severity | Info
| Enabled  | True
| Category | Suppressor
| Code     | [<TYPENAME>](<URL>)

## Description

{EscapeTags(descriptor.Justification)}

## Motivation

ADD MOTIVATION HERE

## How to fix violations

ADD HOW TO FIX VIOLATIONS HERE

<!-- start generated config severity -->
## Configure severity

The rule has no severity, but can be disabled.

### Via ruleset file

To disable the rule for a project, you need to add a
[ruleset file](https://github.com/nunit/nunit.analyzers/blob/master/src/nunit.analyzers/DiagnosticSuppressors/NUnit.Analyzers.Suppressions.ruleset)

```xml
<?xml version=""1.0"" encoding=""utf-8""?>
<RuleSet Name=""NUnit.Analyzer Suppressions"" Description=""DiagnosticSuppression Rules"" ToolsVersion=""12.0"">
  <Rules AnalyzerId=""DiagnosticSuppressors"" RuleNamespace=""NUnit.NUnitAnalyzers"">
    <Rule Id=""NUnit3001"" Action=""Info"" /> <!-- Possible Null Reference -->
    <Rule Id=""NUnit3002"" Action=""Info"" /> <!-- NonNullableField is Uninitialized -->
  </Rules>
</RuleSet>
```

and add it to the project like:

```xml
<PropertyGroup>
  <CodeAnalysisRuleSet>NUnit.Analyzers.Suppressions.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
```

For more info about rulesets see [MSDN](https://msdn.microsoft.com/en-us/library/dd264949.aspx).

### Via .editorconfig file

This is currently not working. Waiting for [Roslyn](https://github.com/dotnet/roslyn/issues/49727)

```ini
# {descriptor.Id}: {descriptor.Justification.ToString(CultureInfo.InvariantCulture)}
dotnet_diagnostic.{descriptor.Id}.severity = none
```
<!-- end generated config severity -->
";

                return(Replace(stub, "| Code     | [<TYPENAME>](<URL>)", text));
            }
Example #6
0
 /// <summary>
 /// Creates a suppression of a <see cref="Diagnostic"/> with the given <see cref="SuppressionDescriptor"/>.
 /// </summary>
 /// <param name="descriptor">
 /// Descriptor for the suppression, which must be from <see cref="DiagnosticSuppressor.SupportedSuppressions"/>
 /// for the <see cref="DiagnosticSuppressor"/> creating this suppression.
 /// </param>
 /// <param name="suppressedDiagnostic">
 /// <see cref="Diagnostic"/> to be suppressed, which must be from <see cref="SuppressionAnalysisContext.ReportedDiagnostics"/>
 /// for the suppression context in which this suppression is being created.</param>
 public static Suppression Create(SuppressionDescriptor descriptor, Diagnostic suppressedDiagnostic)
 => new Suppression(descriptor, suppressedDiagnostic);
 public static void ReportSuppression(this SuppressionAnalysisContext context, SuppressionDescriptor descriptor, Diagnostic suppressedDiagnostic) =>
 context.ReportSuppression(Suppression.Create(descriptor, suppressedDiagnostic));