dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.07k stars 4.04k forks source link

IDE0051 should cover internal members in addition to private #68029

Open stephentoub opened 1 year ago

stephentoub commented 1 year ago

Summary

Extend IDE0051 to internals

Background and Motivation

IDE0051 flags private members that aren't ever called, read, written, etc. That same support is valuable but missing for internal members, too.

Proposed Feature

Extend IDE0051 to also flag internal members that aren't used. Whether internals are flagged in the presence of InternalsVisibleTo could be configurable, though ideally by default diagnostics would be suppressed for internals in the presence of IVT.

Alternative Designs

Building an entirely new analyzer in roslyn-analyzers.

geeknoid commented 1 year ago

Note CA1812 and CA1852 now support the ignore_internalsvisibleto option to control whether IVT affects the analysis or not, IDE0051 could do the same.

CyrusNajmabadi commented 1 year ago

Note, this would likely need to be a different approach (utilizing compilation -end analyzers). We know if a private symbol is unused after finishing that symbol. But we wouldn't know with an internal symbol until the whole compilation was done.

It would also have to be opt in for any assembly using ivt as it would produce too many false positives otherwise.

mavasani commented 1 year ago

@CyrusNajmabadi is correct.

Note that analyzing internal API usage would require full project analysis. Hence these diagnostics can only be reported at CompilationEnd, which means they will be build-only diagnostics and cannot support code fixes. These would still be valuable for customers who want to enable build time enforcement of unused internal APIs, but not something that will lightup automatically in the IDE.

One other alternate approach we have discussed in the past is to add a command under the Analyze menu in Solution explorer to explicitly run such project/solution level analyzers over the entire project/solution and potentially also apply the code fix - very similar to Remove unnecessary references command on the References/Dependencies node.