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

Recognize ConditionalAttribute in BannedApiAnalyzers. #7581

Open
manfred-brands opened this issue Feb 21, 2025 · 0 comments
Open

Recognize ConditionalAttribute in BannedApiAnalyzers. #7581

manfred-brands opened this issue Feb 21, 2025 · 0 comments

Comments

@manfred-brands
Copy link
Contributor

Analyzer

RS0030: BannedApiAnalyzers

Describe the improvement

We don't want libraries to use the type Console, except in Debug or Verbose builds whilst debugging.
So we added a conditional reference to the Microsoft.CodeAnalysis.BannedApiAnalyzers with an appropriate BannedSymbols.txt

  <ItemGroup Condition="'$(Configuration)'=='Release'">
    <PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4" />
    <AdditionalFiles Include="BannedSymbols.txt" />
  </ItemGroup>

However we have conditional executed code like:

[Conditional("VERBOSE")]
private static void Report(xxxx)
{
    System.Console.WriteLine(xxx);
}

I would like to be able to configure the analyzer to exclude reporting usages in methods annotated with the Conditional attribute using a specific constant.

Describe suggestions on how to achieve the rule

When usage of a banned api is detected, the analyzer should check for a ConditionalAttribute on the containing method/class and only report if the ConditionString doesn't match allowed conditions. Or better if the analyzer knows the predefined symbols doesn't match those.

Additional context

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant