roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Enable CA1052 (static holder types should be static)

Open Youssef1313 opened this issue 3 years ago • 11 comments

Youssef1313 avatar Dec 07 '22 09:12 Youssef1313

@mavasani There are flagged public APIs here. I'll add suppressions.

Meanwhile, would you like me to open an issue for any of these suggesting a breaking change to discuss in API review?

Youssef1313 avatar Dec 07 '22 10:12 Youssef1313

Meanwhile, would you like me to open an issue for any of these suggesting a breaking change to discuss in API review?

IMO, it will be difficult to justify such a breaking change for a public API. @333fred for his views.

mavasani avatar Dec 07 '22 10:12 mavasani

@mavasani @333fred nothing good can come of people instantiating these types. I don't think we have any obligation to keep that working.

CyrusNajmabadi avatar Dec 07 '22 17:12 CyrusNajmabadi

@mavasani @333fred nothing good can come of people instantiating these types. I don't think we have any obligation to keep that working.

Not against it, but definitely needs to go through a public API review as it is a technically a breaking change.

mavasani avatar Dec 07 '22 17:12 mavasani

I guess I'm not convinced of the need to break those users.

333fred avatar Dec 07 '22 18:12 333fred

I guess I'm not convinced of the need to break those users.

Instantiating these types would indicate something completely broken in their code. First, i'm skeptical this would even exist anywhere. Second, it doesn't seem like this code could even do anything (you can't access statics through instance values for example). So it would have ot exist, and be unused for any purpose. Compat around that seems unnecessary. But we can certainly bring to discussion.

CyrusNajmabadi avatar Dec 07 '22 18:12 CyrusNajmabadi

I guess I'm ok having that discussion. @Youssef1313, can you please open an issue?

333fred avatar Dec 07 '22 18:12 333fred

roslyn-CI (Correctness Correctness_Analyzers) is green. This doesn't seem good. SymbolAnnotation should have failed since #65829 isn't yet merged.

Youssef1313 avatar Dec 07 '22 20:12 Youssef1313

@mavasani I did a local build of Workspaces via dotnet build src/Workspaces/Core/Portable/Microsoft.CodeAnalysis.Workspaces.csproj --configuration Debug --no-incremental -bl -p:RunAnalyzersDuringBuild=true -p:TreatWarningsAsErrors=true, and no diagnostic is reported for CA1052 for the violation of SymbolAnnotation. What I looked at in the binlog:

  • Confirm that C:\Users\PC\Desktop\roslyn\src\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\Simplification\SymbolAnnotation.cs is passed to csc.
  • Confirm /analyzerconfig:C:\Users\PC\Desktop\roslyn\eng\config\globalconfigs\Common.globalconfig
  • Confirm /analyzer:C:\Users\PC\.nuget\packages\microsoft.codeanalysis.netanalyzers\7.0.0-preview1.22218.1\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll and /analyzer:C:\Users\PC\.nuget\packages\microsoft.codeanalysis.netanalyzers\7.0.0-preview1.22218.1\analyzers\dotnet\cs\Microsoft.CodeAnalysis.NetAnalyzers.dll
  • The class SymbolAnnotation doesn't seem to have anything "special" other than a constant field. I tried a unit test in Roslyn.sln locally with a constant field and it is working properly.

Youssef1313 avatar Dec 08 '22 08:12 Youssef1313

I guess I'm ok having that discussion. @Youssef1313, can you please open an issue?

https://github.com/dotnet/roslyn/issues/65909

Youssef1313 avatar Dec 09 '22 14:12 Youssef1313

@mavasani This is ready for review, pending a question on why CI didn't fail for SymbolAnnotation despite a similar analyzer test in roslyn-analyzers is passing.

Youssef1313 avatar Dec 09 '22 21:12 Youssef1313

Enable CA1052 (static holder types should be static)

Why would we want to do this? I see nothing wrong in a non-static type with only static explicitly declared members.

AlekseyTs avatar Jan 26 '23 15:01 AlekseyTs

Enable CA1052 (static holder types should be static)

Why would we want to do this? I see nothing wrong in a non-static type with only static explicitly declared members.

@AlekseyTs The analyzer caught cases where the class should really have been static. https://github.com/dotnet/roslyn/issues/65909. The rate of false positives should be quite low and not that noisy, and source suppressions are suitable for such cases.

Youssef1313 avatar Jan 26 '23 19:01 Youssef1313

The analyzer caught cases where the class should really have been static. #65909

I do not think there was a real harm there, even if we decided to approve the suggested API change. I find this rule useless in general. It might provide some very small guidance around public API shape, but I prefer to not deal with this noise under Compilers. Could we exclude code under Compilers?

AlekseyTs avatar Jan 26 '23 21:01 AlekseyTs

The analyzer caught cases where the class should really have been static. #65909

I do not think there was a real harm there, even if we decided to approve the suggested API change. I find this rule useless in general. It might provide some very small guidance around public API shape, but I prefer to not deal with this noise under Compilers. Could we exclude code under Compilers?

Excluded in last commit. @AlekseyTs Do you want me to revert changes under Compilers too or they are good to go?

Note: Since most violations found under Compilers are tests, let me know if you find it noisy for tests specifically and we can disable it for tests only (ie, enabled in compilers product code but not tests).

Youssef1313 avatar Jan 29 '23 14:01 Youssef1313

@Youssef1313

Do you want me to revert changes under Compilers too or they are good to go?

In my opinion, all changes that are not fixing "real" problems should be reverted.

AlekseyTs avatar Jan 29 '23 17:01 AlekseyTs