format icon indicating copy to clipboard operation
format copied to clipboard

CA1822 mistakenly triggers on partial class

Open austindonnelly opened this issue 2 years ago • 8 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.

austindonnelly avatar Jul 27 '23 16:07 austindonnelly

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

sharwell avatar Aug 14 '23 21:08 sharwell

This is with

dotnet --version
7.0.400

austindonnelly avatar Aug 22 '23 10:08 austindonnelly

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

austindonnelly avatar Aug 22 '23 11:08 austindonnelly

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

sharwell avatar Aug 22 '23 15:08 sharwell

@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.

sharwell avatar Aug 22 '23 15:08 sharwell

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 avatar Aug 22 '23 16:08 austindonnelly

@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.

sharwell avatar Aug 22 '23 16:08 sharwell

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.

austindonnelly avatar Aug 22 '23 17:08 austindonnelly