Roslyn: Analyzers and code fixes that require .NET Standard 2.1 features should be disabled in projects not targeting it

Created on 24 May 2019  路  7Comments  路  Source: dotnet/roslyn

Version Used: Shipped with Visual Studio 2019 16.1

Steps to Reproduce:

  1. Create a new C# project that targets a framework that does not support .NET Standard 2.1 (e.g. any version of .NET Framework, .NET Core < 3.0, .NET Standard < 2.1).
  2. Add <LangVersion>preview</LangVersion> to the .csproj file.
  3. Make sure the C# code style analyzers "Prefer index operator" and "Prefer range operator" are enabled (this is the case by default).
    Alternatively add an .editorconfig file with those settings:
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
  1. Add the following code:
int[] numbers = { 1, 2 };
string text = "abc";
_ = numbers[numbers.Length - 1];
_ = text.Substring(1, text.Length - 1);
  1. Check the error list and suggested code fixes in the last two lines of the code.

Expected Behavior:
No analyzer messages or code fixes that belong to analyzer messages should be shown.

Actual Behavior:
Analyzer messages "IDE0056 Indexing can be simplified" and "IDE0057 Substring can be simplified" are shown.
If you apply the provided code fixes, compile errors are produced because indexers and ranges are not supported in the target framework of the project.

Note: Right now those two analyzers/code fixes are the only ones I know of that show this problem. But of course this should be applied to all analyzers/code fixes that require features of a specific target framework.

Area-IDE Bug IDE-CodeStyle Resolution-Not Reproducible

Most helpful comment

Agree with @sharwell. This is by-design for all analyzer execution, not just this specific analyzer. @sharwell is this good to close?

All 7 comments

I really, really don't wanna be that person but "Is there any movement here"?

I just followed the steps from the top for both netstandard2.0 and netcoreapp2.0, and was not able to reproduce the issue.

Hmmm... I鈥檓 definitely seeing range and index suggestions on multi-target libraries. I鈥檝e had to disable the rules.

on multi-target libraries

Yes, this is the expected behavior. The analysis operates in the current context, so if you have a file open in a context that supports these operations it will suggest them. The right fix is exactly what you did: disable the rules in .editorconfig since they do not apply for your project.

An alternative fix is to manually set <LangVersion> to the lowest version supported by all target frameworks involved in the build (e.g. C# 7 for this scenario). This will automatically disable analysis and fixes that require newer versions of the language, even when targeting frameworks that would otherwise support them.

Ah ok, I wasn鈥檛 aware of that. That鈥檚 ok then thanks!

Agree with @sharwell. This is by-design for all analyzer execution, not just this specific analyzer. @sharwell is this good to close?

Was this page helpful?
0 / 5 - 0 ratings