Roslyn-analyzers: Way to suppress CA1812 for classes used via DI instead of the whole assembly?

Created on 2 Oct 2019  路  6Comments  路  Source: dotnet/roslyn-analyzers

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v2.9.5

Diagnostic ID

CA1812

Repro steps

Example Dependency Injected Service:

public interface ICategoryService
{
    Task<IEnumerable<MyItems>> GetMyItemsAsync(FilterValues filters);
}

internal sealed class CategoryService : ICategoryService
{
    #region Internal State
    private readonly ICategoryRepository categoryRepository;
    #endregion

    public CategoryService(ICategoryRepository categoryRepository)
    {
        this.categoryRepository = categoryRepository ?? throw new ArgumentNullException(nameof(categoryRepository));
    }

    #region ICategoryService Implementation
    public Task<IEnumerable<MyItems>> GetMyItemsAsync(FilterValues filters)
        => categoryRepository.GetMyItemsAsync(filters);
    #endregion
}

public static class InfrastructureDIConfig
{
    public static void AddInfrastructureServices(this IServiceCollection services)
    {
        services.TryAddScoped<ICategoryRepository, CategoryRepository>();
        services.TryAddScoped<ICategoryService, CategoryService>();
    }

}

Expected behavior

CA1812 should not fire when the internal class has been registered for DI.

Actual behavior

CA1812 falsely fires and suggests CategoryService should be removed.

_Migrated this issue from:
https://github.com/MicrosoftDocs/visualstudio-docs/issues/4028
as the somewhat unhelpful response I received there makes me think there isn't currently a way to suppress the CA1812 warning for only classes used via DI and not the whole assembly._

Bug Resolution-Duplicate Resolution-External Verified

Most helpful comment

To add to this, the reason I think this is 100% a false positive is that the type name in question _appears in the source code_ as part of a type expression, so clearly it _is_ used.

All 6 comments

I'm running into this all the time in a similar scenario. I have a generic factory where I register types (much like you do in a DI container) and I have to suppress the bogus warning on many internal types due to this.

To add to this, the reason I think this is 100% a false positive is that the type name in question _appears in the source code_ as part of a type expression, so clearly it _is_ used.

Bump.

I don't mind a false warning if I can suppress it. But I can't easily suppress this one, except for the whole assembly as @ideoclickVanessa says.

https://github.com/dotnet/roslyn-analyzers/issues/3210#issuecomment-647495727

It's because of annoyances like this that I'm sorely tempted to give up on FxCopAnalyzers altogether. I've learnt useful lessons from it, but these days it's more hindrance and annoyance than help.

BTW you can definitely suppress this code analysis warning for just the 1 affected line, but VS doesn't offer that for the reasons mentioned in https://github.com/dotnet/roslyn-analyzers/issues/3210#issuecomment-647540527.

Here's what I do:

```c#

pragma warning disable CA1812 // Internal class that is apparently never instantiated; this class is instantiated generically

internal class BlazorHybridJSRuntime : JSRuntime

pragma warning restore CA1812 // Internal class that is apparently never instantiated

    ...

```

I confirm that with version 5.0.1 and 6.0.0-preview1.20609.1 it is still not possible to suppress the issue.

@mavasani I am right to assume that the Suppress or Configure issues menu for a rule is handled on roslyn's side? I think that's the real issue behind this ticket.

See https://github.com/dotnet/roslyn-analyzers/issues/3210#issuecomment-747455459

@Evangelink Lets close this as duplicate of the Roslyn feature request that you cited in https://github.com/dotnet/roslyn-analyzers/issues/3210#issuecomment-747455459

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OmarTawfik picture OmarTawfik  路  3Comments

lgolding picture lgolding  路  4Comments

paulomorgado picture paulomorgado  路  3Comments

x3ntrix picture x3ntrix  路  3Comments

paulomorgado picture paulomorgado  路  3Comments