Version Used: Shipped with Visual Studio 2019 16.1
Steps to Reproduce:
<LangVersion>preview</LangVersion> to the .csproj file.csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
int[] numbers = { 1, 2 };
string text = "abc";
_ = numbers[numbers.Length - 1];
_ = text.Substring(1, text.Length - 1);
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.
I really, really don't wanna be that person but "Is there any movement here"?
馃槙 According to the following test and #36909, this was fixed for the 16.3 release.
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?
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?