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

Invalid analyzer results in projects which target multiple frameworks with different language versions #1581

Open
doxxx opened this issue Nov 19, 2024 · 3 comments

Comments

@doxxx
Copy link

doxxx commented Nov 19, 2024

I have a project which is multi-targeted to .NET 4.7.2 and .NET 8. However, I am seeing Roslynator analyzer suggestions that are only valid for one of the target frameworks. For example, RCS1058 "compound assignment" is only valid for language version 8 or above but .NET 4.7.2 uses language version 7.3. The analyzer highlights the affected code but a potential fix is only provided when I have .NET 8 version of the project selected in the top-left dropdown in the editor.

Obviously there is some use to being able to see an analyzer result even if it can only be applied for some of the target frameworks. However, it also means that I'm seeing a lot of analyzer highlighting in the code that I can't / don't want to fix.

What if analyzers only displayed results for the currently selected target framework, as per the dropdown in the top left corner of the editor?

@josefpihrt
Copy link
Collaborator

Hi,

I assume you are talking about ??= operator.

Let's say I have following csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net6;net472</TargetFrameworks>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)' == 'net472'">
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>

</Project>

Then RCS1058 is suggested only if the language version supports it (I use Rider IDE):

Image

Image

Reference to the source code:

if (((CSharpCompilation)startContext.Compilation).LanguageVersion >= LanguageVersion.CSharp8)
{
startContext.RegisterSyntaxNodeAction(
c =>
{
if (DiagnosticRules.UseCompoundAssignment.IsEffective(c))
AnalyzeCoalesceExpression(c);
},
SyntaxKind.CoalesceExpression);
}

@doxxx
Copy link
Author

doxxx commented Nov 22, 2024

I'm using Visual Studio 2022. This is what I see:

Image

@doxxx
Copy link
Author

doxxx commented Nov 22, 2024

Also, when I press Ctrl-. to get suggested fixes, I get two different lists depending on whether net472 or net8 is selected:

Image

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

2 participants