dotnet / format

Home for the dotnet-format command

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CA1822 mistakenly triggers on partial class

austindonnelly opened this issue · comments

If you have a C# class, then rule CA1822 can incorrectly be flagged for a method which accesses a field defined in a separate file.

Example:
D:\projects\ConceptReader\testbed\catcher\liveview\MainWindow.xaml.cs(39,21): info CA1822: Member 'WriteLine' does not access instance data and can be marked as static [D:\projects\ConceptReader\testbed\catcher\liveview\liveview.csproj]

    public void WriteLine(string message)
        {
            Trace.WriteLine(message);
            statusText.Text = message;
        }

However, statusText is an internal field in the machine-generated MainWindows.g.i.cs partial class. This doesn't seem to be picked up by dotnet format and so it is happy to rewrite the WriteLine code to make it static, which doesn't compile anymore.

This looks like #1337. Do we know which version of the tools are in use here?

This is with

dotnet --version
7.0.400

This also repros with the latest 8.0 preview:

bash$ dotnet format --verify-no-changes --severity info --verbosity normal
  Formatting code files in workspace 'D:\projects\ConceptReader\testbed\catcher\catcher.sln'.
D:\projects\ConceptReader\testbed\catcher\liveview\MainWindow.xaml.cs(37,21): info CA1822: Member 'WriteLine' does not access instance data and can be marked as static [D:\projects\ConceptReader\testbed\catcher\liveview\liveview.csproj]
  Formatted code file 'D:\projects\ConceptReader\testbed\catcher\liveview\MainWindow.xaml.cs'.
  Format complete in 13773ms.

bash$ dotnet --version
8.0.100-preview.7.23376.3

@mavasani can you verify that CA1822 is not reported on a method where one or more qualifying identifiers fails to resolve?

@austindonnelly The versions listed above are for the dotnet SDK. To get the version of format, you need to pass --version to either dotnet format (for SDK-installed tools) or dotnet-format (for global tools).

I would assume 8.0.100-preview.7.23376.3 is at least as new as 8.0.100-preview.6.23330.14 which I have here, and this version should be new enough for #1337 to be fixed.

Ok, I've got:

bash$ dotnet format --version
7.4.431902+3c30490bb2ecae8967a3b5e79c97ab98de334676

I uninstalled the SDK for v8 as I had other issues with it.

@austindonnelly If you set the environment variable BuildingInsideVisualStudio to true before running dotnet format, does it work? If so, #1390 might be the solution for the issue. I'm not sure if it has any other negative consequences though.

Yes, this works!

bash$ unset BuildingInsideVisualStudio
bash$ dotnet format --no-restore --verify-no-changes --severity info --verbosity normal
  Formatting code files in workspace 'D:\projects\ConceptReader\testbed\catcher\liveview\liveview.csproj'.
D:\projects\ConceptReader\testbed\catcher\liveview\MainWindow.xaml.cs(37,21): info CA1822: Member 'WriteLine' does not access instance data and can be marked as static [D:\projects\ConceptReader\testbed\catcher\liveview\liveview.csproj]
  Formatted code file 'D:\projects\ConceptReader\testbed\catcher\liveview\MainWindow.xaml.cs'.
  Format complete in 6023ms.

bash$ export BuildingInsideVisualStudio=true
bash$ dotnet format --no-restore --verify-no-changes --severity info --verbosity normal
  Formatting code files in workspace 'D:\projects\ConceptReader\testbed\catcher\liveview\liveview.csproj'.
  Format complete in 6419ms.