roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

RCS0054 Accepting only strange formatting in this situation

Open eduardomezencio opened this issue 2 years ago • 4 comments
trafficstars

Product and Version Used: dotnet 7, Roslynator.Analyzers 4.5.0

Steps to Reproduce: The following code, in top level statements:

builder.Services
    .AddMyServices(builder.Configuration)
    .AddHangfire(config => config
        .UseSimpleAssemblyNameTypeSerializer()
        .UseRecommendedSerializerSettings()
        .UseInMemoryStorage())
    .AddHangfireServer();

Gives RCS0054 here:

    .AddHangfire(config => /*marks warning from here*/ config
        .UseSimpleAssemblyNameTypeSerializer()
        .UseRecommendedSerializerSettings()
        .UseInMemoryStorage()) /*to here*/

Actual Behavior: The action suggested by the Code Action keeps the code the same. I've tried multiple formattings, but the only one accepted by this rule is completely unacceptable, which is the following:

builder.Services
    .AddMyServices(builder.Configuration)
.AddHangfire(config => config
        .UseSimpleAssemblyNameTypeSerializer()
        .UseRecommendedSerializerSettings()
        .UseInMemoryStorage())
    .AddHangfireServer();

(This makes it feel in a way related to this other issue, already solved: https://github.com/JosefPihrt/Roslynator/issues/859)

Expected Behavior: I expect the first code I mentioned here to be accepted as correctly formatted.

eduardomezencio avatar Sep 15 '23 17:09 eduardomezencio

Hi,

Yes, the first code snippet should not produce any diagnostic as it's correctly formatted.

I tried to reproduce the behavior with following snippet but I'm not getting any diagnostic:

top-level statement (essentially entire file)

using System.Collections.Generic;
using System.Linq;

var items = new List<string>();

items
    .Select(f => f)
    .Select(f => f
        .Select(f => f)
        .Select(f => f))
    .Select(f => f);

Can I ask you to create working code sample that I could copy-paste to reproduce the behavior?

Just to rule that out: Can you check that indentation does not contain spaces mixed with tabs?

josefpihrt avatar Sep 15 '23 18:09 josefpihrt

I got the warning with the code you provided above.

eduardomezencio avatar Sep 18 '23 16:09 eduardomezencio

@eduardomezencio I believe you but I need to reproduce the bug.

The best would be to create a project which contains only single file containing code that causes the diagnostic and possibly include editorconfig file which may affect the behavior of the analyzer. Then zip the project and post it here (or create temporary repo if you wish). Thanks.

josefpihrt avatar Sep 22 '23 10:09 josefpihrt

Ok, thanks for looking into it. I'm trying to reproduce this here, too but I'm having problems. I tried to create an empty project enabling only this analyzer and copy your code example into it. Now when I build I get this in the build output:

CSC : warning AD0001: Analyzer 'Roslynator.Formatting.CSharp.FixFormattingOfCallChainAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. [/home/eduardo/rcs0054test/rcs0054test.csproj]
    1 Warning(s)
    0 Error(s)

So now the analyzer itself is having problems running.

This project consists only of a Roslynator.Formatting.Analyzers.analyzerconfig file with this inside:

is_global = true

# [RCS0054] Fix formatting of a call chain.
# http://pihrt.net/roslynator/analyzer?id=RCS0054
dotnet_diagnostic.RCS0054.severity = warning

And this in the .csproj

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <GlobalAnalyzerConfigFiles Include="Roslynator.Formatting.Analyzers.analyzerconfig" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Roslynator.Analyzers" Version="4.5.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Roslynator.CodeAnalysis.Analyzers" Version="4.5.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Roslynator.Formatting.Analyzers" Version="4.5.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

And the Program.cs, that contains your code from above. I'll try to look deeper into it later.

eduardomezencio avatar Sep 22 '23 19:09 eduardomezencio